简体   繁体   中英

Create a new column in pandas based on conditional text values from two other columns

How to create a new column in pandas based on conditional text values from two other columns?

Initial table -

Specialty   Category  
Spec A      Cat A     
Spec A      Cat B     
Spec A      Cat C
Spec A      Cat D
Spec B      Cat A     
Spec B      Cat B     
Spec B      Cat C 
Spec B      Cat D    

Conditional Logic = Cat A and Cat D is not renamed as 'Others' Cat B and Cat C renamed as 'Others' No change in specialty. new column to concatenate Specialty and Category based on the logic above.

This table to be output to -

Specialty   Category  Specialty_group
Spec A      Cat A     Spec A Cat A       
Spec A      Cat B     Spec A Other
Spec A      Cat C     Spec A Other
Spec A      Cat D     Spec A Cat D 
Spec B      Cat A     Spec B Cat A
Spec B      Cat B     Spec B Other
Spec B      Cat C     Spec B Other
Spec B      Cat D     Spec B Cat D
# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM