Built with Tutify.io

    AIStudyLabs

    Polars Series rename() – by Examples

    Polars Series rename() – by Examples

    In Polars, the rename() method is used to change the name (or label) of a Series. A Series is a one-dimensional data array in Polars, typically representing a column within a DataFrame. This method is helpful when you need to change the label of a Series, especially after performing transformations or extracting it from a DataFrame.

    In this article, I will explain the rename() function in Polars Series, including its syntax, parameters, usage, and how it returns a new Series object with the updated name (label). While the underlying data and structure of the Series remain unchanged, the Series is now associated with the new name you assign.

    Key Points –

    <a id="h-polars-series-rename-introduction"></a>

    Polars Series rename() Introduction

    Let’s know the syntax of the series rename() method.

    
    # Syntax of rename()
    Series.rename(name: str) → Series
    

    <a id="h-parameters-of-the-series-rename"></a>

    Parameters of the Series rename()

    It allows only one parameter.

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

    Return Value

    This function returns a new Series with the updated name.

    <a id="h-usage-of-polars-series-rename-method"></a>

    Usage of Polars Series rename() Method

    The rename() method is used to change the name (or label) of a Polars Series. It doesn't affect the data within the Series, only the name associated with it.

    First, let’s create a Polars Series.

    
    import polars as pl
    
    # Sample Series
    ser = pl.Series( 'Courses', ["Spark","Hadoop","Python","Pandas"])
    print("Original Series:\n", ser)
    

    Yields below output.

    To rename a Polars Series, you can use the rename() method to assign a new name to the Series. For instance, the Series name "Courses" is updated to "New_Courses", while the data within the Series remains unaffected.

    
    # Renaming the Series
    ser_renamed = ser.rename('New_Courses')
    print("Renamed Series:\n", ser_renamed)
    

    Here,

    <a id="h-renaming-a-series-with-numeric-data"></a>

    Renaming a Series with Numeric Data

    Renaming a Polars Series with numeric data follows the same approach as renaming a Series with any other data type. Here's how to rename a Series that contains numeric values.

    
    import polars as pl
    
    # Create a Series with numeric data
    ser = pl.Series('Scores', [95, 88, 78, 92])
    
    # Rename the Series to a more descriptive name
    ser_renamed = ser.rename("Student_Scores")
    print("Renamed Series:\n", ser_renamed)
    
    # Output:
    # Renamed Series:
    # shape: (4,)
    # Series: 'Student_Scores' [i64]
    # [
    #	95
    #	88
    #	78
    #	92
    # ]
    

    Here,

    <a id="h-renaming-a-series-with-boolean-data"></a>

    Renaming a Series with Boolean Data

    The rename() function is used to update the name of a Polars Series, and it can be applied to any data type, including boolean values. Below is a detailed explanation of how to use the rename() method on a Series that contains boolean data.

    
    import polars as pl
    
    # Create a Series with Boolean data
    ser = pl.Series('Passed', [True, False, True, True])
    
    # Rename the Series to a more descriptive name
    ser_renamed = ser.rename("Exam_Passed_Status")
    print("Renamed Series:\n", ser_renamed)
    
    # Output:
    # Renamed Series:
    # shape: (4,)
    # Series: 'Exam_Passed_Status' [bool]
    # [
    #	true
    #	false
    #	true
    #	true
    # ]
    

    Here,

    <a id="h-renaming-a-series-after-filtering"></a>

    Renaming a Series After Filtering

    To rename a Polars Series after applying a filter, you can first filter the data based on a condition and then use the rename() method to assign a new, more meaningful name to the resulting Series.

    
    import polars as pl
    
    # Create a Series with numeric data
    ser = pl.Series('Scores', [95, 88, 78, 92, 85])
    
    # Filter the Series: Keep only values greater than 80
    filtered_ser = ser.filter(ser > 80)
    
    # Rename the filtered Series
    ser_renamed = filtered_ser.rename("High_Scores")
    print("Renamed Filtered Series:\n", ser_renamed)
    
    # Output:
    # Renamed Filtered Series:
    # shape: (4,)
    # Series: 'High_Scores' [i64]
    # [
    #	95
    #	88
    #	92
    #	85
    # ]
    

    Here,

    <a id="h-renaming-after-transformation"></a>

    Renaming After Transformation

    Renaming a Polars Series after a transformation is a clear and effective way to reflect the updated purpose or context of the data. For instance, after performing operations like scaling, rounding, or value adjustments, giving the Series a new name helps indicate its transformed role.

    
    import polars as pl
    
    # Original Series
    ser = pl.Series("Scores", [70, 85, 90, 60])
    
    # Add 5 bonus points to each score
    ser_renamed = (ser + 5).rename("Adjusted_Scores")
    print("Renamed Transformed Series:\n", ser_renamed)
    
    # Output:
    # 
    # Renamed Transformed Series:
    # shape: (4,)
    # Series: 'Adjusted_Scores' [i64]
    # [
    #	75
    #	90
    #	95
    #	65
    # ]
    

    <a id="h-renaming-after-applying-a-10-increase"></a>

    Renaming After Applying a 10% Increase

    You can rename a Polars Series after applying a percentage increase, a common transformation when adjusting values like prices, scores, or quantities.

    
    import polars as pl
    
    # Original Series
    ser = pl.Series("Scores", [70, 85, 90, 60])
    
    # Apply a 10% increase and rename the result
    ser_renamed = (ser * 1.10).round(0).rename("Increased_By_10_Percent")
    print("Renamed Transformed Series:\n", ser_renamed)
    
    # Output:
    # Renamed Transformed Series:
    # shape: (4,)
    # Series: 'Increased_By_10_Percent' [f64]
    # [
    #	77.0
    #	94.0
    #	99.0
    #	66.0
    # ]
    

    Here,

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

    Conclusion

    In summary, the rename() method in Polars Series is a simple yet powerful way to update the label of a Series without modifying its underlying data. Whether you're cleaning up column names after transformations or making your data more readable, rename() helps keep your Series well-organized and descriptive.

    Happy Learning!!

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

    Related Articles

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

    References