Skip to content Skip to sidebar Skip to footer

Groupby Of Multiple Columns And Assigning Values To Each By Considering Start And End Of Each (pandas)

I've got a datframe that looks like that df1 v w x y 4 0 1 a b 5 0 1 a a _________________ 6 0 2 a b ______________

Solution 1:

df=df.reset_index(drop=True)
s=pd.DataFrame(np.sort(df[['x','y']],axis=1),index=df.index)[1].iloc[::-1].ne('b').cumsum()
df.groupby([df.v,df.w,s]).ngroup()
0     0
1     0
2     1
3     2
4     4
5     5
6     5
7     3
8     6
9     6
10    6
11    6
dtype: int64

Post a Comment for "Groupby Of Multiple Columns And Assigning Values To Each By Considering Start And End Of Each (pandas)"