Built with Tutify.io

    AIStudyLabs

    Polars DataFrame tail() – Usage & Examples

    Polars DataFrame tail() – Usage & Examples

    In Polars, the tail() function is used to retrieve the last n rows of a DataFrame or Series. It's similar to Pandas' tail() function. By default, it returns the last 5 rows, but you can specify the number of rows you want to retrieve by passing an integer as an argument.

    In this article, I will explain the Polars DataFrame tail() function by using its syntax, parameters, usage, and how to return a new Polars DataFrame that contains the last n rows from the original DataFrame.

    Key Points –

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

    Polars DataFrame tail() Introduction

    Let’s know the syntax of the tail() method.

    
    # Syntax of tail()
    DataFrame.tail(n: int = 5) -> DataFrame
    

    <a id="h-parameters-of-the-nbsp-dataframe-tail"></a>

    Parameters of the DataFrame tail()

    It allows only one parameter.

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

    Return Value

    This function returns a new DataFrame that contains the last n rows from the original DataFrame.

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

    Usage of Polars DataFrame tail() Method

    The tail() method returns the last n rows of a Polars DataFrame, similar to pandas. It's particularly useful when you need to inspect the bottom part of your dataset, especially when working with large datasets where looking at the top rows alone may not be sufficient.

    First, let’s create a Polars DataFrame.

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

    Yields below output.

    To return the last 5 rows of the DataFrame using the tail() method in Polars (which is the default behavior), you can simply call the tail() method without passing any argument.

    
    # Get the last 5 rows (default)
    result = df.tail()
    print("Last 5 rows of the DataFrame (default usage):\n", result)
    

    In the above example, the tail() method returns the last 5 rows by default, and you can see the result based on the df DataFrame.

    <a id="h-get-the-last-3-rows-using-polars-dataframe-tail"></a>

    Get the Last 3 Rows Using Polars DataFrame tail()

    To specify the number of rows you want to return, you simply pass the desired number of rows as an argument to the tail() method.

    
    # Get the last 3 rows
    Result = df.tail(3)
    print("Last 3 rows:\n", Result)
    
    # Output:
    # Last 3 rows:
    # shape: (3, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fee   ┆ Discount ┆ Duration │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ i64      ┆ str      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Pandas  ┆ 26000 ┆ 2500     ┆ 60days   │
    │ PySpark ┆ 30000 ┆ 2000     ┆ 55days   │
    │ Java    ┆ 35000 ┆ 2200     ┆ 45days   │
    └─────────┴───────┴──────────┴──────────┘
    

    In the above example, df.tail(3) returns the last 3 rows of the df DataFrame. You can change the number inside tail() to any other number to retrieve the corresponding number of rows from the end of the DataFrame.

    <a id="h-get-the-last-1-row-using-polars-dataframe-tail"></a>

    Get the Last 1 Row Using Polars DataFrame tail()

    To get the last 1 row of a DataFrame in Polars, you can simply use the tail() method with the argument 1. This will return only the last row of the DataFrame.

    
    # Output:
    # Last 1 row:
     shape: (1, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fee   ┆ Discount ┆ Duration │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ i64      ┆ str      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Java    ┆ 35000 ┆ 2200     ┆ 45days   │
    └─────────┴───────┴──────────┴──────────┘
    

    In the above example, the tail(1) method returns the last row of the DataFrame. By specifying 1 as the argument, we ensure that only the last row is returned.

    <a id="h-tail-on-filtered-dataframe"></a>

    Tail on Filtered DataFrame

    To apply the tail() method on a filtered DataFrame in Polars, you first filter the DataFrame based on certain conditions and then use tail() to get the last n rows of the filtered result.

    
    # Filter DataFrame where Fee > 25000
    filtered_df = df.filter(pl.col('Fee') > 25000)
    
    # Get the last 3 rows of the filtered DataFrame
    Result = filtered_df.tail(3)
    print("Last 3 rows after filtering:\n", Result)
    
    # Output:
    # Last 3 rows after filtering:
    # shape: (3, 4)
    ┌─────────┬───────┬──────────┬──────────┐
    │ Courses ┆ Fee   ┆ Discount ┆ Duration │
    │ ---     ┆ ---   ┆ ---      ┆ ---      │
    │ str     ┆ i64   ┆ i64      ┆ str      │
    ╞═════════╪═══════╪══════════╪══════════╡
    │ Pandas  ┆ 26000 ┆ 2500     ┆ 60days   │
    │ PySpark ┆ 30000 ┆ 2000     ┆ 55days   │
    │ Java    ┆ 35000 ┆ 2200     ┆ 45days   │
    └─────────┴───────┴──────────┴──────────┘
    

    In the above example, we filter the rows where the Fee column is greater than 25000. Then, we use the tail(3) method to return the last 3 rows of the filtered DataFrame.

    <a id="h-tail-with-single-column-dataframe"></a>

    Tail with Single Column DataFrame

    If you have a single-column DataFrame and want to use the tail() method, the approach is similar to working with a multi-column DataFrame. You simply apply the tail() method to the single-column DataFrame to retrieve the last n rows.

    
    import polars as pl
    
    # Define a single-column data
    single_column_data = {
        'Courses': ["Spark", "PySpark", "Hadoop", "Python", "Pandas", "PySpark", "Java"]
    }
    
    # Create a DataFrame with a single column
    df = pl.DataFrame(single_column_data)
    
    # Get the last 3 rows of the single-column DataFrame
    Result = df.tail(3)
    print("Last 3 rows of the single-column DataFrame:\n", Result)
    
    # Output:
    # Last 3 rows of the single-column DataFrame:
    # shape: (3, 1)
    ┌─────────┐
    │ Courses │
    │ ---     │
    │ str     │
    ╞═════════╡
    │ Pandas  │
    │ PySpark │
    │ Java    │
    └─────────┘
    

    In the above example, we create a DataFrame df containing just one column, "Courses", and then use tail(3) to fetch the last 3 rows of this single-column DataFrame.

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

    Conclusion

    In this article, I have explained the Polars DataFrame tail() method, including its syntax, parameters, usage, and how it returns a new DataFrame containing the last n rows of the original DataFrame. If no argument is provided, it defaults to returning the last 5 rows with an example.

    Happy Learning!!

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

    Related Articles

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

    Reference