模仿SQL的row_number() over (partition by column order by column)
import pandas as pd # 创建一个示例数据框 data = { 'group': ['A', 'A', 'A', 'B', 'B', 'C', 'C', 'C', 'C'], 'value': [3, 1, 2, 5, 4, 6, 9, 7, 8] } df = pd.DataFrame(data) # 先按group分组,再按value列升序排序 df_sorted_asc = df.sort_values(by=['group', 'value']) # 使用groupby和cumcount为每组内按value升序分配一个序号 df_sorted_asc['group_rank_asc'] = df_sorted_asc.groupby('group').cumcount() + 1 print(df_sorted_asc) # 先按group分组,再按value列降序排序 df_sorted_desc = df.sort_values(by=['group', 'value'], ascending=[True, False]) # 使用groupby和cumcount为每组内按value降序分配一个序号 df_sorted_desc['group_rank_desc'] = df_sorted_desc.groupby('group').cumcount() + 1 print(df_sorted_desc)
'运行