【发布时间】:2018-11-17 22:57:26
【问题描述】:
我有一本字典,其中包含以下值:
cols = {'animals':['dog','cat','fish'],
'colors':['red','black','blue','dog']}
我想将其转换为一个数据框,其中每个列表都根据它们的键进行枚举,结果为
key variable
animals dog
animals cat
animal fish
colors red
colors black
colors blue
colors dog
到目前为止,我已经这样做了:但它没有为我提供想要的结果。
cols_df = pd.DataFrame.from_dict(cols, orient='index')
如何修改它以实现上述目标?
【问题讨论】:
-
你想要长格式,但
from_dict(.. orient='index')只提供宽格式,from_dict(.. orient='columns')失败并显示ValueError('arrays must all be same length')
标签: python list pandas dictionary dataframe