'Extra values from list inside a dictionary column

label_dict
[{'colour': 'Red', 'value': '20'}, {'colour': 'Black', 'value': '23'}, {'colour': 'Brown ', 'value': '12'}, ]
[{'colour': 'Red', 'value': '15'}, {'colour': 'Black', 'value': '76'}, {'colour': 'Brown ', 'value': '45'}, ]
[{'colour': 'Black', 'value': '54'}, {'colour': 'Brown ', 'value': '56'}, ]
[{'colour': 'Brown ', 'value': '63'}, ]
.
.
.
[]
[{'colour': 'Red', 'value': '45'}, {'colour': 'Black', 'value': '48'}, {'colour': 'Brown ', 'value': '1'}, 

I want to extract the value if Red is present in the dictionary to column name df[Red]

df['Red'] = df['label_dict'].apply(lambda x: next((y.get('value', '') for y in x if y.get('colour') == 'Red'), ''))

df['Red'] = df['label_dict'].apply(lambda x: x.get('Red') if x else None)

Both ways gives me an error



Solution 1:[1]

Pandas approach

Explode the label column then extract the value corresponding to Red colour then group the dataframe by index and select first value per group:

s = df['label_dict'].explode()
df['Red'] = s.str['value'].mask(s.str['colour'].ne('Red')).groupby(level=0).first()

Result:

>>> df['Red']                                                                                                

0      20
1      15
2    None
3    None
Name: Red, dtype: object

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shubham Sharma