简体   繁体   English

在数据框的每一行中添加两个已经在字典中的字典

[英]add two dictionaries that are already inside a dictionary in each row of datframe

I have dictionary that has two dictionaries inside it in each row of a column.我有一个字典,每一列的每一行都有两个字典。 Column name is 'price' and I have written two rows here:列名是“价格”,我在这里写了两行:

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}}

I need to create a dataframe that has three columns, namely 'offer_price', 'regular_price' and 'currency', with the 'value' in both price columns and GBP in currency column.我需要创建一个包含三列的数据框,即“offer_price”、“regular_price”和“currency”,价格列中为“value”,货币列中为 GBP。

I have tried but I am not getting the required dataframe我试过了,但我没有得到所需的数据框

df1 = pd.DataFrame()

for rows in df.price:

    x = pd.DataFrame(rows)

    df1 = df1.append(x)

You could use this command你可以使用这个命令

pd.DataFrame.from_dict(row1)

It will permits you to create Dataframe from dict.它将允许您从 dict 创建 Dataframe。

Result for 'row1' 'row1' 的结果

You can find documentation here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_dict.html您可以在此处找到文档: https : //pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.from_dict.html

I hope it will helps you !我希望它会帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加两个已经在同一个列表中的字典 - adding two dictionaries that are already inside same list 从字典列表中创建 Pandas DataFrame? 每个字典在 DataFrame 中作为行? - Creating a Pandas DataFrame from list of dictionaries? Each dictionary as row in DataFrame? 将两个变量作为值添加到已创建的字典中 - Add two variables as values to an already created dictionary 将字典添加到词典列表 - Add a dictionary to list of dictionaries 如果与列表中每个字典中的一个键的值匹配,则将具有不同词典值的新键添加到词典列表中 - Add new key to list of dictionaries with the values from a different dictionaries if matched with value of one key in each dictionary in a list 如何向字典中的每一行添加一个 Id? python - how to add an Id to each row in dictionary ? python Python:如何遍历词典列表并将每个词典作为值添加到具有唯一键的新词典中-无需重复 - Python: How to iterate over a list of dictionaries and add each dictionary as value to a new dictionary with a unique key - no repeats 将两个字典合并为Python字典 - Merging two dictionaries into a dictionary of dictionaries in Python 更改字典词典中每个词典的类型 - Changing the type of each dictionary in a dictionary of dictionaries 在python中添加字典列表的字典 - add to a dictionary of list of dictionaries in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM