I have a data frame with one json column and I want to split them into multiple columns.
Here is a df I’ve got.
JavaScript
x
3
1
check
2
0 [[9.14,-79.76],[36.96,-76.42],"2021-01-05 06:45:00","2021-02-03 08:00:00", "2021-19690","2021-10230"]
3
I want the output as below:
JavaScript
1
3
1
0 1 2 3 4 5
2
9.14,-79.76 36.96,-76.42 "2021-01-05 06:45:00" "2021-02-03 08:00:00" 2021-19690 2021-10230
3
I’ve tried
JavaScript
1
6
1
df = pd.json_normalize(df['check'])
2
3
and
4
5
df['check'].str.split(",", expand=True)
6
Both didn’t work. can someone please tell me how to get output that I want?
Advertisement
Answer
One way using pandas.DataFrame.explode
:
JavaScript
1
3
1
new_df = df.explode("check", ignore_index=True).T
2
print(new_df)
3
Output:
JavaScript
1
6
1
0 1 2
2
check [9.14, -79.76] [36.96, -76.42] 2021-01-05 06:45:00
3
4
3 4 5
5
check 2021-02-03 08:00:00 2021-19690 2021-10230
6