The Python Oracle

Is it possible to force pandas not to convert data type when using DataFrame.replace

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Ancient Construction

--

Chapters
00:00 Question
00:41 Accepted answer (Score 8)
00:50 Answer 2 (Score 6)
01:09 Thank you

--

Full question
https://stackoverflow.com/questions/4580...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas

#avk47



ACCEPTED ANSWER

Score 8


df.update(df.blocks['object'].astype(str).replace({',': '.'}))



ANSWER 2

Score 6


How about this:

In [25]: df.loc[:, df.dtypes=='object'] = \
             df.select_dtypes(['object']).astype(str).replace({',': '.'})

This will apply .replace only to columns of a string (object) dtype