Series.str.rsplit(self, 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: |
|
|---|---|
| Returns: |
|
See also
Series.str.split
Series.str.rsplit
Series.str.join
str.split
str.rsplit
The handling of the n keyword depends on the number of found splits:
n, make first n splits onlyn, make all splitsn, append None for padding up to n if expand=True
If using expand=True, Series and Index callers return DataFrame and MultiIndex objects, respectively.
>>> s = pd.Series(["this is a regular sentence", ... "https://docs.python.org/3/tutorial/index.html", ... np.nan]) 0 this is a regular sentence 1 https://docs.python.org/3/tutorial/index.html 2 NaN dtype: object
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: object
Without the n parameter, the outputs of rsplit and split are identical.
>>> s.str.rsplit() 0 [this, is, a, regular, sentence] 1 [https://docs.python.org/3/tutorial/index.html] 2 NaN dtype: object
The n parameter can be used to limit the number of splits on the delimiter. The outputs of split and rsplit are 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: object
The pat parameter 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: object
When 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 NaN
For 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
Remember to escape special characters when explicitly using regular expressions.
>>> s = pd.Series(["1+1=2"])
>>> s.str.split(r"\+|=", expand=True)
0 1 2
0 1 1 2
© 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.25.0/reference/api/pandas.Series.str.rsplit.html