• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Pandas 101 / Pandas explode(): Convert list-like column elements to separate rows

Pandas explode(): Convert list-like column elements to separate rows

June 28, 2020 by cmdlinetips

Panads explode() function is one of the coolest functions to help split a list like column elements into separate rows. Often while working with real data you might have a column where each element can be list-like. By list-like, we mean it is of the form that can be easily converted into a list.
Pandas explode
import pandas as pd

Let us create a toy data frame containing data science books in Python and R. It has two columns, one column is programming language and the other column is name of books.

df = pd.DataFrame({'Book' : ['R for Data Science, Data Visualization' , 
                             "Python for Data Analysis, Python Machine Learning"],
        'Language' : ["R", "Python"]})

You can see that each row in book column has names of multiple book names separated by a delimiter, comma in this example.

df 

Book	Language
0	R for Data Science, Data Visualization	R
1	Python for Data Analysis, Python Machine Learning	Python

Create Column with Lists

In this toy data set the Book column is list-like as it can be easily converted to a list. We can convert the column with elements separated by a delimiter into a list of strings using str.split() function. We use Pandas’ assign() function to assign the list to a variable with the same name and create a list column.

df.assign(Book=df.Book.str.split(","))

Book	Language
0	[R for Data Science, Data Visualization]	R
1	[Python for Data Analysis, Python Machine Lea...	Python

Pandas explode() to separate list elements into separate rows()

Now that we have column with list as elements, we can use Pandas explode() function on it. Pandas explode() function will split the list by each element and create a new row for each of them. In this example, each book name goes to a separate row and it also copies other columns for each element.

In this example, we get separate row for each book and also corresponding Language element.

df.assign(Book=df.Book.str.split(",")).explode('Book')
	Book	Language
0	R for Data Science	R
0	Data Visualization	R
1	Python for Data Analysis	Python
1	Python Machine Learning	Python

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailPandas 0.25.0 is Here. What is New? Named aggregation, explode() and sparse dataframe Default ThumbnailHow To Separate a Column into Multiple Rows with in R? Default ThumbnailHow to Get Column Names as List in Pandas? Default ThumbnailHow To Convert a Column to Row Name/Index in Pandas?

Filed Under: Pandas 101 Tagged With: Pandas explode

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version