简体   繁体   English

PYTHON BEGUINNER:如何从python字典列表中创建pandas数据框?

[英]PYTHON BEGUINNER : How to create pandas dataframe from a list of python dictionaries?

I am looking for a way of creating a pandas DataFrame and then add it in an excel file using pandas from a list of dictionary. 我正在寻找一种创建pandas DataFrame的方法,然后使用字典列表中的pandas将其添加到excel文件中。

The first dictionary has 3 values (integer) and the second one has one value which correspond to a set of words. 第一个字典具有3个值(整数),第二个字典具有与一组单词相对应的一个值。 The key for the two dictionaries are the same but to be sure there is not error in the excel file I prefer to have them in the DataFrame. 两个字典的键是相同的,但是要确保excel文件中没有错误,我希望将它们放在DataFrame中。

d1 = {'1': ['45', '89', '96'], '2': ['78956', '50000', '100000'], '3': ['0', '809', '656']}
d2 = {'1': ['connaître', 'rien', 'trouver', 'être', 'emmerder', 'rien', 'suffire', 'mettre', 'multiprise'], '2': ['trouver', 'être', 'emmerder'], '3' : ['con', 'ri', 'trou', 'êt', 'emmer',]}

I am getting error at each tentative and i am really block and I need a solution 我在每个尝试中都遇到错误,而且我真的很堵,我需要一个解决方案


df = pd.read_csv(sys.argv[1], na_values=['no info', '.'], encoding='Cp1252', delimiter=';')
df1 = pd.DataFrame(d1).T.reset_index()
df1['value1_d2'] = ''
# iterate over the dict and add the lists of words in the new column
for k,v in d2.items():
    df1.at[int(k) - 1, 'value1_d2'] = v 
#print(df1)
df1.columns = ['id','value_1_Dict1','value_2_Dict1','value_3_Dict1',' value_2_Dict2']
cols = df1.columns.tolist()
cols = cols[-1:] + cols[:-1]
df1 = df1[cols]
print(df1)
df = pd.concat([df, df1], axis = 1)
df.to_excel('exit.xlsx')


I do not have an error but the filling of the dataframe start after the real columns like in the example and I have more then 2000 lines 我没有错误,但是数据帧的填充在示例中的真实列之后开始,并且我的行数超过了2000行

Expected output: I add it in an existing file : 预期输出:我将其添加到现有文件中:

  score  freq    **value1_d2                       id value1   value2 value3  **    
0  0.5     2     **['connaître', 'rien', 'trouver'] 1  45       89       96   **
1  0.8     5     ** ['trouver', 'être', 'emmerder'] 2  78956    5000    100000 **   
2  0.1     5     **['con', 'ri', 'trou', 'êt', 'emmer',] 3  0        809     65  **


When trying to add to excel file I have the following error, I want to start writing from the first column so that the key will be the same. 尝试添加到excel文件时,出现以下错误,我想从第一列开始写入,以便键相同。

在此处输入图片说明

Is there a way to solve it using pandas (I have to use pandas for this seminar. 有没有一种方法可以使用大熊猫解决这个问题(我必须在本次研讨会中使用大熊猫。

Thank you. 谢谢。

This way you can add the lists of words in a cell: 这样,您可以在单元格中添加单词列表:

df1 = pd.DataFrame(d1)

# the new column needs to have dtype object
df1['value1_d2'] = ''

# iterate over the dict and add the lists of words in the new column
for k,v in d2.items():
    df1.at[int(k) - 1, 'value1_d2'] = v

I used the info in this post as well. 我也使用了这篇文章中的信息。

When reading dictionary into a dataframe you can use : 将字典读入数据框时,可以使用:

>>> d1 = {'1': ['45', '89', '96'], '2': ['78956', '50000', '100000'], '3': ['0', '809', '656']}
>>> df1 = pd.DataFrame.from_dict(d1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM