df = pd.DataFrame({'key1' : ['a','a','a','b','b'],
'key2' : ['c','d','c','c','d'],
'data' : [1,10,2,3,30]})
>>> df
key1 key2 data
0 a c 1
1 a d 10
2 a c 2
3 b c 3
4 b d 30
目标结果
key1 key2 data row_number
0 a c 1 1
1 a d 10 1
2 a c 2 2
3 b c 3 1
4 b d 30 1
以key1、key2分组,按照data排序,取出序号应该怎么处理呢?搜索找到的以下方法没有成功
df['row_number'] = df['data'].groupby(df['key1','key2']).rank(ascending=True,method='first')
结果