dataframe groupby rank by multiple column value

Solutions on MaxInterview for dataframe groupby rank by multiple column value by the best coders in the world

showing results for - "dataframe groupby rank by multiple column value"
Lainey
24 Nov 2017
1df = df.sort_values(by=["x","y"], ascending=False)
2df['rank']=tuple(zip(df.x,df.y))
3df['rank']=df.groupby('sort_by',sort=False)['rank'].apply(lambda x : pd.Series(pd.factorize(x)[0])).values
4df
5Out[615]: 
6  sort_by      x       y  rank
73       a  500.0  1000.0     1
81       a  200.0  2000.0     2
92       a  200.0  2000.0     2
107       a  200.0   100.5     3
110       a  100.5  4000.0     4
126       b    3.0   600.5     1
135       b    2.0   600.5     2
144       b    1.0   500.5     3
15