🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

Series.str.split()

AI & DATA SCIENCE // series-str-split

series.str.split() splits each string in a Series on a delimiter, returning a Series of lists by default, or optionally expanding the pieces into separate columns.

Syntax

series.str.split(pat=None, expand=False, n=-1)

Deep Dive Course

Without a pat argument, split() splits on any whitespace, the same default behavior as Python's own str.split(). Passing expand=True changes the return type from a Series of lists into a full DataFrame, with each split piece becoming its own column — extremely useful for breaking apart a combined field, like a full name or a city/state location, directly into separate, usable columns. The n parameter limits the number of splits performed, useful when a delimiter might appear more times than you actually want to split on.

1Understanding Series.str.split()

Without a pat argument, split() splits on any whitespace, the same default behavior as Python's own str.split(). Passing expand=True changes the return type from a Series of lists into a full DataFrame, with each split piece becoming its own column — extremely useful for breaking apart a combined field, like a full name or a city/state location, directly into separate, usable columns. The n parameter limits the number of splits performed, useful when a delimiter might appear more times than you actually want to split on.

💡

Pass expand=True to str.split() to directly get separate columns from a delimited field, like splitting a full name into distinct first-name and last-name columns, instead of getting back a Series of lists that still needs further unpacking.

editor.html
import pandas as pd

s = pd.Series(["Alice Smith", "Bob Jones"])
print(s.str.split())
localhost:3000

2Practical Example

Here is a real-world application of Series.str.split() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice Smith", "Bob Jones"]})
df[["first", "last"]] = df["name"].str.split(expand=True)
print(df)
localhost:3000

3Best Practices

Follow these guidelines when working with Series.str.split():

1. Pass expand=True when you want the split pieces as separate DataFrame columns, rather than a Series of lists needing further processing

2. Set n explicitly when a delimiter might appear more times than you want to actually split on, to avoid over-splitting

3. Handle rows where the split produces fewer pieces than expected, like a missing last name, explicitly, since expand=True fills those gaps with None

⚠️

Tip: Pass expand=True to str.split() to directly get separate columns from a delimited field, like splitting a full name into distinct first-name and last-name columns, instead of getting back a Series of lists that still needs further unpacking.

editor.html
import pandas as pd

s = pd.Series(["Alice Smith", "Bob Jones"])
print(s.str.split())
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

s = pd.Series(["Alice Smith", "Bob Jones"])
print(s.str.split())
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"name": ["Alice Smith", "Bob Jones"]})
df[["first", "last"]] = df["name"].str.split(expand=True)
print(df)

Best Practices

  • Pass expand=True when you want the split pieces as separate DataFrame columns, rather than a Series of lists needing further processing
  • Set n explicitly when a delimiter might appear more times than you want to actually split on, to avoid over-splitting
  • Handle rows where the split produces fewer pieces than expected, like a missing last name, explicitly, since expand=True fills those gaps with None

Interview Question

How would you split a full-name column directly into two separate first-name and last-name columns of a DataFrame?

Hint: Think about the expand parameter and assigning the result to multiple column names at once.

Call str.split() with expand=True, which returns a full DataFrame instead of a Series of lists, with one column per split piece, then assign that resulting DataFrame directly to a list of new column names on the original DataFrame. Because the number of resulting columns from expand=True matches the number of names in that assignment list, pandas lines them up correctly, creating two brand-new columns directly from the single combined name column in one expression.

Exercises

MediumPractice using Series.str.split() in a real scenario.
View Solution
import pandas as pd

s = pd.Series(["Alice Smith", "Bob Jones"])
print(s.str.split())

Frequently Asked Questions

How would you split a full-name column directly into two separate first-name and last-name columns of a DataFrame?

Call str.split() with expand=True, which returns a full DataFrame instead of a Series of lists, with one column per split piece, then assign that resulting DataFrame directly to a list of new column names on the original DataFrame. Because the number of resulting columns from expand=True matches the number of names in that assignment list, pandas lines them up correctly, creating two brand-new columns directly from the single combined name column in one expression.

Related Functions

series-str-extractdf-assignlists