Unnest List Columns in Pandas | Ben Cunningham

Unnest List Columns in Pandas

Written by Ben Cunningham · on July 31, 2018

Don’t fear the list column. Jenny Bryan touched on this in her PLOTCON 2016 talk and it’s one of the best programming tips I’ve taken to heart over the past few years. In R, list columns are super easy to create with purrr, and just as easy to unravel with tidyr::unnest.

They’re not too difficult to create in Python with pandas either. But unnesting them confuses me every time. Here’s a quick function to handle it:

import pandas as pd

def unnest(df, col):
    unnested = (df.apply(lambda x: pd.Series(x[col]), axis=1)
                .stack()
                .reset_index(level=1, drop=True))
    unnested.name = col
    return df.drop(col, axis=1).join(unnested)