1 回答
TA贡献1851条经验 获得超5个赞
# create a mask based on your logic
mask = (df['Category'] == 'Cat A') | (df['Category'] == 'Cat D')
# assign a values to a new column using loc and join
df.loc[mask, 'Specialty_group'] = df[mask].agg(' '.join, axis=1)
# assign values to a column using loc with the opposite of your logic
df.loc[~mask, 'Specialty_group'] = df[~mask]['Specialty']+' Other'
Specialty Category Specialty_group
0 Spec A Cat A Spec A Cat A
1 Spec A Cat B Spec A Other
2 Spec A Cat C Spec A Other
3 Spec A Cat D Spec A Cat D
4 Spec B Cat A Spec B Cat A
5 Spec B Cat B Spec B Other
6 Spec B Cat C Spec B Other
7 Spec B Cat D Spec B Cat D
添加回答
举报