Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      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?

>Solution :

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  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading