Pandas Series str | rstrip method
str.rstrip(~) method removes the combination of the specified characters that appear from the right of each string in the Series.
Note that a new Series is returned, and the original is kept intact.
The combination of the characters of
to_strip that appears at the back of each string will be removed. For instance, if
Aba will both become
By default, trailing whitespaces (including newlines and tabs) will be removed.
rstrip(~) removes trailing whitespaces (including newlines and tabs):
s = pd.Series([" ","a a","\na","a\t"])s_stripped = s.str.rstrip()s_stripped01 a a2 a3 adtype: object
To confirm that the whitespaces have indeed been stripped, we print the length of each string:
0 01 32 13 1dtype: int64
To remove a combination of the characters
"ab" at the back:
s = pd.Series(["Aab","Aba"])s.str.rstrip("ab")0 A1 Adtype: object