# Idea Use `explode` and `unnest` to get lists and dictionaries out of dataframe cells. ```python import polars as pl df = pl.DataFrame( { "id": [1, 2], "data": [ [{"name": "Alice", "age": 30}, {"name": "Alice", "age": 30}], [{"name": "Bob", "age": 25}], ], } ) print(df) ┌─────┬──────────────────────────────┐ │ id ┆ data │ │ --- ┆ --- │ │ i64 ┆ list[struct[2]] │ ╞═════╪══════════════════════════════╡ │ 1 ┆ [{"Alice",30}, {"Alice",30}] │ │ 2 ┆ [{"Bob",25}] │ └─────┴──────────────────────────────┘ # convert dictionary column to Struct (if not automatically done) df = df.with_column(pl.col("data").cast(pl.Struct)) # explode dictionaries in lists df.explode("data") ┌─────┬──────────────┐ │ id ┆ data │ │ --- ┆ --- │ │ i64 ┆ struct[2] │ ╞═════╪══════════════╡ │ 1 ┆ {"Alice",30} │ │ 1 ┆ {"Alice",30} │ │ 2 ┆ {"Bob",25} │ └─────┴──────────────┘ # unnest them df.explode("data").unnest("data") ┌─────┬───────┬─────┐ │ id ┆ name ┆ age │ │ --- ┆ --- ┆ --- │ │ i64 ┆ str ┆ i64 │ ╞═════╪═══════╪═════╡ │ 1 ┆ Alice ┆ 30 │ │ 1 ┆ Alice ┆ 30 │ │ 2 ┆ Bob ┆ 25 │ └─────┴───────┴─────┘ ``` # References