Home > Back-end >  Manipulating data in Polars
Manipulating data in Polars

Time:12-02

A dumb question. How to manipulate columns in Polars?

Explicitly, I have a table with 3 columns : N , Survivors, Deaths

I want to replace Deaths by Deaths * N and Survivors by Survivors * N

the following code is not working

table["SURVIVORS"] = table["SURVIVORS"]*table["N"]

I have this error:

TypeError: 'DataFrame' object does not support 'Series' assignment by index. Use 'DataFrame.with_columns'

thank you

CodePudding user response:

Maybe something like this:

# Import the pandas library
import pandas as pd

# Load the table data into a DataFrame object
table = pd.read_csv("table.csv")

#Create a new DataFrame object with the modified columns .with_columns()
table = table.with_columns("SURVIVORS": table["SURVIVORS"]*table["N"], "DEATHS": table["DEATHS"]*table["N"])

CodePudding user response:

You can use polars.DataFrame.with_column to overwrite/replace the values of a column.

Return a new DataFrame with the column added or replaced.

Here is an example :

import polars as pl

table = pl.DataFrame({"N": [5, 2, 6],
                    "SURVIVORS": [1, 10, 3],
                    "Deaths": [0, 3, 2]})

table= table.with_column(pl.Series(name="SURVIVORS",
                                   values=table["SURVIVORS"]*table["N"])) 

# Output :

print(table)

shape: (3, 3)
┌─────┬───────────┬────────┐
│ N   ┆ SURVIVORS ┆ Deaths │
│ --- ┆ ---       ┆ ---    │
│ i64 ┆ i64       ┆ i64    │
╞═════╪═══════════╪════════╡
│ 5   ┆ 5         ┆ 0      │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2   ┆ 20        ┆ 3      │
├╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 6   ┆ 18        ┆ 2      │
└─────┴───────────┴────────┘
  • Related