Unnest (explode) a column of list in Pandas

In python, when you have a list of lists and convert it directly to a pandas dataframe, you get columns of lists. This may seem overwhelming, but fear not! Pandas comes to our rescue once again – use pandas.DataFrame.explode()

import pandas as pd
df = pd.DataFrame({'col1': [[0, 1, 2], 'foo', [], [3, 4]],
                   'col2': 1,
                   'col3': [['a', 'b', 'c'], np.nan, [], ['d', 'e']]})

The dataframe looks like this

Let’s explode the first column alone

df.explode('col1')

The result looks like this

Let’s say we want to explode both col1 and col3 simultaneously:

df.explode(['col1', 'col3'])

The result looks like this

Note that for exploding multiple columns simultaneously, you need Pandas version > 1.3

Ref: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.explode.html

Image source: Unsplash

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s