我正在寻找一种有效的方法,用Polars中同一打印机的其他部分复制/替换打印机的部分.
例如,在下面的最小示例中,Pramrame
pl.DataFrame({
"year": [2020,2021,2020,2021],
"district_id": [1,2,1,2],
"distribution_id": [1, 1, 2, 2],
"var_1": [1,2,0.1,0.3],
"var_N": [1,2,0.3,0.5],
"unrelated_var": [0.2,0.5,0.3,0.7],
})
我想将"Distribution_id"= 1的相应值替换为"Distribution_id"= 2的"var_1""var_N"的所有列值.
这是想要的结果:
pl.DataFrame({
"year": [2020,2021,2020,2021],
"district_id": [1,2,1,2],
"distribution_id": [1, 1, 2, 2],
"var_1": [1,2,1,2],
"var_N": [1,2,1,2],
"unrelated_var": [0.2,0.5,0.3,0.7],
})
我try 使用"when"表达,但失败了,结果是"polars.exceptions. Shape错误:self
、mask
和other
的形状不适合zip_with
操作"
df = df.with_columns([
pl.when(pl.col("distribution_id") == 2).then(df.filter(pl.col("distribution_id") == 1).otherwise(pl.col(col)).alias(col) for col in columns_to_copy
]
)
以下是我过go 对SQLAchemy所做的事情:
table_alias = table.alias("table_alias")
stmt = table.update().\
where(table.c.year == table_alias.c.year).\
where(table.c.d_id == table_alias.c.d_id).\
where(table_alias.c.distribution_id == 1).\
where(table.c.distribution_id == 2).\
values(var_1=table_alias.c.var_1,
var_n=table_alias.c.var_n)
非常感谢您的帮助!