Skip to content Skip to sidebar Skip to footer

Pandas Strip Function Removes Numeric Values As Well

I have a dataframe which can be generated from the code below data_file= pd.DataFrame({'studyid':[1,2,3],'age_interview': [' 56','57 ','55'],'ethnicity': ['Chinese','Indian','Europ

Solution 1:

It looks like your column has mixed integers and strings. Here's a reproducible example:

s = pd.Series([1, np.nan, 'abc ', 2.0, '  def '])
s.str.strip()

0    NaN
1    NaN
2    abc
3    NaN
4    def
dtype: object

If the value is not string, it is implicitly handled as NaN.

The solution is to convert the column and all its values to string before calling strip.

s.astype(str).str.strip()

0      1
1    nan
2    abc
3    2.0
4    def
dtype: object

In your case, that'd be

obs['valuestring'] = obs['valuestring'].astype(str).str.strip()

Note that if you want to preserve NaNs, use a mask at the end.

s.astype(str).str.strip().mask(s.isna())

0      1
1    NaN
2    abc
3    2.0
4    def
dtype: object

Post a Comment for "Pandas Strip Function Removes Numeric Values As Well"