Menu Close

How to split text in a column into multiple rows with Python Pandas?

To split text in a column into multiple rows with Python Pandas, we can use the str.split method.

For instance, we write

import pandas as pd
import numpy as np

df = pd.DataFrame(
    {'CustNum': [32363, 31316],
     'CustomerName': ['McCartney, Paul', 'Lennon, John'],
     'ItemQty': [3, 25],
     'Item': ['F04', 'F01'],
     'Seatblocks': ['2:218:10:4,6', '1:13:36:1,12 1:13:37:1,13'],
     'ItemExt': [60, 360]
    }
)
df['Seatblocks'] = df['Seatblocks'].str.split('[ :]')
df = df.explode('Seatblocks').reset_index(drop=True)
cols = list(df.columns)
cols.append(cols.pop(cols.index('CustomerName')))
df = df[cols]

to create the df data frame.

Then we call str.split on df['Seatblocks']. to split the Seatblovks column values by the :.

Then we call exploded to fill the split column values into multiple rows.

And then we create the cols columns list with list.

Next, we call cols.append to append the CustomerName column.

And then we assign the df with the cols columns back to df.

Posted in Python, Python Answers