I have a struct column and want to access fields based on another column
import polars as pl
data = {'my_struct': [{'field1': 1, 'field2': 'A'}, {'field1': 2, 'field2': 'B'}],
'field_name': ['field1', 'field2']}
df = pl.DataFrame(data)
shape: (2, 2)
┌───────────┬────────────┐
│ my_struct ┆ field_name │
│ --- ┆ --- │
│ struct[2] ┆ str │
╞═══════════╪════════════╡
│ {1,"A"} ┆ field1 │
│ {2,"B"} ┆ field2 │
└───────────┴────────────┘
I am looking for something like
df.select( pl.col("my_struct").struct.field(pl.col("field_name")) )
But I get an error:
# TypeError: argument 'name': 'Expr' object cannot be converted to 'PyString'
But how can I access the value of pl.col("field_name")?
I don't think it's currently possible to fetch a field using expressions.
Perhaps it's worth asking as a feature request.
It can be done like so:
df.with_columns(
field_value = pl.coalesce(
pl.when(pl.col('field_name') == field)
.then(pl.col('my_struct').struct[field])
for field in df['my_struct'].struct.fields
)
)
shape: (2, 3)
┌───────────┬────────────┬─────────────┐
│ my_struct ┆ field_name ┆ field_value │
│ --- ┆ --- ┆ --- │
│ struct[2] ┆ str ┆ str │
╞═══════════╪════════════╪═════════════╡
│ {1,"A"} ┆ field1 ┆ 1 │
│ {2,"B"} ┆ field2 ┆ B │
└───────────┴────────────┴─────────────┘
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With