我正在寻找一种有效的方法,用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错误:selfmaskother的形状不适合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)

非常感谢您的帮助!

推荐答案

您可以过滤这1列,将其id更改为2并丢弃不需要的列.

df.filter(distribution_id = 1).select(
   "year", "district_id", "^var_.+$", distribution_id = pl.lit(2, pl.Int64)
)
shape: (2, 5)
┌──────┬─────────────┬───────┬───────┬─────────────────┐
│ year ┆ district_id ┆ var_1 ┆ var_N ┆ distribution_id │
│ ---  ┆ ---         ┆ ---   ┆ ---   ┆ ---             │
│ i64  ┆ i64         ┆ f64   ┆ f64   ┆ i64             │
╞══════╪═════════════╪═══════╪═══════╪═════════════════╡
│ 2020 ┆ 1           ┆ 1.0   ┆ 1.0   ┆ 2               │
│ 2021 ┆ 2           ┆ 2.0   ┆ 2.0   ┆ 2               │
└──────┴─────────────┴───────┴───────┴─────────────────┘
  • (注意:"^var_.+$"通过regex Select 列,但如果愿意,也可以使用 Select 器.)

数据"对齐"后,您可以将其传递给.update()

df.update(
   df.filter(distribution_id = 1)
     .select("year", "district_id", "^var_.+$", distribution_id = pl.lit(2, pl.Int64)),
   on=["year", "district_id", "distribution_id"]
)
shape: (4, 6)
┌──────┬─────────────┬─────────────────┬───────┬───────┬───────────────┐
│ year ┆ district_id ┆ distribution_id ┆ var_1 ┆ var_N ┆ unrelated_var │
│ ---  ┆ ---         ┆ ---             ┆ ---   ┆ ---   ┆ ---           │
│ i64  ┆ i64         ┆ i64             ┆ f64   ┆ f64   ┆ f64           │
╞══════╪═════════════╪═════════════════╪═══════╪═══════╪═══════════════╡
│ 2020 ┆ 1           ┆ 1               ┆ 1.0   ┆ 1.0   ┆ 0.2           │
│ 2021 ┆ 2           ┆ 1               ┆ 2.0   ┆ 2.0   ┆ 0.5           │
│ 2020 ┆ 1           ┆ 2               ┆ 1.0   ┆ 1.0   ┆ 0.3           │
│ 2021 ┆ 2           ┆ 2               ┆ 2.0   ┆ 2.0   ┆ 0.7           │
└──────┴─────────────┴─────────────────┴───────┴───────┴───────────────┘

Python相关问答推荐

理解Python的二分库:澄清bisect_left的使用

Polars LazyFrame在收集后未返回指定的模式顺序

比较2 PD.数组的令人惊讶的结果

try 在树叶 map 上应用覆盖磁贴

Python中的嵌套Ruby哈希

沿着数组中的轴计算真实条目

删除字符串中第一次出现单词后的所有内容

将JSON对象转换为Dataframe

Python逻辑操作作为Pandas中的条件

如何禁用FastAPI应用程序的Swagger UI autodoc中的application/json?

如何创建引用列表并分配值的Systemrame列

人口全部乱序 - Python—Matplotlib—映射

比Pandas 更好的 Select

不允许 Select 北极滚动?

Python类型提示:对于一个可以迭代的变量,我应该使用什么?

裁剪数字.nd数组引发-ValueError:无法将空图像写入JPEG

Django在一个不是ForeignKey的字段上加入'

按条件计算将记录拆分成两条记录

如何将列表从a迭代到z-以抓取数据并将其转换为DataFrame?

如何使用Polars从AWS S3读取镶木地板文件