python - 在 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)
解决方案
你可以使用这个命令
pd.DataFrame.from_dict(row1)
它将允许您从 dict 创建 Dataframe。
你可以在这里找到文档:https ://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_dict.html
我希望它会帮助你!
推荐阅读
- python - 根据其他数据框中的行和列信息索引数据框
- protractor - 在 afterEach 块中放置断言是一种好习惯吗?
- php - 是否可以传递一个变量来路由像 api.php 这样的文件以用作中间件的名称?
- greenfoot - 多人游戏方向键
- html - 带有文本的按钮内部的画布
- c# - 如何在 Xamarin.Forms 中将 LuminanceSource 转换为位图?
- windows-services - 将 PROCESS_DUP_HANDLE 授予服务
- javascript - 比较一个日期时间与当前日期时间
- php - 尝试在后端使用 php 并使用 android 作为前端将值插入 mysql
- spree - 如何使用 spree API v2 SDK 获取 Token