Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot use replace method on python

I wanted to change my variables on spesific column with dictionary values but it does not change. I tried several ways and but it does not work. My dataset has 47k rows and my dictionary has 30 different words so I will show some.

My dataset:

My dataset

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Dictionary:

rolechange = {"\\Adv":"Adversary",
          "\\Sci":"Scientist",
          "\\Inn":"Innocent",
          "\\Und":"Undetermined"}

I’m trying

movies_df["Role Type"].replace(rolechange, inplace=True)

It does not gives error but result is same. I couldn’t find similar question on here, sorry for if its duplicate.

>Solution :

You just have to create raw strings (prefix 'r')

rolechange = {r"\\Adv":"Adversary",
              r"\\Sci":"Scientist",
              r"\\Inn":"Innocent",
              r"\\Und":"Undetermined"}

>>> df['Role Type'].replace(rolechange)

0       Scientist
1        Innocent
2    Undetermined
Name: Role Type, dtype: object
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading