On this page
pandas.Series.str.rsplit
Series.str.rsplit(pat=None, n=-1, expand=False)[source]-
Split strings around given separator/delimiter.
Splits the string in the Series/Index from the end, at the specified delimiter string. Equivalent to
str.rsplit().Parameters: -
pat : str, optional -
String or regular expression to split on. If not specified, split on whitespace.
-
n : int, default -1 (all) -
Limit number of splits in output.
None, 0 and -1 will be interpreted as return all splits. -
expand : bool, default False -
Expand the splitted strings into separate columns.
- If
True, return DataFrame/MultiIndex expanding dimensionality. - If
False, return Series/Index, containing lists of strings.
- If
Returns: - Series, Index, DataFrame or MultiIndex
-
Type matches caller unless
expand=True(see Notes).
See also
Series.str.split- Split strings around given separator/delimiter.
Series.str.rsplit- Splits string around given separator/delimiter, starting from the right.
Series.str.join- Join lists contained as elements in the Series/Index with passed delimiter.
str.split- Standard library version for split.
str.rsplit- Standard library version for rsplit.
Notes
The handling of the
nkeyword depends on the number of found splits:- If found splits >
n, make firstnsplits only - If found splits <=
n, make all splits - If for a certain row the number of found splits <
n, appendNonefor padding up tonifexpand=True
If using
expand=True, Series and Index callers return DataFrame and MultiIndex objects, respectively.Examples
>>> s = pd.Series(["this is a regular sentence", "https://docs.python.org/3/tutorial/index.html", np.nan])In the default setting, the string is split by whitespace.
>>> s.str.split() 0 [this, is, a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: objectWithout the
nparameter, the outputs ofrsplitandsplitare identical.>>> s.str.rsplit() 0 [this, is, a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: objectThe
nparameter can be used to limit the number of splits on the delimiter. The outputs ofsplitandrsplitare different.>>> s.str.split(n=2) 0 [this, is, a regular sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: object>>> s.str.rsplit(n=2) 0 [this is a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: objectThe
patparameter can be used to split by other characters.>>> s.str.split(pat = "/") 0 [this is a regular sentence] 1 [https:, , docs.python.org, 3, tutorial, index... 2 NaN dtype: objectWhen using
expand=True, the split elements will expand out into separate columns. If NaN is present, it is propagated throughout the columns during the split.>>> s.str.split(expand=True) 0 1 2 3 0 this is a regular 1 https://docs.python.org/3/tutorial/index.html None None None 2 NaN NaN NaN NaN 4 0 sentence 1 None 2 NaNFor slightly more complex use cases like splitting the html document name from a url, a combination of parameter settings can be used.
>>> s.str.rsplit("/", n=1, expand=True) 0 1 0 this is a regular sentence None 1 https://docs.python.org/3/tutorial index.html 2 NaN NaN -
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.24.2/reference/api/pandas.Series.str.rsplit.html