我尝试四处搜索,但很多答案似乎都很旧,并且对于当前的极地版本不再有效。如何将 python 函数的返回结果应用于极坐标数据帧的每一行? 我想将整行传递给函数而不是传递特定列。
import polars as pl
def test2(auth, row):
c = row["Group"]
d = row["Val"]
return "{}-{}-{}".format(c, str(d), auth)
df = pl.DataFrame({
'Group': ['A', 'B', 'C', 'D', 'E'],
'Val': [1001, 1002, 1003, 1004, 1005]
})
auth_token = "xxxxxxxxx"
df = df.with_columns(
pl.struct(pl.all())
.map_batches(lambda x: test2(auth_token, x))
.alias("response")
)
print(df)
上面的代码导致了这个错误。我不明白这个消息。我应该在哪里设置 strict=False 以及为什么这是必要的?
Traceback (most recent call last):
File "c:\Scripting\Python\Development\Test.py", line 29, in <module>
df = df.with_columns(
File "c:\Scripting\Python\Development\venv\lib\site-packages\polars\dataframe\frame.py", line 8763, in with_columns
return self.lazy().with_columns(*exprs, **named_exprs).collect(_eager=True)
File "c:\Scripting\Python\Development\venv\lib\site-packages\polars\lazyframe\frame.py", line 1942, in collect
return wrap_df(ldf.collect(callback))
polars.exceptions.ComputeError: TypeError: unexpected value while building Series of type Int64; found value of type String: "C"
Hint: Try setting `strict=False` to allow passing data with mixed types.
我知道我可以指定特定列(例如下面的代码)来执行此操作,但我想传递整行,然后选择要在函数内使用哪些列。任何帮助,将不胜感激。谢谢。
df = df.with_columns(
(
pl.struct(["Group", "Val"]).map_batches(
lambda x: test(auth_token, x.struct.field("Group"), x.struct.field("Val"))
)
).alias("api_response")
)
map_elements()
来代替。
df = df.with_columns(
pl.struct(pl.all())
.map_elements(lambda x: test2(auth_token, x))
.alias("response")
)
┌───────┬──────┬──────────────────┐
│ Group ┆ Val ┆ response │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ str │
╞═══════╪══════╪══════════════════╡
│ A ┆ 1001 ┆ A-1001-xxxxxxxxx │
│ B ┆ 1002 ┆ B-1002-xxxxxxxxx │
│ C ┆ 1003 ┆ C-1003-xxxxxxxxx │
│ D ┆ 1004 ┆ D-1004-xxxxxxxxx │
│ E ┆ 1005 ┆ E-1005-xxxxxxxxx │
└───────┴──────┴──────────────────┘