The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Magic Ocean Looping

--

Chapters
00:00 Is It Possible To Force Pandas Not To Convert Data Type When Using Dataframe.Replace
00:32 Accepted Answer Score 8
00:39 Answer 2 Score 7
00:52 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