Skip to content Skip to sidebar Skip to footer

How To Convert String Representation Of Dictionary In Pandas Dataframe To A New Columns?

I have a string representation of dictionary in Pandas DataFrame Column like this: >>> df['the_column'] 0 '{'a': 1., 'b': 2., 'c':3.}' 1 '{'a': 4., 'b': 5., 'c':6.}'

Solution 1:

You can try with ast

df['the_column']=df['the_column'].apply(ast.literal_eval)

Solution 2:

In addition to the accepted answer I found this works as well

pd.concat([df, df.the_column.apply(json.loads).apply(pd.Series)], axis=1)

Post a Comment for "How To Convert String Representation Of Dictionary In Pandas Dataframe To A New Columns?"