如何在Pandas DataFrame中保存字典



上下文

我有一个PandasDataFrameUse Case,其中我需要在Columns中存储Collection(Dictionary(。我知道,DataFrame可能不是为此而设计的,然而,我正在寻找一个在多个DataFramesSeries等之间没有Relationships的简单解决方案。

我目前收到以下错误:

ValueError:值(3(的长度与索引(5(的长度不匹配


代码

procedures = {'A': 'Procedure A', 'B': 'Procedure B', 'C': 'Procedure C'}
# I would like to create a new Column 'Procedures' and assign the Dictionary above to it for every Row.
data['Procedures'] = procedures

问题

  • Pandas是否可以实现这一点?我如何实现这一目标

按DataFrame长度的倍数重复dict列表:

data['Procedures'] = [procedures] * len(data)

要在Pandas DataFrame中保存Dictionary,请使用pd.DataFrame.from_dict(input_dict)

如果您认为不清楚,请参阅此链接。

最新更新