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

Time:11-10

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

CodePudding user response:

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