TableFrame.unnest
methodView source ↗
def unnest(
columns: td_ColumnNameOrSelector | Collection[td_ColumnNameOrSelector],
*more_columns: td_ColumnNameOrSelector,
) -> TableFrame
Categories: projection
Expand one or more struct columns so that each field within the struct becomes
a separate column in the TableFrame.
The resulting TableFrame will place these new columns in the same position
as the original struct column, effectively replacing it. This makes it easier
to work directly with the inner fields as standard columns.
Parameters
parameter
columnsName of the struct column(s) to expand.
parameter
more_columnsAdditional struct columns to expand, provided as positional arguments.
Examples
>>> import tabsdata as td
>>>
>>> tf = td.TableFrame({
... "id": [1, 2],
... "info": [
... {"name": "Alice", "age": 30},
... {"name": "Bob", "age": None},
... ],
... "status": ["active", "inactive"],
... })
>>>
>>> tf
>>>
┌─────┬───────────────┬───────────┐
│ id ┆ info ┆ status │
│ --- ┆ --- ┆ --- │
│ i64 ┆ struct[2] ┆ str │
╞═════╪═══════════════╪═══════════╡
│ 1 ┆ {"Alice",30} ┆ active │
│ 2 ┆ {"Bob",null} ┆ inactive │
└─────┴───────────────┴───────────┘
>>>
>>> tf.unnest("info")
>>>
┌─────┬───────┬──────┬───────────┐
│ id ┆ name ┆ age ┆ status │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ i64 ┆ str │
╞═════╪═══════╪══════╪═══════════╡
│ 1 ┆ Alice ┆ 30 ┆ active │
│ 2 ┆ Bob ┆ null ┆ inactive │
└─────┴───────┴──────┴───────────┘