É possível fazer algo assim no Polars? Por exemplo, você precisa de um when.then.otherwise separado para cada uma das 4 novas variáveis, ou pode usar struct para criar várias novas variáveis a partir de um when.then.otherwise?
Exemplo regular de Python:
if x=1 and y=3 and w=300*z and z<100:
tot = 300
work = 400
sie = 500
walk = 'into'
else:
tot = 350
work = 400*tot
sie = tot/1000
walk = 'outof'
Tentei fazer algo semelhante no Polars com struct (para criar novas variáveis a e b com base na variável Movie:
import polars as pl
ratings = pl.DataFrame(
{
"Movie": ["Cars", "IT", "ET", "Cars", "Up", "IT", "Cars", "ET", "Up", "Cars"],
"Theatre": ["NE", "ME", "IL", "ND", "NE", "SD", "NE", "IL", "IL", "NE"],
"Avg_Rating": [4.5, 4.4, 4.6, 4.3, 4.8, 4.7, 4.5, 4.9, 4.7, 4.6],
"Count": [30, 27, 26, 29, 31, 28, 28, 26, 33, 28],
}
)
x = ratings.with_columns(
pl.when(pl.col('Movie')=='Up').then(pl.struct(pl.lit(0),pl.lit(2))).otherwise(pl.struct(pl.lit(1),pl.lit(3))).struct.field(['a','b'])
)
print(x)
Obrigado!