Pandas Series.str.split() function
Series.str.split() is a function in Pandas used to split strings.
During data processing, we often need to split a string into multiple parts, such as splitting a sentence into words or comma-separated values into a list.split() The function splits strings into multiple parts according to a specified delimiter.
Word meaning:split means "to separate or split", indicating dividing a string into multiple parts.
Basic syntax and parameters
str.split() is a string accessor method of Series, so you need a Series containing strings first, and then call it through the .str accessor.
Syntax format
Series.str.split(pat=None, n=-1, expand=False)
Parameter Description
| Parameters | Type | Required | Description | Default value |
|---|---|---|---|---|
| pat | str | Optional | The delimiter, which can be a string or a regular expression. Defaults to whitespace. | None (whitespace) |
| n | int | Optional | The number of splits. -1 means no limit, splitting into as many parts as possible. | -1 |
| expand | bool | Optional | Whether to expand the result into a DataFrame. Defaults to False (returns a Series of lists). | False |
Function description
- Return Value: By default, returns a Series containing lists, where each element is a split substring. When
expand=True, a DataFrame is returned. - effect: Splits each string into multiple parts according to the specified delimiter.
- Note: Uses whitespace as the delimiter by default; you can customize the delimiter.
Example
Let us thoroughly master str.split() through a series of examples from simple to complex.
Example 1: Basic Usage - Splitting by Whitespace
Example
import
pandas
as
pd
Create a Series containing sentences
s
=
pd.
Series
(
[
'hello world'
,
'runoob python'
,
'pandas data analysis'
]
)
Split by whitespace (default behavior)
result
=
s.
str
.
split
(
)
(
"Original Series:"
)
(
s
)
(
"nSplit result:"
)
(
result
)
Output result:
原始 Series:
0 hello world
1 runoob python
2 pandas data analysis
dtype: object
Split results:
0 [hello, world]
1 [runoob, python]
2 [pandas, data, analysis]
Code analysis:
s.str.split()Strings are split by whitespace (spaces, tabs, etc.) by default.- Returns a Series containing lists, each holding the split substrings.
- 'pandas data analysis' is split into three parts.
Example 2: Splitting by a Specified Delimiter
can be used via pat The parameter specifies a custom delimiter.
Example
import
pandas
as
pd
Create a Series with comma-separated values
s
=
pd.
Series
(
[
'apple,banana,orange'
,
'dog,cat,bird'
,
'red,green,blue'
]
)
Split by comma
result
=
s.
str
.
split
(
','
)
(
"Original Series:"
)
(
s
)
(
"nResult after splitting by comma:"
)
(
result
)
Output result:
原始 Series:
0 apple,banana,orange
1 dog,cat,bird
2 red,green,blue
dtype: object
Split results:
0 [apple, banana, orange]
1 [dog, cat, bird]
2 [red, green, blue]
Code analysis:
s.str.split(',')Split each string by comma.- This is a common way to handle CSV data or comma-separated values.
Example 3: Limiting the Number of Splits
Pass n The parameter can limit the number of splits.
Example
import
pandas
as
pd
s
=
pd.
Series
(
[
'a,b,c,d,e'
,
'1,2,3,4,5'
]
)
Limit to 2 parts
result_2
=
s.
str
.
split
(
','
,
n
=
2
)
Limit to 1 part (i.e., split only once)
result_1
=
s.
str
.
split
(
','
,
n
=
1
)
(
"Original Series:"
)
(
s
)
(
"nLimited to 2 parts:"
)
(
result_2
)
(
"nLimited to 1 part:"
)
(
result_1
)
Output result:
原始 Series:
0 a,b,c,d,e
1 1,2,3,4,5
dtype: object
Limit split into 2 parts:
0 [a, b, c,d,e]
1 [1, 2, 3,4,5]
Limit split into 1 part:
0 [a, b,c,d,e]
1 [1, 2,3,4,5]
Code analysis:
n=2Means split into at most 2 parts, with the last part containing all the remaining content.n=1Means split only at the first delimiter, yielding 2 parts.
Example 4: Expanding into a DataFrame
When expand=True , the split result is expanded into a DataFrame.
Example
import
pandas
as
pd
Create a Series with comma-separated values
s
=
pd.
Series
(
[
'apple,banana,orange'
,
'dog,cat,bird'
,
'red,green,blue'
]
)
Expand into a DataFrame
result
=
s.
str
.
split
(
','
,
expand
=
True
)
(
"Original Series:"
)
(
s
)
(
"nExpanded into a DataFrame:"
)
(
result
)
(
"nDataFrame type:"
,
type
(
result
)
)
Output result:
原始 Series:
0 apple,banana,orange
1 dog,cat,bird
2 red,green,blue
dtype: object
展开为 DataFrame:
0 1 2
0 apple banana orange
1 dog cat bird
2 red green blue
DataFrame 类型:
Code analysis:
expand=TrueExpand the split result into a DataFrame.- Each column represents a position after splitting.
- This is very useful when working with structured data.
Example 5: Splitting with a Regular Expression
split() It also supports using a regular expression as the delimiter.
Example
import
pandas
as
pd
Create a Series with different delimiters
s
=
pd.
Series
(
[
'hello-world'
,
'runoob_python'
,
'pandas#tutorial'
]
)
Split by regular expression (matching any of -, _, #)
result
=
s.
str
.
split
(
r
'[-_#]'
)
(
"Original Series:"
)
(
s
)
(
"nSplit by regular expression:"
)
(
result
)
Output result:
原始 Series:
0 hello-world
1 runoob_python
2 pandas#tutorial
dtype: object
Split by regular expression:
0 [hello, world]
1 [runoob, python]
2 [pandas, tutorial]
Code analysis:
r'[-_#]'is a regular expression matching any one of the characters '-', '_', or '#'.- It can handle several different delimiters at once.
Example 6: Working with Real Data
In practical applications,split() is often used in combination with other functions.
Example
import
pandas
as
pd
Simulate data extracted from logs
logs
=
pd.
Series
(
[
'2024-01-01 10:30:45 ERROR Connection failed'
,
'2024-01-01 10:31:12 INFO User logged in'
,
'2024-01-01 10:32:00 WARNING Memory usage high'
]
)
Split the log content
log_parts
=
logs.
str
.
split
(
r
's+'
,
expand
=
True
)
(
"Original log:"
)
(
logs
)
(
"nSplit DataFrame:"
)
(
log_parts
)
Rename columns
log_parts.
columns
=
[
'timestamp'
,
'level'
,
'message'
]
(
"nResult after renaming:"
)
(
log_parts
)
Output result:
Raw log:
0 2024-01-01 10:30:45 ERROR Connection failed
1 2024-01-01 10:31:12 INFO User logged in
2 2024-01-01 10:32:00 WARNING Memory usage high
dtype: object
拆分后的 DataFrame:
0 1 2
0 2024-01-01 10:30:45 ERROR Connection failed
1 2024-01-01 10:31:12 INFO User logged in
2 2024-01-01 10:32:00 WARNING Memory usage high
Renamed result:
timestamp level message
0 2024-01-01 10:30:45 ERROR Connection failed
1 2024-01-01 10:31:12 INFO User logged in
2 2024-01-01 10:32:00 WARNING Memory usage high
Code analysis:
r's+'Matches one or more whitespace characters to split the log into multiple parts.expand=TrueExpand the split result into a DataFrame.- By renaming columns, each part can be easily accessed and processed.
Notes
str.split()Whitespace is used as the delimiter by default.- When
expand=False, a Series containing lists is returned. - When
expand=True, a DataFrame is returned, with the number of columns depending on the longest split result. - If a string contains no delimiter, the returned list has a single element (the original string).
- A regular expression can serve as the delimiter, providing a more flexible way to split.
- If the Series contains NaN values, NaN is returned.
Common Pandas functions