df = pd.DataFrame({'key1' : ['a','a','a','b','b'], 'key2' : ['c','d','c','c','d'], 'data' : [1,10,2,3,30]})
>>> df
key1 key2 data0 a c 11 a d 102 a c 23 b c 34 b d 30目标结果
key1 key2 data row_number0 a c 1 11 a d 10 12 a c 2 23 b c 3 14 b d 30 1以key1、key2分组,按照data排序,取出序号应该怎么处理呢?搜索找到的以下方法没有成功df['row_number'] = df['data'].groupby(df['key1','key2']).rank(ascending=True,method='first')
2 回答
守着一只汪
TA贡献1872条经验 获得超3个赞
def cumsum_seq(v): sub = v.sort_values('data') sub['seq'] = sub['seq'].cumsum() return sub.loc[:, ['data', 'seq']] df['seq'] = 1df.groupby(['key1', 'key2']).apply(cumsum_seq).reset_index().drop(columns='level_2')
添加回答
举报
0/150
提交
取消