search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas Series str | split method

schedule Aug 10, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas Series.str.split(~) method performs a split on each string in the Series.

Parameters

1. pat | string | optional

The string or regular expression pattern to split the strings on. By default, pat=" " (a single whitespace).

2. n | int | optional

The number of splits to allow for each value. By default, there is no limit. Note that parameter values None, 0 or -1 will be interpreted as no limit.

3. expand | boolean | optional

  • If True, then the returned list is horizontally expanded to separate columns.

  • If False, then a list is returned for each value.

By default, expand=False.

Return Value

If expand=True, then a DataFrame/MultiIndex is returned. Otherwise, a Series/Index is returned.

Examples

Basic usage

Consider the following Series:

s = pd.Series(["a","a_1","a_2"])
s
0 a
1 a_1
2 a_2
dtype: object

To split each string by _:

s.str.split("_")
0 [a]
1 [a, 1]
2 [a, 2]
dtype: object

Notice how each value in the Series is now a list.

Using regex

Regex can be directly used as the separator:

s = pd.Series(["a_1","a*2"])
s.str.split(r'[_*]')
0 [a, 1]
1 [a, 2]
dtype: object

Specifying n

By default, there is no limit as to how many splits can be made:

s = pd.Series(["a_1","a_2_3"])
s.str.split("_")
0 [a, 1]
1 [a, 2, 3]
dtype: object

To allow at most 1 split to take place for each value:

s.str.split("_", n=1)
0 [a, 1]
1 [a, 2_3]
dtype: object

Specifying expand

By default, expand=False, which means that each value becomes a list:

s = pd.Series(["a", "a_1","a_2"])
s.str.split("_")
0 [a]
1 [a, 1]
2 [a, 2]
dtype: object

You can expand the list by setting expand=True like so:

s.str.split("_", expand=True) # returns a DataFrame
0 1
0 a None
1 a 1
2 a 2

Handling missing values

The result of a split for a individual missing value (NaN) is also NaN:

s = pd.Series(["a_1",pd.np.NaN])
s.str.split("_")
0 [a, 1]
1 NaN
dtype: object
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!