Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replacing dots in pandas dataframe

I have a dataframe as in the image. (the numbers are really objects). Doing df.treasury_rate = pd.to_numeric(df.treasury_rate), predictably bombs. However, doing df.replace('.', np.nan) does not appear to get rid of the dot, so I am flummoxed. Any suggestions?

UPDATE pd.to_numeric takes an errors keyword, which, when set to coerce does the right thing, however, I am still confused as to why the . is not getting replaced.

UPDATE 2 As text:

treasury_rate
1962-02-09 4.05
1962-02-10 4.05
1962-02-11 4.05
1962-02-12 .
1962-02-13 4.03
1962-02-14 4.03
1962-02-15 4.02 
1962-02-16 4.02
1962-02-17 4.02
1962-02-18 4.02
like image 501
Igor Rivin Avatar asked Aug 04 '26 07:08

Igor Rivin


1 Answers

df.replace('\.','0', regex=True,inplace=True)

I think you have to give regex for '.' character to replace it.

like image 148
Nusrath Avatar answered Aug 05 '26 20:08

Nusrath