How to replace multiple column values on a condition when-then-otherwise in python polars?

Question:

I have a dataframe as follows:

df_ = pl.DataFrame({'r_num':['Yes','','Yes'],'pin':['Yes','',''],'fin':['','','']})

enter image description here

Here I would like to find an observation which has r_num is YES, pin is Yes and fin is EMPTY. on meeting this condition r_num and pin should be filled in as EMPTY.

df_.with_columns(pl.when((pl.col('r_num')=='Yes') &  (pl.col('pin')=='Yes') & (pl.col('fin') !='Yes')
                        ).then(pl.col('r_num')=='').otherwise(pl.col('r_num')))

enter image description here

Here why r_num is getting filled up with false?

Here is a syantax that i’m following. how to specify multiple columns in then clause.

expected output will be:

enter image description here

Here is a syntax to implement the same in pandas:

df_.loc[(df_['r_num']=='Yes') & (df_['pin']=='Yes') & (df_['fin']!='Yes'),['r_num','pin']]=''
``
Asked By: myamulla_ciencia

||

Answers:

You had a small bug in your code. Within the .then() method, you’d specified

pl.col('r_num')==''

This evaluates to False for the first row (where the when condition is True). The behaviour of .then() is to insert that value into the cell that is being evaluated.

What you want is .then(""), and to tell polars that you want to essentially overwrite r_num (and pin) with .alias("r_num") / .alias("pin").

Since the value in .otherwise depends on which column you want to overwrite, I think we have to perform the loop twice:

(
    df_
    .with_columns(
        [
            pl.when((pl.col("r_num") == "Yes") & (pl.col("pin") == "Yes") & (pl.col("fin") != "Yes"))
            .then("") # changed here
            .otherwise(pl.col("r_num"))
            .alias("r_num"), # added this
            pl.when((pl.col("r_num") == "Yes") & (pl.col("pin") == "Yes") & (pl.col("fin") != "Yes"))
            .then("") # changed here
            .otherwise(pl.col("pin"))
            .alias("pin"), # added this
            ]
        )
)

This results in your desired output:

shape: (3, 3)
┌───────┬─────┬─────┐
│ r_num ┆ pin ┆ fin │
│ ---   ┆ --- ┆ --- │
│ str   ┆ str ┆ str │
╞═══════╪═════╪═════╡
│       ┆     ┆     │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│       ┆     ┆     │
├╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ Yes   ┆     ┆     │
└───────┴─────┴─────┘
Answered By: TomNorway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.