Open In App

How to Split Explode Pandas DataFrame String Entry to Separate Rows

Last Updated : 27 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

When working with Pandas, you may encounter columns with multiple values separated by a delimiter. To split these strings into separate rows, you can use the split() and explode() functions.

  • str.split() splits the string into a list of substrings based on a delimiter (e.g., space, comma).
  • explode() transforms the list into separate rows, where each list item gets its own row.

Let's learn how to split and explode Pandas DataFrame entry to separate rows.

Splitting Strings into Columns

Let's say we have a DataFrame with a column of strings containing comma-separated names. We can split these strings into separate columns as follows:

Python
import pandas as pd

data = {'Names': ['Alice,Bob,Charlie', 'David,Eve', 'Frank']}
df = pd.DataFrame(data)

# Splitting the string column into separate columns
df[['Name1', 'Name2', 'Name3']] = df['Names'].str.split(',', expand=True)

print(df)

Output:

               Names  Name1 Name2    Name3
0 Alice,Bob,Charlie Alice Bob Charlie
1 David,Eve David Eve None
2 Frank Frank None None

Exploding the Split Columns into Separate Rows

Once you've split the strings, you may want to explode these split values into individual rows. Here's how you can use explode() to achieve this:

Python
# Exploding the 'Names' column
df_exploded = df['Names'].str.split(',').explode().reset_index(drop=True)

print(df_exploded)

Output:

0      Alice
1 Bob
2 Charlie
3 David
4 Eve
5 Frank
Name: Names, dtype: object

Splitting and Exploding in Pandas DataFrame is useful for cleaning survey data where multiple answers are stored in one field. This allows you to analyze individual responses more easily, such as counting how many times a specific hobby appears across all respondents.


Next Article

Similar Reads