14.7 C
London
Tuesday, July 2, 2024
HomePandas in PythonGeneral Functions in PythonHow to Split String Column in Pandas into Multiple Columns

How to Split String Column in Pandas into Multiple Columns

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following basic syntax to split a string column in a pandas DataFrame into multiple columns:

#split column A into two columns: column A and column B
df[['A', 'B']] = df['A'].str.split(',', 1, expand=True)

The following examples show how to use this syntax in practice.

Example 1: Split Column by Comma

The following code shows how to split a column in a pandas DataFrame, based on a comma, into two separate columns:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs, West', 'Spurs, West', 'Nets, East'],
                   'points': [112, 104, 127]})

#view DataFrame
df

	team	points
0	Mavs, West	112
1	Spurs, West	104
2	Nets, East	127

#split team column into two columns
df[['team', 'conference']] = df['team'].str.split(',', 1, expand=True)

#view updated DataFrame
df

	team	points	conference
0	Mavs	112	West
1	Spurs	104	West
2	Nets	127	East

Note that you can also reorder the columns after performing the split if you’d like:

#reorder columns
df = df[['team', 'conference', 'points']]

#view DataFrame
df

	team	conference points
0	Mavs	West	   112
1	Spurs	West	   104
2	Nets	East	   127

Example 2: Split Column by Other Delimiters

We can use the same syntax to split a column by other delimiters.

For example, we can split a column by a space:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs West', 'Spurs West', 'Nets East'],
                   'points': [112, 104, 127]})

#split team column into two columns
df[['team', 'conference']] = df['team'].str.split(' ', 1, expand=True)

#view updated DataFrame
df

	team	conference points
0	Mavs	West	   112
1	Spurs	West	   104
2	Nets	East	   127

We can also split a column by a slash:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs/West', 'Spurs/West', 'Nets/East'],
                   'points': [112, 104, 127]})

#split team column into two columns
df[['team', 'conference']] = df['team'].str.split('/', 1, expand=True)

#view updated DataFrame
df

	team	conference points
0	Mavs	West	   112
1	Spurs	West	   104
2	Nets	East	   127

Using this syntax, we can split a column by any delimiter we’d like.

Additional Resources

How to Add Rows to a Pandas DataFrame
How to Add a Numpy Array to a Pandas DataFrame
How to Count Number of Rows in Pandas DataFrame

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories