By using Pandas DataFrame explode()
function you can transform or modify each element of a list-like to a row (single or multiple columns), replicating the index values. This function converts the list elements to a row while replacing the index values and returning the DataFrame exploded list.
In this article, I will explain how to explode single and multiple columns in pandas DataFrame by using explode()
function. If the array-like is empty, a missing value NaN will be placed for that row.
1. Quick Examples of Explode Multiple Columns
If you are in a hurry, below are some quick examples of how to explode a single column and multiple columns in Pandas DataFrame.
# Quick examples of explode multiple columns
# Example 1: use DataFrame.explode() function
df2 = df.explode('A')
# Example 2: Explode single column
# Using DataFrame.explode() function
df2 = df.explode(list('A'))
# Example 3: Explode single column & ignore_index
df2 = df.explode('A',ignore_index=True)
# Example 4: Explode multiple columns
# Using DataFrame.explode() function
df2 = df.explode(list('AC'))
# Example 5: Use DataFrame.explode() function
# Ignore_index
df2 = df.explode(list('AC'), ignore_index=True)
2. Syntax of DataFrame.explode()
Following is the syntax of DataFrame.explode() function.
# Syntax of DataFrame.explode()
DataFrame.explode(column, ignore_index=False)
2.1 Parameters of explode()
Following are the parameters of the pandas DataFrme explode() function.
column
– IndexLabel: column to explode. For multiple columns, specify a non-empty list with each element being str or tuple.ignore_index
– boolean, default False. If True, the resulting index will be labeled 0, 1, …, n – 1.
2.2 Return value of explode()
It returns exploded lists to rows of the subset columns; the index will be duplicated for these rows.
Create DataFrame
Now, Let’s create Pandas DataFrame using data from a Python dictionary, where the columns are A
, B
, and C
.
# Create pandas DataFrame
import pandas as pd
import numpy as np
technologies = (
{'A': [["Spark","PySpark","Python"], 'Course', [], ["Java","pandas"]],
'B': [25000,15000,30000,40000],
'C': [['30days','40days','35days'], np.nan, [], ['40days','55days']]})
df = pd.DataFrame(technologies)
print(df)
Yields below output.
3. Explode Single Column Using DataFrame.explode()
You can use DataFrame.explode()
function to convert each element of the specified single column "A"
into a row (each value in a list becomes a row). This turns every element of the list A
into a row. If the array-like is empty, the empty lists will be expanded into a NaN
value.
# Use DataFrame.explode() function
df2 = df.explode('A')
print(df2)
# Explode single column using list()
df2 = df.explode(list('A'))
print(df2)
The above examples yield the below output.
4. Explode Multiple Columns Using DataFrame.explode()
Alternatively, You can also use explode()
function to explode multiple columns together using the explode function in pandas DataFrame. It returns DataFrame exploded lists to rows of the subset columns; the index will be duplicated for these rows. The following example explods multiple columns A
and C
.
# Explode multiple columns
# Using DataFrame.explode() function
df2 = df.explode(list('AC'))
print(df2)
Yields below output.
5. Use DataFrame.explode() Function & ignore_index
You can pass ignore_index=True
to DataFrame.explode()
function to reset the index on DataFrame.
# Use DataFrame.explode() Function & ignore_index
df2 = df.explode(list('AC'), ignore_index=True)
print(df2)
Yields below output.
# Output:
A B C
0 Spark 25000 30days
1 PySpark 25000 40days
2 Python 25000 35days
3 Course 25000 NaN
4 NaN 25000 NaN
5 Java 25000 40days
6 pandas 25000 55days
6. Complete Example of Explode Multiple Columns
import pandas as pd
import numpy as np
technologies = ({'A': [["Spark","PySpark","Python"], 'Course', [], ["Java","pandas"]],
'B': 25000,
'C': [['30days','40days','35days'], np.nan, [], ['40days','55days']]})
df = pd.DataFrame(technologies)
print(df)
# Use DataFrame.explode() function
df2 = df.explode('A')
print(df2)
# Explode single column
# Using DataFrame.explode()function
df2 = df.explode(list('A'))
print(df2)
# Explode single column & ignore_index
df2 = df.explode('A',ignore_index=True)
print(df2)
# Explode multiple columns
# Using DataFrame.explode() function
df2 = df.explode(list('AC'))
print(df2)
# Use DataFrame.explode() Function & ignore_index
df2 = df.explode(list('AC'), ignore_index=True)
print(df2)
Frequently Asked Questions on Pandas Explode Multiple Columns
The explode()
function in Pandas is designed to be applied to a single column at a time. To explode multiple columns, you can use the apply
function in combination with explode
. Each column can be exploded individually within the apply
function.
As of my last knowledge update, the explode()
function itself does not have an ignore_index
parameter. To achieve a similar result, you can use the reset_index()
method after exploding the DataFrame
If you want to explode multiple columns and reset the index in a single step, you can use the apply
function along with a custom function that explodes the specified columns and resets the index.
It’s essential to check the official Pandas documentation for the version you are using to get the most accurate and up-to-date information. New features or changes may have been introduced in newer versions.
When exploding columns with lists of varying lengths, Pandas may introduce NaN values for missing elements. If you want to handle this efficiently, consider using the explode
function in combination with apply
and reset_index
.
Conclusion
In this article, I have explained how to explode single and multiple columns (list to rows) of Pandas DataFrame by using DataFrame.explode()
function with examples.
Happy Learning !!
Related Articles
- Pandas Count Rows with Condition
- Pandas DataFrame insert() Function
- How to Get Pandas Columns Count
- Pandas Add Column with Default Value
- Pandas DataFrame count() Function
- Pandas split column into two columns
- Split pandas DataFrame
- Pandas convert column to string-type
- Pandas Count Unique Values in Column
- Split Pandas DataFrame by column value
- Convert Pandas Series of Lists to One Series
- Pandas Add Multiple Columns to DataFrame
- Pandas Drop First Column From DataFrame
- Pandas Drop Last Column From DataFrame
- How to Convert Pandas to PySpark DataFrame
- How to Count Duplicates in Pandas DataFrame
- Split the column of DataFrame into two columns
- Pandas Check Column Contains a Value in DataFrame
- Pandas Extract Column Value Based on Another Column