Remove Whitespace in Pandas
Creating Sample Pandas DataFrame.
Python3
# importing library import pandas as pd # Creating dataframe df = pd.DataFrame({ 'Names' : [ ' Sunny' , 'Bunny' , 'Ginny ' , ' Binny ' , ' Chinni' , 'Minni' ], 'Age' : [ 23 , 44 , 23 , 54 , 22 , 11 ], 'Blood Group' : [ ' A+' , ' B+' , 'O+' , 'O-' , ' A-' , 'B-' ], 'Gender' : [ ' M' , ' M' , 'F' , 'F' , 'F' , ' F' ] }) print (df) |
Output:
In the above figure, we are observing that inside Name, Age, Blood Group, and Gender columns, data is in an irregular manner. In most of the cells of a particular column, extra whitespace are present in the leading part of the values
Pandas – Strip whitespace from Entire DataFrame
“We can have data without information, but we cannot have information without data.” How beautiful this quote is. Data is the backbone of a Data Scientist and according to a survey, data scientist spends approximately 60% of their time in Cleaning and Organizing Data, so it’s our responsibility to become familiar with different techniques to organize the data in a better way.
In this article, we will learn about different methods to remove the extra strip whitespace from the entire DataFrame. The dataset used here is given below: