I have the following dataframe:
d_test = {
'c1' : ['31', '421', 'sgdsgd', '523.3'],
'c2' : ['41', np.nan, '412', '412'],
'test': [1,2,3,4],
}
df_test = pd.DataFrame(d_test)
I want to replace all values to np.nan if they are not float:
0 31 41 1
1 421 NaN 2
2 NaN 412 3
3 523.3 412 4
here what I do:
df_test[['c1', 'c2']] = df_test[['c1', 'c2']].replace(to_replace=r'^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$', value=np.nan, regex=True)
But result is not what I am looking for:
0 NaN NaN 1
1 NaN NaN 2
2 sgdsgd NaN 3
3 NaN NaN 4
.replace()will turn numbers intoNaN, but you explained that you want to turn non-numbers intoNaN. Just use.to_numeric()and be done with it.