将数据框转换为字典

时间:2019-03-12 06:16:04

标签: python python-3.x pandas dataframe dictionary

我有一个数据框,我希望它选择一些列并以某种方式将其转换为Dictionary

数据框:

Dataframe :

这是我想要的输出

{20: [4.6, 4.3, 4.3, 20],
21: [4.6, 4.3, 4.3, 21],
 22: [6.0, 5.6, 9.0, 22],
 23: [8.75, 5.6, 6.6, 23]}

我已经尝试过了

items_dic = data[["Length","Width","Height","Pid" ]].set_index('Pid').T.to_dict('list')

items_dic = {20: [4.6, 4.3, 4.3],
 21: [4.6, 4.3, 4.3],
 22: [6.0, 5.6, 9.0],
 23: [8.75, 5.6, 6.6]}

,但这不包括值列表中的Pid 有人可以解释为什么吗?

1 个答案:

答案 0 :(得分:11)

DataFrame.set_index中设置参数curl: (3) URL using bad/illegal format or missing URL Makefile:35: recipe for target 'something' failed make: *** [savetradesdbscheme] Error 3,因为默认参数drop=False将列移到索引:

drop=False