Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

remove leading and lagging spaces dataframe python

cols = df.select_dtypes(['object']).columns
df[cols] = df[cols].apply(lambda x: x.str.strip())
print (df)
                     A    B     C
0                  A b  NaN   3.0
1                  NaN  NaN   3.0
2               random  NaN   4.0
3  any txt is possible  2 1  22.0
4                       NaN  99.0
5                 help  NaN   NaN
Comment

pandas remove leading trailing spaces in dataframe

# Importing required libraries
import pandas as pd
 
# Creating DataFrame having 4 columns and but
# the data is in unregularized way.
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']
                   })
 
 
# Creating a function which will remove extra leading
# and tailing whitespace from the data.
# pass dataframe as a parameter here
def whitespace_remover(dataframe):
   
    # iterating over the columns
    for i in dataframe.columns:
         
        # checking datatype of each columns
        if dataframe[i].dtype == 'object':
             
            # applying strip function on column
            dataframe[i] = dataframe[i].map(str.strip)
        else:
             
            # if condn. is False then it will do nothing.
            pass
 
# applying whitespace_remover function on dataframe
whitespace_remover(df)
 
# printing dataframe
print(df)
Comment

remove leading and lagging spaces dataframe python

df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
print (df)
                     A    B     C
0                  A b    2   3.0
1                  NaN    2   3.0
2               random   43   4.0
3  any txt is possible  2 1  22.0
4                        23  99.0
5                 help   23   NaN
Comment

PREVIOUS NEXT
Code Example
Python :: reading doc in python 
Python :: pandas df to dict 
Python :: play sound python 
Python :: python get the intersection of two lists 
Python :: math domain error python 
Python :: python declare variables from dictionary 
Python :: find pdf encrypted password with python 
Python :: traversing a tree in python 
Python :: use of kwargs and args in python classes 
Python :: how to create staff account in django 
Python :: python math operators 
Python :: depth first search in python 
Python :: how to run python file from cmd 
Python :: dice roller in python 
Python :: how to run fastapi with code python 
Python :: timer in python 
Python :: lastindexof python 
Python :: time difference between two datetime.time 
Python :: parentheses in python 
Python :: boids algorithm 
Python :: or statement python 
Python :: how to make a clock in python 3 
Python :: list element swapping python 
Python :: tkinter delete toplevel 
Python :: to string python 
Python :: download pytz python 
Python :: pytorch older versions 
Python :: ordered dictionary 
Python :: pandas where retuning NaN 
Python :: estimate time to run a chunk of code in python 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =