0
0
Pandasdata~5 mins

str.split() for splitting in Pandas

Choose your learning style9 modes available
Introduction

We use str.split() to break text into smaller parts. This helps us work with each piece separately.

You have a column of full names and want to separate first and last names.
You want to split a list of tags stored as one string into individual tags.
You need to extract parts of an address stored in one column.
You want to convert a string of numbers separated by commas into separate columns.
Syntax
Pandas
Series.str.split(pat=None, n=-1, expand=False)

pat is the character or pattern to split on. If None, splits on whitespace.

n limits the number of splits. Default -1 means no limit.

expand=True returns a DataFrame with split parts as columns.

Examples
Splits on whitespace by default.
Pandas
df['col'].str.split()
Splits the string at each comma.
Pandas
df['col'].str.split(',')
Splits only once at the first dash.
Pandas
df['col'].str.split('-', n=1)
Splits on commas and returns a DataFrame with each part as a separate column.
Pandas
df['col'].str.split(',', expand=True)
Sample Program

This code splits the full_name column into two columns: first_name and last_name.

Pandas
import pandas as pd

data = {'full_name': ['Alice Smith', 'Bob Johnson', 'Charlie Lee']}
df = pd.DataFrame(data)

# Split full_name into first and last name
split_names = df['full_name'].str.split(' ', expand=True)
split_names.columns = ['first_name', 'last_name']

print(split_names)
OutputSuccess
Important Notes

If you use expand=False (default), the result is a Series of lists.

Use expand=True to get a DataFrame with separate columns for each split part.

If the number of splits varies, some columns may have missing values (NaN).

Summary

str.split() breaks text into parts based on a separator.

Use expand=True to get multiple columns from one text column.

This helps analyze or clean text data by working with smaller pieces.