Search code examples
python-3.xpandasremoving-whitespace

How to remove whitespace from a row in pandas dataframe?


I have a large excel file and I am looking at a single row in it and I want to remove leading and trailing whitespaces from text entries.

My code is as follows:

df = pd.read_excel(excel_file_name)
layer = df.loc[[layer_index]] # layer is a single row in df and layer_index is an integer
print(layer.iloc[:,37:42])
for col in layer.columns:
    if type(layer[col]) == str:
        layer[col] = layer[col].map(str.strip)
print(layer.iloc[:,37:42])

Column 41 contains a lot of leading and trailing whitespace so that the excel entry there is something like Cell 3 ramp . The output is:

   Unnamed: 37 Unnamed: 38 Unnamed: 39                           Unnamed: 40 Unnamed: 41
22         NaN         Ga2         NaN                       Cell 3 ramp             NaN
   Unnamed: 37 Unnamed: 38 Unnamed: 39                           Unnamed: 40 Unnamed: 41
22         NaN         Ga2         NaN                       Cell 3 ramp             NaN

But I want it to be such that the whitespaces are removed:

   Unnamed: 37 Unnamed: 38 Unnamed: 39                           Unnamed: 40 Unnamed: 41
22         NaN         Ga2         NaN                       Cell 3 ramp             NaN
   Unnamed: 37 Unnamed: 38 Unnamed: 39  Unnamed: 40 Unnamed: 41
22         NaN         Ga2         NaN  Cell 3 ramp         NaN

Why doesn't my code work?


Solution

  • Example

    we need minimal and reproducible example.

    import pandas as pd
    df = pd.DataFrame({'col1':['a', 'b', '  c'], 'col2':[1, 2, 3], 'col3':['   ab   ', '  bc', 'd']})
    

    df

    col1  col2      col3
    0    a     1     ab   
    1    b     2        bc
    2    c     3         d
    

    Code

    this is code of removing white space from string columns of all.

    cols = df.select_dtypes('object').columns
    df[cols] = df[cols].apply(lambda x: x.str.strip())
    

    df

     col1  col2 col3
    0    a     1   ab
    1    b     2   bc
    2    c     3    d