transforming multiple columns using loop

cra*_*igm 1 python-polars

I am trying to transform groups of columns based on certain conditions. I am having trouble looping over groups of columns in my select statement. Simplified example (actual df has many more columns in avars and bvars):

df = pl.DataFrame(
    {'a': [0,1,2,4],
     'b': [1,0,3,5],
     'w': [4,7,5,8],
     'x': [10, 20, 25, 30],
     'y': [15,3,16,88],
     'z': [22,17,4,32]
     }
)
avars=['w','x']
bvars=['y','z']
Run Code Online (Sandbox Code Playgroud)

This works:

df.select(
    (pl.when(pl.col('a')>0)
    .then(pl.col(var)/pl.col('a')) 
    .otherwise(pl.col(var)) for var in avars),
)
Run Code Online (Sandbox Code Playgroud)

But I get an error message when I try

df.select(
    (pl.when(pl.col('a')>0)
    .then(pl.col(var)/pl.col('a')) 
    .otherwise(pl.col(var)) for var in avars),
    (pl.when(pl.col('b')>0)
    .then(pl.col(var)/pl.col('b')) 
    .otherwise(pl.col(var)) for var in bvars),
)
Run Code Online (Sandbox Code Playgroud)

Even this fails:

df.select(
    pl.col('a'),
    pl.col('b'),
    (pl.when(pl.col('a')>0)
    .then(pl.col(var)/pl.col('a')) 
    .otherwise(pl.col(var)) for var in avars),
)
Run Code Online (Sandbox Code Playgroud)

似乎在 for 循环中选择除列组之外的任何列都会导致错误消息。有人能指出我正确的方向吗?

小智 5

*你需要用as来解压理解select不会接受生成器。

\n
import polars as pl\n\ndf = pl.DataFrame({"a": [0, 3], "b": [3, 4], "c": [5, 6], "d": [7, 9]})\n\navars = ["c", "d"]\n\ndf = df.select(\n    pl.col("a"),\n    pl.col("b"),\n    *(\n        pl.when(pl.col("a") > 0).then(pl.col(var) / pl.col("a")).otherwise(pl.col(var))\n        for var in avars\n    ),\n)\n\nprint(df)\n
Run Code Online (Sandbox Code Playgroud)\n

结果:

\n
shape: (2, 4)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 a   \xe2\x94\x86 b   \xe2\x94\x86 c   \xe2\x94\x86 d   \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 i64 \xe2\x94\x86 i64 \xe2\x94\x86 f64 \xe2\x94\x86 f64 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 0   \xe2\x94\x86 3   \xe2\x94\x86 5.0 \xe2\x94\x86 7.0 \xe2\x94\x82\n\xe2\x94\x82 3   \xe2\x94\x86 4   \xe2\x94\x86 2.0 \xe2\x94\x86 3.0 \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xb4\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n
Run Code Online (Sandbox Code Playgroud)\n