1

I'm using numpy to add to a new column baased off another column. I believe only 2 arguments are allowed but I need 3. Is this possible with an elif statement?

I need S3 to be "VM", CloudWatch to be "Disk", and everything else to go as "Other"

What I have:

data_1 = pd.read_csv('data.csv')

data_1['ADDED_COLUMN1'] = np.where(data_1.DIMENSION.isin(['S3', 'Glacier']), 
'VM', 'Other')

Output:

S3             VM
Glacier        VM
S3             VM
S3             VM
CloudWatch     VM
Athena       Other

What I want:

S3             VM
Glacier        VM
S3             VM
S3             VM
CloudWatch     Disk
Athena         Other

How do I add 1 more argument to get this output?

4

2 回答 2

0

You can use this syntax:

np.where((condition 1) & (condition 2))
于 2021-01-11T02:10:33.430 回答
0

You can use numpy.select here

conditions  = [ data_1.DIMENSION.isin(["s3","Glacier"]), data_1.DIMENSION == "CloudWatch" ]
choices = ["VM", "Disk"]

data_1["ADDED_COLUMN1"] = np.select(conditions, choices, default="Other")

data_1
    DIMENSION ADDED_COLUMN1
0          s3            VM
1     Glacier            VM
2          s3            VM
3          s3            VM
4  CloudWatch          Disk
5      Athena         Other

于 2021-01-11T05:41:58.787 回答