- API reference
- Series
- pandas.Serie...
pandas.Series.str.rpartition#
- Series.str.rpartition(sep='',expand=True)[source]#
Split the string at the last occurrence ofsep.
This method splits the string at the last occurrence ofsep,and returns 3 elements containing the part before the separator,the separator itself, and the part after the separator.If the separator is not found, return 3 elements containing two empty strings, followed by the string itself.
- Parameters:
- sepstr, default whitespace
String to split on.
- expandbool, default True
If True, return DataFrame/MultiIndex expanding dimensionality.If False, return Series/Index.
- Returns:
- DataFrame/MultiIndex or Series/Index of objects
See also
partitionSplit the string at the first occurrence ofsep.
Series.str.splitSplit strings around given separators.
str.partitionStandard library version.
Examples
>>>s=pd.Series(['Linda van der Berg','George Pitt-Rivers'])>>>s0 Linda van der Berg1 George Pitt-Riversdtype: object
>>>s.str.partition() 0 1 20 Linda van der Berg1 George Pitt-Rivers
To partition by the last space instead of the first one:
>>>s.str.rpartition() 0 1 20 Linda van der Berg1 George Pitt-Rivers
To partition by something different than a space:
>>>s.str.partition('-') 0 1 20 Linda van der Berg1 George Pitt - Rivers
To return a Series containing tuples instead of a DataFrame:
>>>s.str.partition('-',expand=False)0 (Linda van der Berg, , )1 (George Pitt, -, Rivers)dtype: object
Also available on indices:
>>>idx=pd.Index(['X 123','Y 999'])>>>idxIndex(['X 123', 'Y 999'], dtype='object')
Which will create a MultiIndex:
>>>idx.str.partition()MultiIndex([('X', ' ', '123'), ('Y', ' ', '999')], )
Or an index with tuples with
expand=False:>>>idx.str.partition(expand=False)Index([('X', ' ', '123'), ('Y', ' ', '999')], dtype='object')