1

I been trying to get my code to work but I am having some trouble here. It would be great if someone could assist me

DF

  Col1              Col2          
  2017-01-01        Coffee
  2017-01-01        Muffin
  2017-01-01        Donut
  2017-01-01        Toast

How can I change Col2 so that every value that isn't Coffee or Muffin becomes 'Other'?

  Col1              Col2          
  2017-01-01        Coffee
  2017-01-01        Muffin
  2017-01-01        Other
  2017-01-01        Other
0

1 Answer 1

1
In [265]: df.loc[~df.Col2.isin(['Coffee','Muffin']), 'Col2'] = 'Other'

In [266]: df
Out[266]:
         Col1    Col2
0  2017-01-01  Coffee
1  2017-01-01  Muffin
2  2017-01-01   Other
3  2017-01-01   Other
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @MaxU I was wondering how would you change it to not change the missing values. df.loc[~df.Col2.isin(['Coffee','Muffin', 'nan' ]), 'Col2'] = 'Other' ????

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.