Pandas: Strip white spaces from columns with mixed string-float values

Viewed 1249

I have a dataframe with mixed string and float/int values in column 'k':

>>> df
   a  b  k
0  1  a  q
1  2  b  1
2  3  c  e
3  4  d  r

When I do this to remove any whitespaces from all columns:

df.apply(lambda x: x.str.strip() if x.dtype == "object" else x)

It converts the integer 1 to a NaN:

   a  b    k
0  1  a    q
1  2  b  NaN
2  3  c    e
3  4  d    r

How can I overcome this?

1 Answers

You can do with mask and to_numeric, this will mask all nonnumeric value to NaN

df=df.mask(df.apply(pd.to_numeric,errors = 'coerce').isnull(),df.astype(str).apply(lambda x : x.str.strip()))
df
Out[572]: 
   a  b  k
0  1  a  q
1  2  b  1
2  3  c  e
3  4  d  r
Related