Reputation: 39
My dataset currently contain a column listing many competitors. I only want to retain 2 competitors (eg, ABC and KLM), and combine the rest into 1 category called Others.
For example,
df = pd.DataFrame({"competitors": ["ABC", "DEF", "123", "456", "XYZ", "KLM"]})
desired output:
competitors new_competitors
0 ABC ABC
1 DEF others
2 123 others
3 456 others
4 XYZ others
5 KLM KLM
Thank you!
Upvotes: 0
Views: 134
Reputation: 153460
Try:
df['new_competitors'] = df['competitors'].where(df['competitors'].isin(['ABC','KLM']), 'other')
Output:
competitors new_competitors
0 ABC ABC
1 DEF other
2 123 other
3 456 other
4 XYZ other
5 KLM KLM
Upvotes: 1
Reputation: 438
df = pd.DataFrame({"competitors": ["ABC", "DEF", "123", "456", "XYZ", "KLM"]})
df['new_competitors'] = [x if x in ['ABC', 'KLM'] else 'others' for x in df['competitors']]
Upvotes: 1