pandas groupby and rank within groups that start with 1 for each group
问题 I have a dataframe: import pandas as pd df = pd.DataFrame([[1, 'a'], [1, 'a'], [1, 'b'], [1, 'a'], [2, 'a'], [2, 'b'], [2, 'a'], [2, 'b'], [3, 'b'], [3, 'a'], [3, 'b'], ], columns=['session', 'issue']) df I would like to rank issues within sessions. I tried with: df.groupby(['session', 'issue']).size().rank(ascending=False, method='dense') session issue 1 a 1.0 b 3.0 2 a 2.0 b 2.0 3 a 3.0 b 2.0 dtype: float64 What I need is result like this one: for group session=1, there are three a issues