Home > database >  Cannot use replace method on python
Cannot use replace method on python

Time:03-14

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

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.

CodePudding user response:

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
  • Related