我使用 Polars 库进行数据框操作。我有两个数据帧,我想使用根据条件从另一个数据帧获取的单个值来更新一个数据帧的列值。这是代码:
tmp = df[df['UnifiedInvoiceID'] == inv]
mask = (df_invoice_features['UnifiedInvoiceID'] == inv)
df_invoice_features[mask, 'UnifiedCustomerID'] = tmp[0, 'UnifiedCustomerID']
而且,这是错误:
PySeries.new_u64() missing 1 required positional argument: '_strict'
您认为为什么会返回这样的错误?
Polars 语法与 pandas 非常不同。在我看来,您正在尝试像在 pandas DataFrame 上那样修改值。
以下是如何设置列值的示例:
df = pl.DataFrame({
"a": [1, 2, 3, 4, 5],
"b": list("abcde")
})
df.with_columns(
pl.when(pl.col("a") > 3).then(10).otherwise(pl.col("a")).alias("new")
)
输出:
shape: (5, 3)
┌─────┬─────┬─────┐
│ a ┆ b ┆ new │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 │
╞═════╪═════╪═════╡
│ 1 ┆ a ┆ 1 │
│ 2 ┆ b ┆ 2 │
│ 3 ┆ c ┆ 3 │
│ 4 ┆ d ┆ 10 │
│ 5 ┆ e ┆ 10 │
└─────┴─────┴─────┘
如果你举一个小例子,我可以举一个更彻底的例子。我还建议阅读用户指南,尤其是表达指南: