Nabih Bawazir
Nabih Bawazir

Reputation: 7255

How to overwrite columns in pandas DataFrames of using other as existed category

Here's my dataset

No   Item             Desc
1    AWS              Lambda
2    AWS              Polly
3    Microsoft        Azure
4    Microsoft        Excel
5    Google           BigQuery
6    RDS              Database
7    Athena           Big Data
8    Lambda           NoSQL

Here's What I want is make any other item than AWS, Google, and Microsoft as AWS

No   Item             Desc
1    AWS              Lambda
2    AWS              Polly
3    Microsoft        Azure
4    Microsoft        Excel
5    Google           BigQuery
6    AWS              Database
7    AWS              Big Data
8    AWS              NoSQL

Upvotes: 1

Views: 34

Answers (1)

jezrael
jezrael

Reputation: 862511

Use numpy.where with isin:

df['Item'] = np.where(df['Item'].isin(['Microsoft','Google']), df['Item'], 'AWS')
#alternative
#df['Item'] = np.where(np.in1d(df['Item'], ['Microsoft','Google']), df['Item'], 'AWS')

Or:

df.loc[~df['Item'].isin(['Microsoft','Google']), 'Item'] = 'AWS'

print (df)
   No       Item      Desc
0   1        AWS    Lambda
1   2        AWS     Polly
2   3  Microsoft     Azure
3   4  Microsoft     Excel
4   5     Google  BigQuery
5   6        AWS  Database
6   7        AWS  Big Data
7   8        AWS     NoSQL

Upvotes: 1

Related Questions