user14076838
user14076838

Reputation:

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')

Upvotes: 1

Views: 35

Answers (1)

jezrael
jezrael

Reputation: 863301

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

Upvotes: 1

Related Questions