[英]python - Convert pandas dataframe to json or dict and then back to df with non-unique columns
[英]python pandas non-unique dict keys
我有一个Excel文件,其中包含这样的数据
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
我使用以下代码将其变成字典
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'))
当我执行以上操作时,出现错误
UserWarning: DataFrame columns are not unique, some columns will be omitted.
我想要一本看起来像下面的字典
{'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']}
这个怎么样?
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']}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.