Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

polars subset struct variable field name

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")?

like image 444
linus heinz Avatar asked Mar 06 '26 19:03

linus heinz


1 Answers

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           │
└───────────┴────────────┴─────────────┘
like image 81
jqurious Avatar answered Mar 09 '26 08:03

jqurious



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!