Built with Tutify.io

    AIStudyLabs

    Polars Series explode() – Explained by Examples

    Polars Series explode() – Explained by Examples

    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 –

    <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.

    <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,

    <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,

    <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,

    <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,

    <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

    <a id="h-reference"></a>

    Reference