繁体   English   中英

从另一本词典中的嵌套词典中提取信息

[英]Extract information from nested dictionaries inside another dictionary

数据集包含25列500行,其中一列是包含嵌套字典的“ orderItems”,“ orderItems”的所有键都包含1至15个字典。 随机取一行,例如:

dataset.orderItems[691581]

结果:

[{'product': 10152, 'price': 78.76, 'quantity': 1.0},
 {'product': 3584, 'price': 20.9, 'quantity': 1.0},
 {'product': 20308, 'price': 9.9, 'quantity': 1.0},
 {'product': 7619, 'price': 13.9, 'quantity': 1.0},
 {'product': 3795, 'price': 15.9, 'quantity': 1.0},
 {'product': 6504, 'price': 18.9, 'quantity': 2.0},
 {'product': 13720, 'price': 75.9, 'quantity': 1.0},
 {'product': 18419, 'price': 31.9, 'quantity': 1.0}]

想要创建3列:“产品”,“价格”和“数量”,以使其适合这些列中所有词典的所有信息。 上面的示例是从“ orderItems”的单个值中提取的8行。 从“ dataset.orderItems [691581]”中提取的价格,产品和数量信息将在这3列中划分为各自的适当列。 请记住,有些键有1个字典,其他键有15个(máx)

有人可以帮助我吗?

这已返回您的行号的列表。 您可以这样处理:

for dict_current in dataset.orderItems[691581]:
    i_prod_num = dict_current["product"]
    i_price = dict_current["price"]
    fl_quantity = dict_current["quantity"]

接下来要做什么取决于您要使用这些值做什么。

请尝试以下操作:

list_df = []
for i, row in enumerate(df.values):
   df_values = df.loc[i,"orderitems"]
   for i, row in enumerate(df_values): 
        list_df.append(pd.DataFrame([row]))
 df_values_final=pd.concat(list_df)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM