本文介绍如何对多级索引(如 `place_id` 和 `position_id`)数据,按每个 `place_id` 独立地将 `position_id` 映射为从 1 开始的连续整数 `location`,实现每组内位置的标准化重编号。
在实际数据分析中,原始 position_id 往往是全局唯一但不连续(例如 place 1 对应 1–4,place 2 对应 6–9),而业务需求常需将其转换为每组内从 1 开始的局部序号(即 location)。这种操作本质是按 place_id 分组后,对 position_id 进行组内排序并赋予秩次(rank)或顺序编号(cumcount),而非依赖硬编码的偏移量(如 position – 5),后者难以泛化且易出错。
✅ 推荐做法:使用 groupby().cumcount() + 1
这是最简洁、可扩展、健壮的方案:
# 假设 filterd_df 是你已有的 Series,索引为 MultiIndex (place_id, position_id)
# 先将其转为 DataFrame 便于操作(若仍为 Series)
df_reset = filterd_df.reset_index(name='count')
# 按 place_id 分组,对 position_id 组内升序排序后编号(从 0 开始 → +1 得到 location)
df_reset['location'] = (
df_reset.sort_values(['place_id', 'position_id'])
.groupby('place_id')
.cumcount() + 1
)
print(df_reset[['place_id', 'position_id', 'count', 'location']])
输出示例:
place_id position_id count location 0 1 1 1234 1 1 1 2 7000 2 2 1 3 8000 3 3 1 4 10000 4 4 2 6 4111 1 5 2 7 9875 2 6 2 8 9900 3 7 2 9 1000 4
? 关键说明: