Faster way to rank rows in subgroups in pandas dataframe

前端 未结 2 1567
误落风尘
误落风尘 2020-12-07 22:56

I have a pandas data frame that has is composed of different subgroups.

    df = pd.DataFrame({
    \'id\':[1, 2, 3, 4, 5, 6, 7, 8], 
    \'group\':[\'a\',          


        
2条回答
  •  抹茶落季
    2020-12-07 23:53

    Working with a big DataFrame (13 million lines), the method rank with groupby maxed out my 8GB of RAM an it took a really long time. I found a workaround less greedy in memory , that I put here just in case:

    df.sort_values('value')
    tmp = df.groupby('group').size()
    rank = tmp.map(range)
    rank =[item for sublist in rank for item in sublist]
    df['rank'] = rank
    

提交回复
热议问题