0

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.

The first dictionary has 3 values (integer) and the second one has one value which correspond to a set of words. 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.

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

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.

enter image description here

Is there a way to solve it using pandas (I have to use pandas for this seminar.

Thank you.

kely789456123
  • 605
  • 1
  • 6
  • 21

2 Answers2

1

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.

Niels Hameleers
  • 1,201
  • 10
  • 11
  • thank you but I also want the key to be integrate in the data frame alongside the list. since I Moving the column at the beguinning of the dataframe and writing to an existing file. see above update code – kely789456123 May 19 '19 at 20:03
1

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)
Tal Avissar
  • 10,088
  • 6
  • 45
  • 70