Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dataframe groupby rank by multiple column value

df = df.sort_values(by=["x","y"], ascending=False)
df['rank']=tuple(zip(df.x,df.y))
df['rank']=df.groupby('sort_by',sort=False)['rank'].apply(lambda x : pd.Series(pd.factorize(x)[0])).values
df
Out[615]: 
  sort_by      x       y  rank
3       a  500.0  1000.0     1
1       a  200.0  2000.0     2
2       a  200.0  2000.0     2
7       a  200.0   100.5     3
0       a  100.5  4000.0     4
6       b    3.0   600.5     1
5       b    2.0   600.5     2
4       b    1.0   500.5     3
Comment

PREVIOUS NEXT
Code Example
Python :: k means image classification 
Python :: save a text file from web python 
Python :: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly 
Python :: python script superuser 
Python :: how to correct spelling in pandas datafeame 
Python :: print fps in while loop python 
Python :: how to dynamically search for a class variable in python 
Python :: topaz barziv 
Python :: remove all the valu ein dict exacpt provided key pythn 
Python :: explorer gives new window 
Python :: name decorator in python 
Python :: dorp ligne in df where values equal zeros 
Python :: what are the mouseX/mouseY variebles in pycharm 
Python :: Perform a right outer join of self and other. 
Python :: there is no difference in R between a string scalar and a vector of strings 
Python :: how to get only non-blank entry of list in python 
Python :: getting vocab from a text file python 
Python :: slicing time series 
Python :: discord.py get channel name from id 
Python :: arabert 
Python :: cannot import name Glib 
Python :: python writelignes 
Python :: sort true 
Python :: elevando numero ao quadrado em python 
Python :: geopandas españa map 
Python :: openign in browser python 
Python :: minio python remove an object 
Python :: check cudann 
Python :: python read vcf file line by line 
Python :: exercise of python loops 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =