Celeste Wilson
Celeste Wilson

Reputation: 45

How to replace string value with numeric then change string object to numeric?

I have a dataset that has numeric values but in some cells it has <0.0001 which is string. How can I replace these with 0.00005. Then I can try to convert to float from string, it won't let me do it since this has to be replaced first. Here is what I have tried and it hasn't worked.

dataframe 'new':

ID ALL
1 <0.0001
1 <0.0001
1 15.2
1 <0.0001
2 0.030
2 <0.0001
3 <0.0001
new.ALL[new.ALL == '<0.0001'] = '0.00005'
new.select_dtypes(exclude=np.number).replace(to_replace=['<0.0001'],value='0.00005')

neither one works and no error is thrown, it just won't replace it.

Upvotes: 0

Views: 618

Answers (1)

Karthik S
Karthik S

Reputation: 11584

Does this work:

df['ALL'].str.replace('<0.0001','0.00005').astype('float')
0     0.00005
1     0.00005
2    15.20000
3     0.00005
4     0.03000
5     0.00005
6     0.00005
Name: ALL, dtype: float64

Upvotes: 1

Related Questions