Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the explode() function in Pandas?
2.1.
Syntax
2.2.
Python
2.3.
Parameters
2.4.
Return Value
3.
Examples of the explode() Function in Pandas
3.1.
Example 1
3.2.
Python
3.3.
Example 2
3.4.
Python
3.5.
Example 3
3.6.
Python
3.7.
Example 4
3.8.
Python
4.
Common Errors of the explode() Function
4.1.
ValueError: columns must have matching element counts
4.2.
AttributeError: 'Series' object has no attribute 'explode'
4.3.
TypeError: cannot explode non-iterable
5.
Frequently Asked Questions
5.1.
Does the explode() function in Pandas modify the original data?
5.2.
What is Series in Pandas?
5.3.
What is a ValueError in Python?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Pandas explode() Function

Author Abhinav Anand
0 upvote

Introduction

Pandas is an open-source Python library that provides data manipulation and analysis tools. It includes data structures such as Dataframes and Series for handling structured data. The library also offers various functions, such as explode()groupby()count(), etc., for reshaping or manipulating your dataset.

pandas explode function

The explode() function transforms a column containing iterable objects (like lists) into multiple rows. In this article, you will learn about the explode() function in Pandas with the help of some examples.

Let’s get started.

What is the explode() function in Pandas?

The explode() function is used to transform a column containing lists (or other iterable objects) into multiple rows, effectively exploding the values within those lists into separate rows. This is helpful when you want to perform operations on the individual elements of these lists.

Let’s look at the syntax, accepted parameters, and the return value of the explode() function.

Syntax

  • Python

Python

DataFrame.explode(column, ignore_index=False)
You can also try this code with Online Python Compiler
Run Code

Parameters

The explode() function accepts the following parameters:-

  1. column: This parameter can either be a single column name or a list of column names on which you want to perform the explode operation.
     
  2. ignore_index: This boolean parameter specifies if the index of the resulting dataframe will be reset to integers or not. The original index values are used if set to false(default).

Return Value

This function returns a dataframe 

In the next section, we will go through some examples of using the explode() function in Pandas.

Examples of the explode() Function in Pandas

Example 1

For the first example, we will create a dataframe containing two columns - ID and Items. We will apply the explode() function on the Items column because it contains arrays/lists below it.

  • Python

Python

import pandas as pd

data = {'ID': [1, 2, 3],

       'Items': [['apple', 'banana'], ['orange', 'grape'], ['pear']]}


df = pd.DataFrame(data)


print("Original DataFrame:")

print(df)


df_exploded = df.explode('Items')


print("\nExploded DataFrame:")

print(df_exploded)
You can also try this code with Online Python Compiler
Run Code

 

Output:

output

Example 2

Now we will use this function for nested lists.

  • Python

Python

import pandas as pd


data = {'ID': [1, 2, 3],

       'Items': [[[1, 2], [3, 4]], [[5, 6]], [[7, 8], [9, 10, 11]]]}



df = pd.DataFrame(data)


print("Original DataFrame:")

print(df)



df_exploded = df.explode('Items')


print("\nExploded DataFrame:")

print(df_exploded)
You can also try this code with Online Python Compiler
Run Code

 

Output:

output

In this example, the Items column contains nested lists, and the explode() function breaks down these nested lists into separate rows.

Example 3

Let’s look at the significance of the ignore_index parameter.

  • Python

Python

import pandas as pd


data = {'ID': [1, 2],

       'Items': [['apple', 'banana'], ['orange', 'grape', 'pear']]}


df = pd.DataFrame(data)


print("Original DataFrame:")

print(df)


df_exploded_newidx = df.explode('Items', ignore_index=True)

df_exploded = df.explode('Items', ignore_index=False)


print("\nExploded DataFrame (with reset index):")

print(df_exploded_newidx)



print("\nExploded DataFrame (without reset index):")

print(df_exploded)
You can also try this code with Online Python Compiler
Run Code

 

Output: 

output

Here you can see two dataframes, one where we used the reset index parameter and one where we didn’t use it.

Example 4

We will apply the explode() function on multiple columns simultaneously. The dataframe in this example has 3 columns - Order_ID, Products, and Quantities. Products and Quantities columns have iterable values.

  • Python

Python

import pandas as pd



data = {'Order_ID': [1, 2, 3],

       'Products': [['apple', 'banana'], ['orange', 'grape', 'pear'], ['carrot', 'lettuce']],

       'Quantities': [[5, 3], [2, 4, 6], [10, 8]]}


df = pd.DataFrame(data)


print("Original DataFrame:")

print(df)


# Explode 'Products' and 'Quantities' columns simultaneously

df_exploded = df.explode(['Products', 'Quantities'], ignore_index=True)


print("\nExploded DataFrame:")

print(df_exploded)
You can also try this code with Online Python Compiler
Run Code

 

Output:

output

While using the explode() function, we passed a list of column names and set the ignore_index parameter to true. Note that the number of elements in the lists in the same row is equal.

Now, let’s look at some common errors thrown by the explode() function with their solutions.

Common Errors of the explode() Function

The following are some common errors that the explode() function throws in Pandas:-

ValueError: columns must have matching element counts

This error occurs when you attempt to explode multiple columns simultaneously, but their iterable elements don't have matching lengths.

Solution: To explode multiple columns, ensure their iterable values have the same lengths. You can explode them separately and merge the resulting dataframes if they don't have equal lengths.

AttributeError: 'Series' object has no attribute 'explode'

This error occurs if you're trying to use the explode() function on a Series object instead of a dataframe.

Solution: Ensure you use the explode() function on a dataframe, not a Series. If you want to explode a Series, you should first convert it to a dataframe with a single column.

TypeError: cannot explode non-iterable

This error happens if you're trying to explode a column that doesn't contain iterable objects (e.g., trying to explode a column of scalar values).

Solution: Ensure the column you're trying to explode contains iterable objects (like lists or arrays).

Frequently Asked Questions

Does the explode() function in Pandas modify the original data?

No, the explode() function does not modify the original dataframe. Instead, it returns a new DataFrame with the specified column (or columns) exploded as separate rows.

What is Series in Pandas?

A series is a one-dimensional labeled data structure that can hold data types such as numeric, string, boolean, etc. Each element in a series has a corresponding label called an index which may or may not be a numeric value.

What is a ValueError in Python?

In Python, a ValueError is a built-in exception raised when a function receives an argument of the correct type but an inappropriate value. It indicates that the value provided does not meet the expected or required criteria.

Conclusion

In this article, you learned about the explode() function in Pandas with the help of various examples. We also discussed some common errors thrown by it along with their solutions.

Go through the following articles to learn more about Pandas:-

Happy Learning!

Live masterclass