Expanding Each Element Stored as a List in pandas

This note explains how to expand a list stored in pandas into independent columns, assuming the lists are of equal length.

github

  • The Jupyter notebook file is available on github here

google colaboratory

Author’s Environment

My OS is macOS. The options might differ from Linux or Unix commands.

!sw_vers
ProductName:		macOS
ProductVersion:		13.5.1
BuildVersion:		22G90
!python -V
Python 3.9.17

Import the basic libraries and check their versions.

%matplotlib inline

import pandas as pd

print('pandas version :', pd.__version__)
pandas version : 2.0.3

Preparing Sample Data

df = pd.DataFrame(
    {
        "user_id": ["A", "B", "C"],
        "item_id": [["PC", "Book"], ["Book", "Table"], ["Desk", "CD"]],
    }
)

df.head()
user_iditem_id
0A[PC, Book]
1B[Book, Table]
2C[Desk, CD]

Solution

To expand a column, apply pd.Series to the column you want to expand. Apply it to the item_id column and set the column names appropriately.

df.item_id.apply(pd.Series).set_axis(["col1", "col2"], axis=1)
col1col2
0PCBook
1BookTable
2DeskCD

To apply it to only the necessary columns, use pop and join.

df.join(df.pop("item_id").apply(pd.Series).set_axis(["col1", "col2"], axis=1))
user_idcol1col2
0APCBook
1BBookTable
2CDeskCD

The DataFrame is now as expected.