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 lines) in any text data. As can be seen in the name, str.lstrip()
is used to remove spaces from the left side of the string, str.rstrip()
to remove spaces from the right side of the string, and str.strip()
remove spaces from both sides. Since these are pandas functions with the 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
Dataset
In the following examples, the data frame used contains data from some NBA players. To download the CSV used in the code, click here. Since none of the values in the data frame has any extra spaces, the spaces are added in some elements using str.replace() method. Let’s load the dataset.
Python3
import pandas as pd
data = pd.read_csv( "nba.csv" )
print (data.head())
|
Output:
Name Team Number Position Age Height Weight College Salary
0 Avery Bradley Boston Celtics 0.0 PG 25.0 6-2 180.0 Texas 7730337.0
1 Jae Crowder Boston Celtics 99.0 SF 25.0 6-6 235.0 Marquette 6796117.0
2 John Holland Boston Celtics 30.0 SG 27.0 6-5 205.0 Boston University NaN
3 R.J. Hunter Boston Celtics 28.0 SG 22.0 6-5 185.0 Georgia State 1148640.0
4 Jonas Jerebko Boston Celtics 8.0 PF 29.0 6-10 231.0 NaN 5000000.0
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.
Python3
new = data[ "Team" ].replace( "Boston Celtics" , " Boston Celtics " ).copy()
new. str .lstrip() = = "Boston Celtics "
|
Output:
As shown in the output, the comparison is true after removing the left side spaces.
0 True
1 True
2 True
3 True
4 True
...
453 False
454 False
455 False
456 False
457 False
Name: Team, Length: 458, dtype: bool
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.
Python3
import pandas as pd
new = data[ "Team" ].replace( "Boston Celtics" , " Boston Celtics " ).copy()
new. str .strip() = = " Boston Celtic"
new. str .strip() = = "Boston Celtics "
new. str .strip() = = " Boston Celtic "
|
Output:
As shown in the output, 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.
0 False
1 False
2 False
3 False
4 False
...
453 False
454 False
455 False
456 False
457 False
Name: Team, Length: 458, dtype: bool
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.
Python3
import pandas as pd
new = data[ "Team" ].replace( "Boston Celtics" , " Boston Celtics " ).copy()
new. str .rstrip() = = " Boston Celtics"
|
Output:
As shown in the output, the comparison is true after removing the right side spaces.
0 True
1 True
2 True
3 True
4 True
...
453 False
454 False
455 False
456 False
457 False
Name: Team, Length: 458, dtype: bool
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Aug, 2023
Like Article
Save Article