I have a data frame like this:
d = {'id1':['3def','xyx1','roro','p344o','fjfje'],
'id2':['hhyy','drdr','xyx1','xyx1','xyx1']
,'score':[100,90,100,90,70]
}
df1 = pd.DataFrame(d)
df1
id1 id2 score
0 3def hhyy 100
1 xyx1 drdr 90
2 roro xyx1 100
3 p344o xyx1 90
4 fjfje xyx1 70
My end goal is to give a unique key to each row. The problem is that i would like the set of rows where the id1 equals the id2 & have score more than 80 to have a common key (in my case index 1,2,3). So i would like my desired result to be like this:
id1 id2 score uid
0 3def hhyy 100 1
1 xyx1 drdr 90 2
2 roro xyx1 100 2
3 p344o xyx1 90 2
4 fjfje xyx1 70 3
I have done something but doesn't seem to do the trick:
msk = (((df1['id1'].isin(df1['id2']).shift(-1)) (df1['id2'].isin(df1['id2']).shift(-1))) &\
(df1['score'] > 80).shift(-1))
df1['uid'] = msk.cumsum()
print(df1)
id1 id2 score uid
0 3def hhyy 100 1
1 xyx1 drdr 90 2
2 roro xyx1 100 3
3 p344o xyx1 90 3
4 fjfje xyx1 70 3
Any help on how to solve this would be very appreciated.