The explode() method in Polars Series is used to flatten list-like elements within a Series. When a Series contains lists or arrays, this method will expand each element of those lists into separate rows, effectively turning nested data into a flat structure. This method is especially useful for handling complex or grouped data and functions similarly to Pandas’ explode() method.
In this article, I will explain the Polars series explode() method by using its syntax, parameters, and usage, and how to returns a new Series by expanding each list (or array) element from the original Series into individual rows.
Key Points –
- The
explode()method flattens list-like elements in a Polars Series into individual rows. - The
explode()method transforms lists within a Series into individual rows, effectively "flattening" the list structure. - Each element within the lists is retained as an individual value after the explosion.
- Empty lists are ignored by the
explode()method, resulting in no rows for those entries. - The
explode()method only flattens the outermost level of the lists. Inner lists remain intact as lists. - After using
explode(), the column type changes from a list type to a basic scalar type (e.g., integers, strings, etc.). - If there are multiple nested levels, calling
explode()multiple times can flatten the Series further. - The
explode()method is specifically designed for Polars Series containing lists and is not applicable to non-list data types.
<a id="h-polars-series-explode-introduction"></a>
Polars Series explode() Introduction
Let’s know the syntax of the series explode() method.
# Syntax of explode()
Series.explode() → Series
<a id="h-parameters-of-the-nbsp-series-nbsp-explode"></a>
Parameters of the Series explode()
It allows only one parameter.
None- The method does not take any parameters other thanself(the instance of the Series).
<a id="h-return-value"></a>
Return Value
This function returns a new Series with each element of the list (or array) in the original Series exploded into a separate row.
<a id="h-usage-of-polars-series-explode-method"></a>
Usage of Polars Series explode() Method
The explode() method is used to transform list-type elements in a polars Series into multiple rows, where each element of the list becomes its row. This is particularly useful when working with Series that contain nested lists, and you want to flatten them.
First, let’s create a Polars Series.
import polars as pl
# Creating a Series with list elements
ser = pl.Series("numbers", [[10, 20], [30, 40, 50], [60]])
print("Original Series:\n", ser)
Yields below output.
To flatten a Series of lists in Polars, you can use the explode() method. This will transform the lists in the Series, creating a new row for each individual element in the list. This function takes a Polars Series containing lists (or arrays) and expands each list element into its own row.
# Exploding the Series
ser2 = ser.explode()
print("Exploded Series:\n", ser2)
Here,
- Before Explosion: The Series
serhas lists as elements. - After Explosion: Each element of the lists becomes its own row in the resulting Series. As a result, the number of rows increases (from 3 to 6 in this case), and each list element is extracted as an individual row.
<a id="h-explode-with-strings-in-lists"></a>
Explode with Strings in Lists
You can also use the explode() method to flatten a Series that contains strings inside the lists. When the lists hold string values, explode() works the same way as with numeric values, converting each string in the list into a separate row in the resulting Series.
import polars as pl
# Creating a Series with lists containing strings
ser = pl.Series("technology", [["Spark", "Pandas"], ["Hadoop", "Polars"], ["NumPy"]])
# Exploding the Series
ser2 = ser.explode()
print("Exploded Series with Strings:\n", ser2)
# Output:
# Exploded Series with Strings:
# shape: (5,)
# Series: 'technology' [str]
#[
# "Spark"
# "Pandas"
# "Hadoop"
# "Polars"
# "NumPy"
#]
Here,
- The Series
sercontains lists of strings. - The
explode()method splits each list into individual rows, and each string becomes a separate row in the resulting Series.
<a id="h-explode-a-series-with-empty-lists"></a>
Explode a Series with Empty Lists
When you explode a Series containing empty lists in Polars, the explode() method will exclude those empty lists, resulting in no rows for them. The non-empty lists will be expanded into individual rows as usual.
import polars as pl
# Creating a Series with lists, including an empty list
ser = pl.Series("data", [[1, 2], [], [3, 4, 5]])
# Exploding the Series
ser2 = ser.explode()
print("Exploded Series with Empty Lists:\n", ser2)
# Output:
# Exploded Series with Empty Lists:
# shape: (6,)
# Series: 'data' [i64]
# [
# 1
# 2
# 3
# 4
# 5
#]
Here,
- The Series
sercontains lists, one of which is empty ([]). - The non-empty lists (
[1, 2]and[3, 4, 5]) are exploded into individual rows. - The empty list (
[]) is excluded from the result, so no rows are created for it.
<a id="h-explode-a-series-with-nested-lists"></a>
Explode a Series with Nested Lists
When you explode a Series with nested lists (lists inside lists), the explode() method only flattens the outermost level. This means the nested structure will still remain as it is, with the inner lists staying intact.
import polars as pl
# Creating a Series with nested lists
ser = pl.Series("data", [[[1, 2], [3, 4]], [[5, 6]], [7]])
# Exploding the Series
ser2 = ser.explode()
print("Exploded Series with Nested Lists:\n", ser2)
# Output:
# Exploded Series with Nested Lists:
# shape: (4,)
# Series: 'data' [list[i64]]
# [
# [1, 2]
# [3, 4]
# [5, 6]
# [7]
# ]
Here,
- The Series
sercontains lists, some of which have nested lists (e.g.,[ [1, 2], [3, 4] ]). - The
explode()method flattens the outermost level of the list, creating new rows for the inner lists. - The nested structure inside the inner lists remains unchanged, meaning each inner list is kept intact as a list.
<a id="h-exploding-series-with-different-lengths-of-lists"></a>
Exploding Series with Different Lengths of Lists
When you explode a Polars Series with lists of varying lengths, the explode() method flattens all list elements across the Series, no matter how many items each list contains. Every individual value becomes a separate row in the resulting Series.
import polars as pl
# Series with lists of different lengths
ser = pl.Series("mixed_lengths", [[10], [20, 30], [40, 50, 60], []])
# Exploding the Series
ser2 = ser.explode()
print("Exploded Series with Different Lengths:\n", ser2)
# Output:
# Exploded Series with Different Lengths:
# shape: (7,)
# Series: 'mixed_lengths' [i64]
# [
# 10
# 20
# 30
# 40
# 50
# 60
#]
<a id="h-conclusion"></a>
Conclusion
In conclusion, the explode() method in Polars Series is a powerful tool for flattening list-like data. Whether your Series contains numbers, strings, or varying list lengths, explode() efficiently transforms each list element into its own row, making your data easier to work with and analyze.
Happy Learning!!
<a id="h-related-articles"></a>
Related Articles
- Polars Series max() Function
- Convert Polars Series to List
- Polars Series tail() Usage & Examples
- Polars Series cast() – Usage & Examples
- Polars Series head() Method with Examples
- Polars Series filter() Method with Examples
- Polars Series min() – Explained by Examples
- Polars Series list.join() Function with Examples
- How to Replace Certain Values in a Polars Series?
- How to Convert Polars Series to Pandas Series in Python?
<a id="h-reference"></a>
