首页 > 解决方案 > 在 datframe 的每一行中添加两个已经在字典中的字典

问题描述

我的字典在一列的每一行中有两个字典。列名是“价格”,我在这里写了两行:

row1 = {'offer_price': {'currency': 'GBP', 'value': 35.0}, 'regular_price': {'currency': 'GBP', 'value': 35.0}}

row2 = {'offer_price': {'currency': 'GBP', 'value': 55.0}, 'regular_price': {'currency': 'GBP', 'value': 55.0}}

我需要创建一个包含三列的数据框,即“offer_price”、“regular_price”和“currency”,“value”在价格列中,GBP 在货币列中。

我已经尝试过,但我没有得到所需的数据框

df1 = pd.DataFrame()

for rows in df.price:

    x = pd.DataFrame(rows)

    df1 = df1.append(x)

标签: pythonpandas

解决方案


你可以使用这个命令

pd.DataFrame.from_dict(row1)

它将允许您从 dict 创建 Dataframe。

“row1”的结果

你可以在这里找到文档:https ://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_dict.html

我希望它会帮助你!


推荐阅读