PythonPandas不均匀的dict键

2024-04-28 13:27:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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

我用下面的代码把这个变成字典

^{pr2}$

当我执行上述操作时,我得到了错误

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']} 

Tags: anapplemakeissomethiscanalso
1条回答
网友
1楼 · 发布于 2024-04-28 13:27:14

这个怎么样?在

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']}

相关问题 更多 >