Reputation: 290
I have a column in a dataframe which is a list of dictionaries. Eg:
[{'attr': 'color', 'value': 'BLUE'}]
The issue is 'attr' and 'value' are not required, and I want the format in all rows to change to :
[{'color' : 'BLUE'}]
pandas str.replace is not working since 'attr' and 'value' are contained within quotes, and I am unable to write a suitable regex that completely removes 'attr' and 'value' but keeps the quotes over color and BlUE.
What can be a pythonic way to solve this problem?
Upvotes: 0
Views: 198
Reputation: 2696
Something like this:
df = pd.DataFrame({'col':[[{'attr': 'color', 'value': 'BLUE'}]]})
df['col'].apply(lambda x: {elt['attr']: elt['value'] for elt in x})
Upvotes: 1
Reputation: 82765
Using .apply
Ex:
import pandas as pd
df = pd.DataFrame({"A": [[{'attr': 'color', 'value': 'BLUE'}]]})
df["A"] = df["A"].apply(lambda x: [dict([i.values()]) for i in x])
print(df)
Output:
A
0 [{'color': 'BLUE'}]
Upvotes: 2