Exploding a list-like column with Pandas explode() method

Introduction to Pandas explode() and how to use it to reshape a Series or a DataFrame

B. Chen

--

Photo by Sincerely Media on Unsplash

In the step of data pre-processing, we often need to prepare our data in specific ways before feeding it into a machine learning model. One of the examples is to transform list-like columns into rows. Pandas provides various methods for that, among them apply() and explode() are the two most popular methods.

Pandas explode() example

In this article, we’ll explore how to use the Pandas explode() method to unnest a list-like column. This article covers the following topics:

1. Basic usage
2. Series use cases
2.1 Other singular entries
2.2 Empty list
2.3 String with delimiter
3. DataFrame use cases
3.1 Other singular entries
3.2 Empty list
3.3 Multiple list-like columns
3.4 String with delimiter
4. explode() v.s. apply()
Conclusion

Please check out the Notebook for source code. More tutorials are available from Github Repo.

1. Basic usage

Let’s take a look at Pandas explode(column, ignore_index):

  • The 1st argument column is to specify the column(s) to be exploded or unnested.
  • The 2nd argument ignore_index is to reset the index. If True, the resulting index will be labeled 0, 1, , n-1.

This method is available in both Series and DataFrame. The simplest use case of explode() is on a Series, for example, to unnest a list-like column “students”, we can call df['students'].explode():

df = pd.DataFrame({
'class': ['Year 1', 'Year 2'],
'students': [['Tom', 'Jane'], ['Liz', 'James']]
})

df['students'].explode()

--

--