我有一个如下的pandas数据框:
df=pd.DataFrame({'a':['red','yellow','blue'], 'b':[0,0,1], 'c':[0,1,0], 'd':[1,0,0]})
df
看起来像
a b c d
0 red 0 0 1
1 yellow 0 1 0
2 blue 1 0 0
我希望将其转换为字典,以便得到以下结果:
red d
yellow c
blue b
这个数据集很大,所以请避免使用任何迭代方法。我还没有想出一个解决方案。感谢任何帮助。
pandas
中提供的to_dict
函数将其转换为字典。具体用法请参考:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_dict.html - Vivek Kalyanarangan