Built with Tutify.io

    AIStudyLabs

    Polars DataFrame slice() – Usage & Examples

    Polars DataFrame slice() – Usage & Examples

    In Polars, the DataFrame.slice() method is used to select a specific subset of rows from a DataFrame, similar to slicing a Python list or array. It takes an offset (the starting row index) and an optional length (how many rows to return), making it easy to extract a desired portion of the data.

    In this article, I will explain the Polars DataFrame slice() method, covering its syntax, parameters, and usage. This function creates a new DataFrame by selecting rows based on a specified offset and an optional length.

    Key Points –

    <a id="h-polars-dataframe-slice-introduction"></a>

    Polars DataFrame slice() Introduction

    Let’s know the syntax of the slice() function.

    
    # Syntax of DataFrame.slice()
    DataFrame.slice(offset: int, length: int | None = None) → DataFra
    

    <a id="h-parameters-of-the-polars-dataframe-slice"></a>

    Parameters of the Polars DataFrame slice()

    Following are the parameters of the slice() method.

    <a id="h-return-value"></a>

    Return Value

    This function returns a new DataFrame with only the sliced rows.

    <a id="h-usage-of-polars-dataframe-slice-method"></a>

    Usage of Polars DataFrame slice() Method

    The DataFrame.slice() method in Polars is used to select a specific range of rows from a DataFrame based on their row positions.

    To run some examples of the Polars DataFrame slice() function, let’s create a Polars DataFrame.

    
    import polars as pl
    
    technologies= {
        'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
        'Fees' :[22000,25000,23000,24000,26000],
        'Duration':['30days','50days','35days', '40days','55days'],
        'Discount':[1000,2300,1000,1200,2500]
              }
    df = pl.DataFrame(technologies)
    print("Original DataFrame:\n", df)
    

    Yields below output.

    To extract the first 3 rows from a Polars DataFrame using the slice() method. The method you choose may vary based on the programming language you’re using.

    
    # Using slice(0, 3)
    df2 = df.slice(0, 3)
    print("First 3 rows (using slice(0, 3)):\n", df2)
    

    Here,

    <a id="h-slice-rows-starting-from-index-2-length-2"></a>

    Slice Rows Starting from Index 2, Length 2

    To slice rows starting from index 2 with a length of 2 in Polars, you can use the slice() method.

    
    # Slice from index 2, take 2 rows
    df2 = df.slice(2, 2)
    print("Sliced DataFrame (index 2, length 2):\n", df2)
    

    Here,

    <a id="h-slice-the-last-2-rows"></a>

    Slice the Last 2 Rows

    To slice the last 2 rows of a Polars DataFrame, you can use negative indexing with the slice() method.

    
    # Slice the last 2 rows
    df2 = df.slice(-2, 2)
    print("Last 2 Rows:\n", df2)
    
    # Output:
    # Last 2 Rows:
     shape: (2, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Python  ┆ 24000 ┆ 40days   ┆ 1200     │
    │ Pandas  ┆ 26000 ┆ 55days   ┆ 2500     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-get-all-rows-from-index-2-to-the-end"></a>

    Get All Rows from Index 2 to the End

    To get all rows from index 2 to the end in a Polars DataFrame, you can use the slice() method.

    
    # Get all rows from index 2 to the end
    df2 = df.slice(2, df.height - 2)
    print("Rows from index 2 to end:\n", df2)
    
    # Output:
    # Rows from index 2 to end:
    # shape: (3, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Hadoop  ┆ 23000 ┆ 35days   ┆ 1000     │
    │ Python  ┆ 24000 ┆ 40days   ┆ 1200     │
    │ Pandas  ┆ 26000 ┆ 55days   ┆ 2500     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-slice-entire-dataframe-offset-0-no-length"></a>

    Slice Entire DataFrame (offset = 0, no length)

    When you use the slice() method with only the offset parameter (and skip the length), Polars returns all rows from that offset to the end of the DataFrame.

    
    # Slice entire DataFrame (offset = 0, no length)
    df2 = df.slice(0)
    print("Entire DataFrame:\n", df2)
    
    # Output:
    # Entire DataFrame:
    # shape: (5, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fees  ┆ Duration ┆ Discount │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ str      ┆ i64      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Spark   ┆ 22000 ┆ 30days   ┆ 1000     │
    │ PySpark ┆ 25000 ┆ 50days   ┆ 2300     │
    │ Hadoop  ┆ 23000 ┆ 35days   ┆ 1000     │
    │ Python  ┆ 24000 ┆ 40days   ┆ 1200     │
    │ Pandas  ┆ 26000 ┆ 55days   ┆ 2500     │
    └─────────┴───────┴──────────┴──────────┘
    

    Here,

    <a id="h-slice-with-offset-beyond-data-length-empty-dataframe"></a>

    Slice with Offset Beyond Data Length (Empty DataFrame)

    When you use df.slice() with an offset greater than or equal to the number of rows, Polars will return an empty DataFrame, but it will still have the same column names and data types.

    
    # Suppose df has 5 rows
    df2 = df.slice(10)
    print("Sliced DataFrame with offset beyond length:\n", df2)
    
    # Output:
    # Sliced DataFrame with offset beyond length:
    # shape: (0, 4)
    ┌─────────┬──────┬──────────┬──────────┐
    │ Courses ┆ Fees ┆ Duration ┆ Discount │
    │ ---     ┆ ---  ┆ ---      ┆ ---      │
    │ str     ┆ i64  ┆ str      ┆ i64      │
    ╞═════════╪══════╪══════════╪══════════╡
    └─────────┴──────┴──────────┴──────────┘
    

    Here,

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

    Conclusion

    In conclusion, the slice() function in Polars provides a flexible way to retrieve specific portions of a DataFrame using an offset and optional length. Whether you're slicing just a few rows or starting from a specific index to the end, slice() makes it easy to control the subset of data you work with.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

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

    References