How to replace a column with a series in Polars

The replace_column() function is a method in the Polars’s DataFrame that replaces an entire column at a specified index with a new Series. This operation is performed in place, meaning it modifies the original DataFrame directly.

Syntax

The syntax of the replace_column() function is given below:

DataFrame.replace_column(index: int , column: Series)

Parameters

  • index: It specifies the index of the column that will be replaced. It refers to the position of the column within the DataFrame.

  • column: It represents the Series that will replace the existing column at the specified index.

Code

To utilize the functionality of replace_column(), we’ll create a DataFrame containing three columns and a new series. Let’s explore how we can replace the values of the column at index 0 with the new series in the provided code example.

import polars as pl
df = pl.DataFrame({
"A": [1, 2, 3],
"B": [4, 5, 6],
"C": [7, 8, 9]
})
# Create a new Series
new_series = pl.Series("D", [10, 20, 30])
# Replace the column at index 0 ("A") with the new Series
df.replace_column(0, new_series)
print(df)

Explanation

  • Lines 3–7: We create a DataFrame named df with three columns A, B, and C. The DataFrame is initialized with a dictionary where keys are column names, and values are lists representing column values.

  • Line 10: We create a new Series named new_series with a column name (D) and values ([10, 20, 30]).

  • Line 13: The replace_column method is called on the DataFrame df to replace the values in the column at index 0 (A) with the values from the new_series Series. This operation is performed in place.

  • Line 14: We print the df DataFrame after the replacement operation. The output will show the modified DataFrame with the new values in the specified column.

Wrap up

The replace_column method offers a powerful, in-place solution for updating entire columns at specified indexes in a DataFrame. Together, these functions empower data professionals to efficiently address data quality issues, standardize information, and seamlessly manage and transform tabular data.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved