在 Python 中重复第 n 次 DataFrame 的列中的值
repeating nth times of a value in a column of a DataFrame in Python
我在 Python 中有一个包含 3532 行和 20 列的 DataFrame。我正在尝试从 'Snew' 列的第一行到第 706 个值重复每个值的 5 次。如果您看到下面的列,我有一个包含 3532 行的 'Snew' 列,但我只想将每个值从第一行到第 706 行的值重复 5 次,那么我将有一个新列,其中包含具有 3532 行的重复值。
如果有人有任何想法,我会很高兴。我尝试 df.repeat 也在 numpy 和 pandas 中连接命令,但效果不佳
将 numpy.repeat
与 Series
构造函数一起使用:
df = pd.DataFrame({'Snew':range(3532)})
#seems in real data is necessary add next value (707) and filter 3532 values
df['Snew'] = np.repeat(df['Snew'].to_numpy()[:707], 5)[:3532]
print (df.tail())
Snew
3527 705
3528 705
3529 705
3530 706
3531 706
我在 Python 中有一个包含 3532 行和 20 列的 DataFrame。我正在尝试从 'Snew' 列的第一行到第 706 个值重复每个值的 5 次。如果您看到下面的列,我有一个包含 3532 行的 'Snew' 列,但我只想将每个值从第一行到第 706 行的值重复 5 次,那么我将有一个新列,其中包含具有 3532 行的重复值。 如果有人有任何想法,我会很高兴。我尝试 df.repeat 也在 numpy 和 pandas 中连接命令,但效果不佳
将 numpy.repeat
与 Series
构造函数一起使用:
df = pd.DataFrame({'Snew':range(3532)})
#seems in real data is necessary add next value (707) and filter 3532 values
df['Snew'] = np.repeat(df['Snew'].to_numpy()[:707], 5)[:3532]
print (df.tail())
Snew
3527 705
3528 705
3529 705
3530 706
3531 706