Python | Pandas Series.str.strip(), lstrip() and rstrip()

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier.

Pandas provide 3 methods to handle white spaces(including New line) in any text data. As it can be seen in the name, str.lstrip() is used to remove spaces from the left side of string, str.rstrip() to remove spaces from right side of the string and str.strip() removes spaces from both sides. Since these are pandas function with same name as Python’s default functions, .str has to be prefixed to tell the compiler that a Pandas function is being called.

Syntax: Series.str.strip()



Return Type: Series with removed spaces

To download the CSV used in code, click here.
 

In the following examples, the data frame used contains data of some NBA players. Since none of the values in data frame is having any extra spaces, the spaces are added in some elements using str.replace() method. The image of data frame before any operations is shown below.

 

Example #1: Using lstrip()

In this example, a new series similar to Team column is created which has 2 spaces in both start and end of string. After that, str.lstrip() method is applied and checked against a custom string with removed left side spaces.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module
import pandas as pd
  
# making data frame
  
# replacing team name and adding spaces in start and end
new = data["Team"].replace("Boston Celtics", "  Boston Celtics  ").copy()
  
# checking with custom removed space string
new.str.lstrip()=="Boston Celtics  "

chevron_right


Ouptut:
As shown in the output image, the comparison is true after removing the left side spaces.

 
Example #2: Using strip()

In this example, str.strip() method is used to remove spaces from both left and right side of the string. A new copy of Team column is created with 2 blank spaces in both start and the end. Then str.strip() method is called on that series. After that, it is compared with ” Boston Celtics “, ” Boston Celtics” and “Boston Celtics ” to check if the spaces were removed from both sides or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module
import pandas as pd
  
# making data frame
  
# replacing team name and adding spaces in start and end
new = data["Team"].replace("Boston Celtics", "  Boston Celtics  ").copy()
  
# checking with custom string
new.str.strip()=="  Boston Celtic"
new.str.strip()=="Boston Celtics  "
new.str.strip()=="  Boston Celtic  "

chevron_right


Output:
As shown in the output image, the comparison is returning False for all 3 conditions, which means the spaces were successfully removed from both sides and the string is no longer having spaces.

Example #3: Using rstrip()

In this example, a new series similar to Team column is created which has 2 spaces in both start and end of string. After that str.rstrip() method is applied and checked against a custom string with removed right side spaces.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas module
import pandas as pd
  
# making data frame
  
# replacing team name and adding spaces in start and end
new = data["Team"].replace("Boston Celtics", "  Boston Celtics  ").copy()
  
# checking with custom removed space string
new.str.rstrip()=="  Boston Celtics"

chevron_right


Ouptut:
As shown in the output image, the comparison is true after removing the right side spaces.



My Personal Notes arrow_drop_up

Developer in day, Designer at night GSoC 2019 with Python Software Foundation (EOS Design system)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.