Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14185

Replacing values from a given column with the modified values from a different column in python-polars

$
0
0

I need to update the values of a column (X) in correspondance with certain values ('v') of another column Y with values from another column (Z) times some numbers and then cast the obtained column to int:

In pandas the code is as follow:

df.loc[df["Y"] == "v", "X"] = (        df.loc[df["Y"] == "v", "Z"] * 1.341 * 15).astype(int)

In polars i'm able to select values (e.g. nulls) from a column and replace them with values from another column:

df=df.with_columns(    pl.when(df['col'].is_null())        .then(df['other_col'])            .otherwise(df['col'])                .alias('alias_col'))

but i'm stuck at nesting the call to the (modified) third column.

Any help would be very much appreciated!


Viewing all articles
Browse latest Browse all 14185

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>