I have a column in pandas dataframe that has the following structure (see example). I think I have a nested dictionary in a single column, and I want each key to have it’s own column. I want all the matching keys to be the same column. Run the examples for more details
JavaScript
x
11
11
1
import pandas as pd
2
import numpy as np
3
4
data = ["{'age': 59, 'gender': 'Female','pain': 'No', 'date': '2022-09-29'}",
5
"{'gender': 'Male', 'date': '2022-10-11'}",
6
"{'age': 18, 'date': '2022-10-11', 'pain': 'No'}",
7
]
8
9
original_df = pd.DataFrame(data, columns=['test'])
10
original_df.head()
11
I want to explode the dataframe so that it has the following structure:
JavaScript
1
6
1
data = [[59,'Female','No','2022-09-29'], [np.nan,'Male',np.nan,'2022-10-11'], [18,np.nan,'2022-10-11','No']]
2
3
# Create the pandas DataFrame
4
desired_df = pd.DataFrame(data, columns=['age','gender','date','pain'])
5
desired_df.head()
6
Advertisement
Answer
Using json.normalize
JavaScript
1
14
14
1
from ast import literal_eval
2
3
import pandas as pd
4
5
6
original_df["test"] = original_df["test"].apply(literal_eval)
7
df = original_df.join(pd.json_normalize(original_df.pop("test")))
8
print(df)
9
10
age gender pain date
11
0 59.0 Female No 2022-09-29
12
1 NaN Male NaN 2022-10-11
13
2 18.0 NaN No 2022-10-11
14