rj dj
rj dj

Reputation: 290

Replace string containing quotes in pandas dataframe

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

Answers (2)

ags29
ags29

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

Rakesh
Rakesh

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

Related Questions