This article discusses how to split a multi-value column into multiple rows in pandas using the str.split() and explode() methods.
Abstract
The article provides a step-by-step guide on how to convert a multi-value column in a pandas DataFrame into multiple rows. The first step involves using the str.split() method to split a column into a list. The second step involves using the explode() method to convert the list-type column into multiple rows with other columns duplicated. The article also includes examples and visuals to help illustrate the process.
Bullet points
The article discusses how to split a multi-value column into multiple rows in pandas.
The first step involves using the str.split() method to split a column into a list.
The second step involves using the explode() method to convert the list-type column into multiple rows with other columns duplicated.
The article includes examples and visuals to help illustrate the process.
The article also provides a link to another article on using string methods in pandas.
Pandas >> How to Convert a Multi-Value Column to Multiple Rows
In this article, we will talk about how to split one column in which contents are concatenated with a delimiter like a comma to a list. And convert the list column to multiple rows with other columns duplicated.
Use str.split() to split one column to a list
Convert the column with a list type value to multiple rows using explode() method
Preparing data
Use str.split() to split a column to a list
df["hobbies"] = df["hobbies"].str.split(",")
df
Convert the column with a list-type value to multiple rows
You can use explode() method of Pandas to convert a column with list-type values to multiple rows with other columns that are duplicated.
df= df.explode("hobbies")
df
Conclusion
We used str.split() to split one column into a list and used explode() method to convert it to multiple rows.
For more str methods, you can read the article below.