小编jum*_*947的帖子

python pandas非唯一字典键

我有一个包含这样数据的 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
Run Code Online (Sandbox Code Playgroud)

我正在使用以下代码将其转换为字典

 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'))
Run Code Online (Sandbox Code Playgroud)

当我执行上述我得到错误

UserWarning: DataFrame columns are not unique, some columns will be omitted.
Run Code Online (Sandbox Code Playgroud)

我想要一本看起来像下面的字典

{'oranges': ['this is an orange', 'this is …
Run Code Online (Sandbox Code Playgroud)

python dictionary pandas

3
推荐指数
1
解决办法
1290
查看次数

标签 统计

dictionary ×1

pandas ×1

python ×1