Using pandas.replace to change all value in a column except one value

Question:

Lets say that i have this dataframe below, and i wanted to change all the non ‘United-states’ native_country to ‘Other’ can i use pandas.replace to do this? if not what should i do?

    age education       marital_status  occupation  race    sex     native_country
1   37  Non-grad        Married         Sales       Other   Female  ?
2   39  HS-grad         Married         Sales       Other   Female  Dominican-Republic
3   29  HS-grad         Married         Sales       White   Female  United-States
4   64  HS-grad         Married         Sales       White   Female  United-States
5   31  HS-grad         Married         Sales       White   Female  United-States
6   53  HS-grad         Married         Sales       White   Female  United-States
7   56  HS-grad         Married         Sales       White   Female  United-States
8   28  Bachelors       Married         Sales       White   Female  United-States
9   52  Some-college    Married         Sales       White   Female  Canada
10  23  Some-college    Married         Sales       White   Female  United-States

The ouput i wanted:

    age education       marital_status  occupation  race    sex     native_country
1   37  Non-grad        Married         Sales       Other   Female  Other
2   39  HS-grad         Married         Sales       Other   Female  Other
3   29  HS-grad         Married         Sales       White   Female  United-States
4   64  HS-grad         Married         Sales       White   Female  United-States
5   31  HS-grad         Married         Sales       White   Female  United-States
6   53  HS-grad         Married         Sales       White   Female  United-States
7   56  HS-grad         Married         Sales       White   Female  United-States
8   28  Bachelors       Married         Sales       White   Female  United-States
9   52  Some-college    Married         Sales       White   Female  Other
10  23  Some-college    Married         Sales       White   Female  United-States
Asked By: Huesca Rashad

||

Answers:

using mask or loc

# using mask, when native country is not United-States, makes it other, else
# leave value as is

df['native_country'] = df['native_country'].mask(df['native_country'].ne('United-States'),'other')
df

OR

# using loc, updated native country, where its not united-states
df.loc[df['native_country'].ne('United-States'), 'native_country'] = 'other'
df

    age     education   marital_status  occupation  race    sex     native_country
1   37  Non-grad    Married     Sales   Other   Female  other
2   39  HS-grad     Married     Sales   Other   Female  other
3   29  HS-grad     Married     Sales   White   Female  United-States
4   64  HS-grad     Married     Sales   White   Female  United-States
5   31  HS-grad     Married     Sales   White   Female  United-States
6   53  HS-grad     Married     Sales   White   Female  United-States
7   56  HS-grad     Married     Sales   White   Female  United-States
8   28  Bachelors   Married     Sales   White   Female  United-States
9   52  Some-college    Married     Sales   White   Female  other
10  23  Some-college    Married     Sales   White   Female  United-States
Answered By: Naveed
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.