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
4defdtype: objectIf 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()
011 nan
2 abc
32.04defdtype: objectIn 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())
011 NaN
2 abc
32.04defdtype: object
Post a Comment for "Pandas Strip Function Removes Numeric Values As Well"