每隔5行我该如何在pandas DataFrame中添加新ID,

时间:2018-11-03 15:57:19

标签: python python-3.x pandas

如何每隔5行在pandas DataFrame中添加新ID,

所以我的数据是:

index            DateTime       A1   A2   A4        AMS  baseD       
16610745    2011-01-03T13:15:00 130 122 368010037   128 003669730   
16610745    2011-01-03T13:15:00 130 122 368010037   128 003669730   
16610747    2011-01-03T13:15:59 112 103 368010037   128 003669730   
16610749    2011-01-03T13:17:00 95  90  368010037   128 003669730   
16610751    2011-01-03T13:18:00 75  67  368010037   128 003669730   
16610753    2011-01-03T13:18:59 42  33  368010037   128 003669730   
16610755    2011-01-03T13:20:00 14  7   368010037   128 003669794   
16610745    2011-01-03T13:15:00 130 122 368010037   128 003669730   
16610745    2011-01-03T13:15:00 130 122 368010037   128 003669730   
16610747    2011-01-03T13:15:59 112 103 368010037   128 003669730   
16610749    2011-01-03T13:17:00 95  90  368010037   128 003669730   
16610751    2011-01-03T13:18:00 75  67  368010037   128 003669730   
16610753    2011-01-03T13:18:59 42  33  368010037   128 003669730   
16610755    2011-01-03T13:20:00 14  7   368010037   128 003669794   

我需要的是一个ID列,该ID列每5行将递增一次

index            DateTime       A1   A2   A4        AMS  baseD      ID 
16610745    2011-01-03T13:15:00 130 122 368010037   128 003669730   1
16610745    2011-01-03T13:15:00 130 122 368010037   128 003669730   1
16610747    2011-01-03T13:15:59 112 103 368010037   128 003669730   1
16610749    2011-01-03T13:17:00 95  90  368010037   128 003669730   1
16610751    2011-01-03T13:18:00 75  67  368010037   128 003669730   1
16610753    2011-01-03T13:18:59 42  33  368010037   128 003669730   2
16610755    2011-01-03T13:20:00 14  7   368010037   128 003669794   2
16610745    2011-01-03T13:15:00 130 122 368010037   128 003669730   2
16610745    2011-01-03T13:15:00 130 122 368010037   128 003669730   2
16610747    2011-01-03T13:15:59 112 103 368010037   128 003669730   2
16610749    2011-01-03T13:17:00 95  90  368010037   128 003669730   3
16610751    2011-01-03T13:18:00 75  67  368010037   128 003669730   3
16610753    2011-01-03T13:18:59 42  33  368010037   128 003669730   3
16610755    2011-01-03T13:20:00 14  7   368010037   128 003669794   3
16610755    2011-01-03T13:20:00 14  7   368010037   128 003669794   3

1 个答案:

答案 0 :(得分:3)

您可以为5%的数据帧行生成1,2,3,....序列,并将每个值重复(np.repeat)5次,并分配为ID:-)

df['id'] = np.repeat(np.arange(np.ceil(len(df)/5),5)[:len(df)]