1

How do I use multiple conditions to correct values in my dataframe

I want to change

  • Apple = Fruit
  • Potato = Veg

My data frame is like this

      Item      Category
      Apple     Freshco
      Potato    Sobeys
      Orange    Fruit
      Banana    Fruit

I want to change the two categories so the dataframe is like this

      Item      Categroy
      Apple     Fruit
      Potato    Veg
      Orange    Fruit
      Banana    Fruit  

2 Answers 2

1

Use .loc:

df.loc[df['Item'] == 'Apple', 'Category'] = 'Fruit'
df.loc[df['Item'] == 'Potato', 'Category'] = 'Veg'

Output:

>>> df
     Item Category
0   Apple    Fruit
1  Potato      Veg
2  Orange    Fruit
3  Banana    Fruit

More dynamic version:

reps = {
    'Apple': 'Fruit',
    'Potato': 'Veg',
}
df.loc[df['Item'].isin(reps), 'Category'] = df['Item'].replace(reps)
Sign up to request clarification or add additional context in comments.

Comments

1

Or using numpy.select:

np.select([df['Item'] == 'Apple', df['Item'] == 'Potato'], ['Fruit', 'Veg'], df['Category'])

Or, using a dict:

mapping = {
    'Apple': 'Fruit',
    'Potato': 'Veg',
}
df['Category'] = np.select([df['Item'] == k for k in mapping.keys()], [v for v in mapping.values()], df['Category'])

2 Comments

You can also use np.select. It's basicaly like a super-where: np.select([df['Item'] == 'Apple', df['Item'] == 'Potato'], ['Fruit', 'Veg'], df['Category'])
I didn't know numpy select, but then you can make it a legible oneliner. Thus that seems a good option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.