22 Dec Remove Whitespace or specific characters in Pandas
To remove whitespace (including newlines) or specific characters on text data in a Series or DataFrame, use the following methods in Python Pandas:
- strip(): Strip whitespace (including newlines) or specific characters from the left and right
- lstrip(): Strip whitespace (including newlines) or specific characters from only the left side
- rstrip(): Strip whitespace (including newlines) or specific characters from only the right side
Before moving further, we’ve prepared a video tutorial to remove whitespace or specific characters in Pandas:
strip() method
To strip whitespace (including newlines) or specific characters from both the left and right side of values in a Series or DataFrame, use the strip() method in Pandas. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# strip() method in Python Pandas # Code by Studyopedia import pandas as pd # Data to be stored in the Pandas Series data = ["!Jacob", "Amit\n\n", "Trent", "Nathan\t", "Martin"] # Create a Series using the Series() method series = pd.Series(data) # Display the Series print("Series:\n", series) # Strip the values print("\nStrip from both the sides\n",series.str.strip("!\n\t")) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Series: 0 !Jacob 1 Amit\n\n 2 Trent 3 Nathan\t 4 Martin dtype: object Strip from both the sides 0 Jacob 1 Amit 2 Trent 3 Nathan 4 Martin dtype: object |
lstrip() method
To strip whitespace (including newlines) or specific characters from the left side of values in a Series or DataFrame, use the lstrip() method in Pandas. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# lstrip() method in Python Pandas # Code by Studyopedia import pandas as pd # Data to be stored in the Pandas Series data = ["!Jacob", "\n\tAmit\n\n", "!Trent!", "Nathan\t", "Martin"] # Create a Series using the Series() method series = pd.Series(data) # Display the Series print("Series:\n", series) # Strip from the left print("\nStrip from the left side:\n", series.str.lstrip("!\n\t")) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Series: 0 !Jacob 1 \n\tAmit\n\n 2 !Trent! 3 Nathan\t 4 Martin dtype: object Strip from the left side: 0 Jacob 1 Amit\n\n 2 Trent! 3 Nathan\t 4 Martin dtype: object |
rstrip() method
To strip whitespace (including newlines) or specific characters from the right side of values in a Series or DataFrame, use the rstrip() method in Pandas. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# rstrip() method in Python Pandas # Code by Studyopedia import pandas as pd # Data to be stored in the Pandas Series data = ["!Jacob", "\n\tAmit\n\n", "!Trent!", "Nathan\t", "Martin"] # Create a Series using the Series() method series = pd.Series(data) # Display the Series print("Series:\n", series) # Remove characters from the right side print("\nRemove from the right:\n", series.str.rstrip("\n\t!")) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Series: 0 !Jacob 1 \n\tAmit\n\n 2 !Trent! 3 Nathan\t 4 Martin dtype: object Remove from the right: 0 !Jacob 1 \n\tAmit 2 !Trent 3 Nathan 4 Martin dtype: object |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments