简体   繁体   中英

How to loop through nested dictionaries and extract keys and values from sub-dictionaries containing a specific key?

If you need to loop through multiple nested dictionaries like the two below, how would you get the keys and values only from sub-dictionaries with the key 'fruit'? The goal is to create a dataframe with three columns: 'color', 'price' and 'fruit'.

{'A':{'color':'red','price':22, 'fruit':'apple'},'B':{'color':'orange','price':123, 'vegetable':'carrot'}}

{'X':{'color':'yellow','price':2, 'fruit':'banana'},'Y':{'color':'yellow','price':14, 'vegetable':'melon'}}

Use a dictionary comprehension with an if condition.

stock = {'A':{'color':'red','price':22, 'fruit':'apple'},'B':{'color':'orange','price':123, 'vegetable':'carrot'}}

only_fruit = {key: value for key, value in stock.items() if 'fruit' in value}

Looks like multiple nested dictionaries are different variables.

You can try this :

import pandas as pd

nested_dict1 = {'A':{'color':'red','price':22, 'fruit':'apple'},'B':{'color':'orange','price':123, 'vegetable':'carrot'}}

nested_dict2 = {'X':{'color':'yellow','price':2, 'fruit':'banana'},'Y':{'color':'yellow','price':14, 'vegetable':'melon'}}

final_dict = {"fruit" :[], "color":[], "price":[]}

for key, val in nested_dict1.items():
    if "fruit" in val:
        final_dict["fruit"].append(val["fruit"])
        final_dict["color"].append(val["color"])
        final_dict["price"].append(val["price"])

for key, val in nested_dict2.items():
    if "fruit" in val:
        final_dict["fruit"].append(val["fruit"])
        final_dict["color"].append(val["color"])
        final_dict["price"].append(val["price"])

df = pd.DataFrame(final_dict)

print(df)

Output:

    fruit   color  price
0   apple     red     22
1  banana  yellow      2

Here I have created 2 different loops for 2 different multiple nested dictionaries. Depending on your use case you can optimise it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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