Reputation: 5822
In my dataframe, there is a column of dictionaries:
ID | name | value | stats |
---|---|---|---|
{'mean': 154.0, 'median': 154.0, 'std': 0.0} | |||
{'mean': 131.19, 'median': 93.68, 'std': 53.04} |
I need to break down that column as new columns
ID | name | value | mean | median | std |
---|---|---|---|---|---|
154.0 | 154.0 | 0.0 | |||
131.19 | 93.68 | 53.04 |
I tried to use pd.json_normalize
as follow:
df2 = pd.json_normalize(df['stats'])
df2
But this way I lose the indexes in df2
and can't join them to add new columns to df
. How should I do it?
Upvotes: 2
Views: 62
Reputation: 4607
You can try applying a pd.Series on each dictionary , it would convert as individual dataframe, followed by merging
pd.concat([df, df['col'].apply(pd.Series)], axis=1).drop('col',axis=1)
other approach
df.merge(df['col'].apply(pd.Series), left_index=True, right_index=True, how='outer').drop('col',axis=1)
Example
df = pd.DataFrame()
df['col'] = [{'mean': 154.0, 'median': 154.0, 'std': 0.0},
{'mean': 131.19, 'median': 93.68, 'std': 53.04}]
df['some'] =1
Prior
col some
0 {'mean': 154.0, 'median': 154.0, 'std': 0.0} 1
1 {'mean': 131.19, 'median': 93.68, 'std': 53.04} 1
OUt:
some mean median std
0 1 154.00 154.00 0.00
1 1 131.19 93.68 53.04
Upvotes: 1