python pandas非独特的字典键

问题描述 投票:1回答:1

我有一个包含这样数据的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']} 
python pandas dictionary
1个回答
4
投票

这个怎么样?

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']}
© www.soinside.com 2019 - 2024. All rights reserved.