假设我们有下面给定的数据框。现在,对于每一行,我需要创建字典并将其传递给 UDF 进行一些逻辑处理。有没有办法使用 Polars 或 pyspark 数据帧来实现此目的?
With Polars
, 您可以使用:
# Dict of lists
>>> df.transpose().to_dict(as_series=False)
{'column_0': [1.0, 100.0, 1000.0], 'column_1': [2.0, 200.0, None]}
# List of dicts
>>> df.to_dicts()
[{'Account number': 1, 'V1': 100, 'V2': 1000.0},
{'Account number': 2, 'V1': 200, 'V2': None}]
输入数据框:
>>> df
shape: (2, 3)
┌────────────────┬─────┬────────┐
│ Account number ┆ V1 ┆ V2 │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ f64 │
╞════════════════╪═════╪════════╡
│ 1 ┆ 100 ┆ 1000.0 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2 ┆ 200 ┆ null │
└────────────────┴─────┴────────┘
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)