To unnest a column containing nested pandas Series (or lists, dictionaries, or similar structures) into individual rows or columns, you can use various pandas methods. This operation is similar to R’s tidyr::unnest() function.
Here’s how you can unnest Series in a pandas DataFrame:
To unnest the nested_series column into rows, use explode(). It expands each nested structure into separate rows.
# Unnest the nested_series into separate rowsdf_unnested = df.explode('nested_series').reset_index(drop=True)df_unnested
id
nested_series
0
1
10
1
1
20
2
1
30
3
2
40
4
2
50
5
3
60
# Unnest the nested_series into separate rowsdf_unnested2 = df2.explode('nested_dict').reset_index(drop=True)df_unnested2
id
nested_dict
0
1
A
1
1
B
2
2
A
3
2
B
4
3
A
5
3
B
Explanation:
explode() splits the Series in the nested_series column into individual rows.
reset_index() is used to reindex the resulting DataFrame for cleaner output.
16.1.2 Unnest into Columns
16.1.2.1 Series
If you want to unnest the Series into multiple columns, convert them into a DataFrame using pd.concat or .apply(pd.Series).
# Unnest the nested_series into separate columnsdf_unnested_cols = pd.concat( [df.drop(columns='nested_series'), df['nested_series'].apply(pd.Series)], axis=1 )df_unnested_cols
df['nested_series'].apply(pd.Series) converts the Series in the column into a DataFrame with one column per value.
pd.concat() merges this with the rest of the original DataFrame.
16.1.3 Unnest into a Long Format
You can unnest into a long format with identifiers using explode() and create an additional identifier column.
# Add an identifier column for position in nested Seriesdf_long = df.explode('nested_series').reset_index(drop=True)df_long['position'] = df_long.groupby('id').cumcount() +1df_long
id
nested_series
position
0
1
10
1
1
1
20
2
2
1
30
3
3
2
40
1
4
2
50
2
5
3
60
1
Explanation:
explode() unnests the Series into rows.
groupby('id').cumcount() adds a position identifier for the unnesting, mimicking the long format with row-wise detail.
16.1.4 Summary of Methods:
Goal
Method
Example
Unnest into rows
explode()
df.explode('column')
Unnest into columns
apply(pd.Series) + pd.concat()
pd.concat([...], axis=1)
Unnest into long format
explode() + groupby().cumcount()
df.explode('column').reset_index()
Let me know if you have more specific scenarios or need additional examples!