我使用numpy来添加一个新列,删除另一个列。我相信只有两个论点是允许的,但我需要3个。这有可能用elif语句吗?
我需要S3是"VM",CloudWatch是“磁盘”,其他一切都是“其他”
我所拥有的:
data_1 = pd.read_csv('data.csv')
data_1['ADDED_COLUMN1'] = np.where(data_1.DIMENSION.isin(['S3', 'Glacier']),
'VM', 'Other')输出:
S3 VM
Glacier VM
S3 VM
S3 VM
CloudWatch VM
Athena Other我想要的:
S3 VM
Glacier VM
S3 VM
S3 VM
CloudWatch Disk
Athena Other如何再添加一个参数才能得到这个输出?
发布于 2021-01-11 05:41:58
您可以在这里使用numpy.select
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-11 02:10:33
您可以使用以下语法:
np.where((condition 1) & (condition 2))https://stackoverflow.com/questions/65660509
复制相似问题