You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pandas DataFrame批量生成规则序列索引、避免手动输入的方案咨询

pandas DataFrame 自定义索引生成方案

实现代码

import pandas as pd

# 替换为你自己的DataFrame对象
# df = pd.DataFrame(index=range(1111))

# 生成前11行索引
first_part = [f"p{i*100:03d}" if i < 10 else "p1000" for i in range(11)]

# 生成中间100行索引
second_part = []
for X in range(1, 11):
    for j in range(1, 11):
        second_part.append(f"p{X}{j*10}")

# 生成最后1000行索引
third_part = []
for block_id in range(1, 11):
    # 块内序号补零为3位,保证格式统一
    for seq in range(1, 101):
        third_part.append(f"p{block_id}{seq:03d}")

# 合并索引并赋值给DataFrame
custom_index = first_part + second_part + third_part
df.index = custom_index

# 校验长度,输出1111即为匹配成功
print(len(custom_index))

规则匹配说明

  • 前11行完全覆盖p000到p1000的要求格式
  • 中间100行每个X取值1-10,对应生成pX10到pX100格式的索引
  • 最后1000行分为10个百行块,p后第一位依次为1到10,和要求的块编号替换规则完全匹配

内容的提问来源于stack exchange,提问作者Theresa_S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 08:27:02