from_lists#
- classmethod NestedFrame.from_lists(df, base_columns=None, list_columns=None, name='nested')[source]#
Creates a NestedFrame with base and nested columns from a flat dataframe.
- Parameters:
df (pd.DataFrame or NestedFrame) – A dataframe with list columns.
base_columns (list-like, or None) – Any columns that have non-list values in the input df. These will simply be kept as identical columns in the result
list_columns (list-like, or None) – The list-value 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.
name – The name of the output column the nested_columns are packed into.
- Returns:
A NestedFrame with the specified nesting structure.
- Return type:
Examples
>>> import nested_pandas as npd >>> nf = npd.NestedFrame({"c":[1,2,3], "d":[2,4,6], ... "e":[[1,2,3], [4,5,6], [7,8,9]]}, ... index=[0,1,2])
>>> npd.NestedFrame.from_lists(nf, base_columns=["c","d"]) c d nested 0 1 2 [{e: 1}; …] (3 rows) 1 2 4 [{e: 4}; …] (3 rows) 2 3 6 [{e: 7}; …] (3 rows)