Python – Pandas: Strips spaces from columns with mixed string floating-point values

Pandas: Strips spaces from columns with mixed string floating-point values… here is a solution to the problem.

Pandas: Strips spaces from columns with mixed string floating-point values

I have a data frame in column “k” that mixes string and floating/integer values:

>>> 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 all spaces in all columns:

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

It converts the integer 1 to 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 problem?

Solution

You can use masks and to_numeric, which mask all non-numeric values as 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 Problems and Solutions