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 = ''
df = pd.read_csv(url)

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