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'}}
CodePudding user response:
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}
CodePudding user response:
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.