'how to do check column values is not clash if all value correspond to same value of other column, if clashed then return that row
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
Solution 1:[1]
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
Solution 2:[2]
clashed if all values in s_id groups are the same (duplicated):
df['clash'] = df.duplicated(['s_id', 'PSC'], keep=False)\
.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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jezrael |
| Solution 2 |
