pythonpandasdataframenumpydata-filtering

repeating nth times of a value in a column of a DataFrame in Python


I have a DataFrame with 3532 rows and 20 columns in Python. I am trying to repeat the 5times of each value from the first row of the column of 'Snew' up to the 706th value. If you see the column below, I have 'Snew' column that has 3532 rows but I just want to repeat 5 times each value from the first row to the value of the 706th row, then I will have a new column with a repeated value with 3532 rows. I would be happy if anyone have any idea. I tried df.repeat also concatenate command in numpy and pandas but it is not working well

enter image description here


Solution

  • Use numpy.repeat with Series constructor:

    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