Pra*_*pgb 8 python apply python-polars
我需要在数据框中创建一个新列来存储处理后的值。所以我使用了 Polars apply 函数对 dicom 进行一些处理,然后返回值。但此应用函数默认将整列视为极坐标系列,并且不会逐行处理。
df = df.with_columns(
[
pl.apply(
exprs=["Filename", "Dicom_Tag", "Dicom_Tag_Corrected", "Name"],
f=apply_corrections_polars,
).alias("dicom_tag_value_corrected"),
]
)
Run Code Online (Sandbox Code Playgroud)
rit*_*e46 13
作为状态文档pl.apply
,它不应该在select
上下文中使用。它只能在groupby
应用于不同组的操作中使用。
对于项目的自定义函数,您可以使用Struct
数据类型。
因为polars>=0.13.16
您可以应用Struct
数据类型。AStruct
可以由极坐标中的任何列组成。
df = pl.DataFrame({"ham": [2, 2, 3], \n "spam": [11, 22, 33], \n "foo": [3, 2, 1]})\n\ndef my_complicated_function(struct: dict) -> int:\n """\n A function that can not utilize polars expressions.\n This should be avoided.\n """\n\n # do work\n return struct["ham"] + struct["spam"] + struct["foo"]\n\ndf.select([\n pl.struct(["ham", "spam", "foo"]).apply(my_complicated_function)\n])\n\n
Run Code Online (Sandbox Code Playgroud)\nshape: (3, 1)\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 ham \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x82\n\xe2\x94\x82 i64 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 16 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 26 \xe2\x94\x82\n\xe2\x94\x9c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x95\x8c\xe2\x94\xa4\n\xe2\x94\x82 37 \xe2\x94\x82\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
10269 次 |
最近记录: |