首页 > 解决方案 > 将列中的值重复 n 次

问题描述

我有以下数据框

 Code 
   1
   3
   4
   5

我想将每个代码值重复 5 次,以便输出为:

 Code 
   1
   1
   1
   1
   1
   3
   3
   3
   3
   3
   4
   4
   4
   4
   4
   5
   5
   5
   5
   5

np.repeat 似乎不适用于数据帧

标签: pythonpandasdataframenumpy

解决方案


You can repeat the index:

df.loc[df.index.repeat(5)]

推荐阅读