Home > OS >  how to do check column values is not clash if all value correspond to same value of other column, if
how to do check column values is not clash if all value correspond to same value of other column, if

Time:03-31

s_id    PSC
pbx     4
pbx     5
pbx     7
pby     8
pbn     8
pby     7
pbn     8

now check PSC of pbx does not clash bt PSC of pbn clashed

CodePudding user response:

If clashed means there are unique values per groups by s_id use:

df['new'] = np.where(df.groupby('s_id')['PSC'].transform('nunique').ne(1), 
                     'not clash',
                     'clashed')

print (df)
  s_id  PSC        new
0  pbx    4  not clash
1  pbx    5  not clash
2  pbx    7  not clash
3  pby    8  not clash
4  pbn    8    clashed
5  pby    7  not clash
6  pbn    8    clashed

CodePudding user response:

clashed if all values in s_id groups are the same (duplicated):

df['clash'] = df.duplicated(['s_id', 'PSC'])\
                .groupby(df['s_id'])\
                .transform('all')\
                .map({True: 'clashed', False: 'not clashed'})
print(df)

  s_id  PSC        clash
0  pbx    4  not clashed
1  pbx    5  not clashed
2  pbx    7  not clashed
3  pby    8  not clashed
4  pbn    8      clashed
5  pby    7  not clashed
6  pbn    8      clashed
  • Related