小编sin*_*ngh的帖子

将pandas数据帧转换为字典

我有一个pandas数据帧如下:

df=pd.DataFrame({'a':['red','yellow','blue'], 'b':[0,0,1], 'c':[0,1,0], 'd':[1,0,0]})
df
Run Code Online (Sandbox Code Playgroud)

看起来像

    a       b   c   d
0   red     0   0   1
1   yellow  0   1   0
2   blue    1   0   0
Run Code Online (Sandbox Code Playgroud)

我想将它转换为字典,以便我得到:

red     d
yellow  c
blue    b
Run Code Online (Sandbox Code Playgroud)

如果数据集非常大,请避免使用任何迭代方法.我还没有找到解决方案.任何帮助表示赞赏.

python dictionary dataframe python-3.x pandas

4
推荐指数
1
解决办法
3200
查看次数

标签 统计

dataframe ×1

dictionary ×1

pandas ×1

python ×1

python-3.x ×1