How to Split a String Column into Two Columns in Pandas?

Spread the love

Problem –

You have a string column and you want to split it into two columns in pandas.

Solution –

Let’s read a dataset to work with.

import pandas as pd

url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv'
df = pd.read_csv(url)
df.head()

Now, let’s say you want to split the Method of Payment column into two columns. You want to split the column on white space.

df[['M1','M2']] = df['Method of Payment'].str.split(' ', 1, expand=True)

Rating: 1 out of 5.

Leave a Reply