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 :: how to change case of string in python 
Python :: beautifulsoup find get value 
Python :: is string mutable in python 
Python :: python how to add up all numbers in a list 
Python :: dropna threshold 
Python :: pytube 
Python :: change marker border color plotly 
Python :: python for character in string 
Python :: selenium save webpage as pdf python 
Python :: how to use argparse 
Python :: algorithms for Determine the sum of al digits of n 
Python :: django include 
Python :: how can i plot graph from 2 dataframes in same window python 
Python :: one hot encoding 
Python :: how do i get parent directory python 
Python :: replace nan with 0 pandas 
Python :: Calculate Euclidean Distance in Python using norm() 
Python :: how to write a script to display an image in python 
Python :: find the highest id in model django 
Python :: how to fix valueerror in python 
Python :: Game of Piles Version 2 codechef solution 
Python :: read a file python 
Python :: Cast image to float32 
Python :: change dataframe to list 
Python :: python execute function from string 
Python :: Fast api importing optional 
Python :: python formdata requests 
Python :: django apiview pagination 
Python :: python detect warning 
Python :: remove item list python 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =