简体   繁体   中英

Python dataframe count the values in a row and add status to the another row

Have a df with values

name    subject  

mark     M         
mark     S
mark     P
mark     SS
staurt   M
cuban    S
cuban    P

if a usercount in name is present more once in name column then present status should be YES, if it's only once then status present status should be NO

name    subject   present status

mark     M          YES
mark     S          YES
mark     P          YES
mark     SS         YES
staurt   M          No
cuban    S          YES
cuban    P          YES

tried this:

df['present status'] = np.where(df['name'] == df['name'].shift(), 'YES', 'NO')

Use Series.duplicated with keep=False :

df['present status'] = np.where(df['name'].duplicated(keep=False), 'YES', 'NO')
print (df)
     name subject present status
0    mark       M            YES
1    mark       S            YES
2    mark       P            YES
3    mark      SS            YES
4  staurt       M             NO
5   cuban       S            YES
6   cuban       P            YES

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