Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: How to explode data frame with json arrays

How to explode pandas data frame?

Input df:

enter image description here

Required output df:

+----------------+------+-----+------+
|level_2         | date | val | num  | 
+----------------+------+-----+------+
| name_1a        | 2020 |  1  | null |
| name_1b        | 2019 |  2  | null |
| name_1b        | 2020 |  3  | null |
| name_10000_xyz | 2018 |  4  | str  |
| name_10000_xyz | 2019 |  5  | null |
| name_10000_xyz | 2020 |  6  | str  |
+------------------------------------+

To reproduce input df:

import pandas as pd
pd.set_option('display.max_colwidth', None)
data={'level_2':{1:'name_1a',3:'name_1b',5:'name_10000_xyz'},'value':{1:[{'date':'2020','val':1}],3:[{'date':'2019','val':2},{'date':'2020','val':3}],5:[{'date':'2018','val':4,'num':'str'},{'date':'2019','val':5},{'date':'2020','val':6,'num':'str'}]}}
df = pd.DataFrame(data)
like image 560
Dan Avatar asked Oct 28 '25 06:10

Dan


1 Answers

Explode the dataframe on value column, then pop the value column and create a new dataframe from it then join the new frame with the exploded frame.

s = df.explode('value', ignore_index=True)
s.join(pd.DataFrame([*s.pop('value')], index=s.index))

          level_2  date  val  num
0         name_1a  2020    1  NaN
1         name_1b  2019    2  NaN
2         name_1b  2020    3  NaN
3  name_10000_xyz  2018    4  str
4  name_10000_xyz  2019    5  NaN
5  name_10000_xyz  2020    6  str
like image 91
Shubham Sharma Avatar answered Oct 30 '25 23:10

Shubham Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!