I have an Excel file with data like this
Fruits Description
oranges This is an orange
apples This is an apple
oranges This is also oranges
plum this is a plum
plum this is also a plum
grape I can make some wine
grape make it red
I'm turning this into a dictionary using the below code
import pandas as pd
import xlrd
file = 'example.xlsx'
x1 = pd.ExcelFile(file)
print(x1.sheet_names)
df1 = x1.parse('Sheet1')
#print(df1)
print(df1.set_index('Fruits').T.to_dict('list'))
When i execute the above i get the error
UserWarning: DataFrame columns are not unique, some columns will be omitted.
I want to have a dictionary that looks like the below
{'oranges': ['this is an orange', 'this is also oranges'], 'apples':['this is an apple'],
'plum'['This is a plum', 'this is also a plum'], 'grape'['i can make some wine', 'make it red']}
How about this?
df.groupby(['Fruits'])['Description'].apply(list).to_dict()
{'apples': ['This is an apple'],
'grape': ['make it red', 'I can make some wine'],
'oranges': ['This is an orange', 'This is also oranges'],
'plum': ['this is a plum', 'this is also a plum']}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.