Skip to content
Advertisement

Apply strip() to all cells in dataframe with multiple data types

I have a dataframe that has multiple data types. Part of my processing code is to apply the strip() function before I work on the df.

My example df:

Unnamed: 1      Unnamed: 2      Unnamed: 3  Unamed: 4
Protocol Number NaN             NaN         5
xyz-4134        NaN             3           NaN
Section FINANCE Cost Category   Major Tasks # of Units 
NaN             1325            some string NaN

Here is my code:

df.applymap(lambda x: x.strip() if type(x) == str else x)

It doesn’t seem to be processing for all strings though. I’m still seeing spaces before and after in some of my output cells.

Question

Is there a better way to apply strip() to my entire raw dataframe? In the above input dataframe, the # of Units has a trailing white space and the some string has a leading white space

Advertisement

Answer

Incorporating the suggestions by both IgnatiusReilly & Rabinzel, this worked for me:

df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x)

Also worth noting, the reason the # of Units was keeping the trailing space was because I used used applymap after I already assigned that row as the headers. I moved the cleaning before the header assignment and it worked.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement