python—按组分隔列

8wtpewkr  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(190)

以下是我的数据的外观:

这是我提出的代码:

surgery_types = ["Ascendensvervanging", "AVR", "AVR + MVP", "Bentall", "CABG", "CABG + AVR", "CABG + MVP", "Epicardiale LV-lead", "Lobectomie of segmentresectie", "Mediastinoscopie", "MVP", "MVP + TVP", "Nuss","Refixatie sternum", "Staaldraden verwijderen", "VATS Boxlaesie", "Wondtoilet"]

这是我需要的手术和休息的列表,我想将其重命名为“其他类型”。我有158种不同类型的手术。有人能帮我吗?

icomxhvb

icomxhvb1#

尝试:
布尔掩蔽, loc 存取器和 str.contains() :

m=df['Surgery Type'].str.contains('|'.join(surgery_types),case=False)

# created a boolean mask

df.loc[~m,'Surgery Type']='other_types'

# pass that boolean mask(opposite) and changed value


布尔掩蔽, loc 存取器和 isin() :

m=df['Surgery Type'].isin(surgery_types)

# created a boolean mask

df.loc[~m,'Surgery Type']='other_types'

# pass that boolean mask(opposite) and changed value

相关问题