Skip to content
Advertisement

python convert single json column to multiple columns

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.

     check
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"]

I want the output as below:

      0            1                  2                        3                4        5
9.14,-79.76   36.96,-76.42   "2021-01-05 06:45:00"   "2021-02-03 08:00:00"  2021-19690  2021-10230

I’ve tried

df = pd.json_normalize(df['check'])

and

df['check'].str.split(",", expand=True)

Both didn’t work. can someone please tell me how to get output that I want?

Advertisement

Answer

One way using pandas.DataFrame.explode:

new_df = df.explode("check", ignore_index=True).T
print(new_df)

Output:

                    0                1                    2  
check  [9.14, -79.76]  [36.96, -76.42]  2021-01-05 06:45:00   

                         3           4           5  
check  2021-02-03 08:00:00  2021-19690  2021-10230  
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement