Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create dictionary of each row in polars Dataframe

Lets assume we have below given dataframe. Now for each row I need to create dictionary and pass it to UDF for some logic processing.Is there a way to achieve this using either polars or pyspark dataframe ?

enter image description here

like image 972
pbh Avatar asked Oct 29 '25 19:10

pbh


1 Answers

With Polars, you can use:

# 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}]

Input dataframe:

>>> df
shape: (2, 3)
┌────────────────┬─────┬────────┐
│ Account number ┆ V1  ┆ V2     │
│ ---            ┆ --- ┆ ---    │
│ i64            ┆ i64 ┆ f64    │
╞════════════════╪═════╪════════╡
│ 1              ┆ 100 ┆ 1000.0 │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌┤
│ 2              ┆ 200 ┆ null   │
└────────────────┴─────┴────────┘
like image 193
Corralien Avatar answered Oct 31 '25 10:10

Corralien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!