0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Polars struct

Posted at

struct

df = pl.DataFrame(
    {"person": [{"name": "Alice", "age": 32}, {"name": "Bob", "age": 41}]}
)
shape: (2, 1)
┌──────────────┐
│ person       │
│ ---          │
│ struct[2]    │
╞══════════════╡
│ {"Alice",32} │
│ {"Bob",41}   │
└──────────────┘

structの値を個別に取得する

structのfield nameを指定して取得すれば良い。

df.select([
    pl.col("person").struct.field("name"), 
    pl.col("person").struct.field("age"), 
])
shape: (2, 2)
┌───────┬─────┐
│ name  ┆ age │
│ ---   ┆ --- │
│ str   ┆ i64 │
╞═══════╪═════╡
│ Alice ┆ 32  │
│ Bob   ┆ 41  │
└───────┴─────┘

field nameが分からない場合はSeriesにしてからstruct.fieldsにアクセスすればfield nameが表示される。

df["person"].struct.fields
['name', 'age']
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?