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
- To run on Google Colaboratory, use this link
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_id | item_id | |
---|---|---|
0 | A | [PC, Book] |
1 | B | [Book, Table] |
2 | C | [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)
col1 | col2 | |
---|---|---|
0 | PC | Book |
1 | Book | Table |
2 | Desk | CD |
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_id | col1 | col2 | |
---|---|---|---|
0 | A | PC | Book |
1 | B | Book | Table |
2 | C | Desk | CD |
The DataFrame is now as expected.