from_flat

from_flat#

classmethod NestedFrame.from_flat(df, base_columns, nested_columns=None, on: str | None = None, name='nested')[source]#

Creates a NestedFrame with base and nested columns from a flat dataframe.

Parameters:
  • df (pd.DataFrame or NestedFrame) – A flat dataframe.

  • base_columns (list-like) – The columns that should be used as base (flat) columns in the output dataframe.

  • nested_columns (list-like, or None) – The columns that should be packed into a nested column. All columns in the list will attempt to be packed into a single nested column with the name provided in nested_name. If None, is defined as all columns not in base_columns.

  • on (str or None) – The name of a column to use as the new index. Typically, the index should have a unique value per row for base columns, and should repeat for nested columns. For example, a dataframe with two columns; a=[1,1,1,2,2,2] and b=[5,10,15,20,25,30] would want an index like [0,0,0,1,1,1] if a is chosen as a base column. If not provided the current index will be used.

  • name – The name of the output column the nested_columns are packed into.

Returns:

A NestedFrame with the specified nesting structure.

Return type:

NestedFrame

Examples

>>> import nested_pandas as npd
>>> nf = npd.NestedFrame({"a":[1,1,1,2,2], "b":[2,2,2,4,4],
...                   "c":[1,2,3,4,5], "d":[2,4,6,8,10]},
...                   index=[0,0,0,1,1])
>>> npd.NestedFrame.from_flat(nf, base_columns=["a","b"])
   a  b                      nested
0  1  2  [{c: 1, d: 2}; …] (3 rows)
1  2  4  [{c: 4, d: 8}; …] (2 rows)