How to replicate/ copy pandas dataframe rows

I need to copy the same block of rows 3 times exact below this blocks. Like below example.

DataFrame (df1):-

S.No    Cal        Date              DFM

  1            8       01-03-2013   100.00%
  1            7       01-06-2013   100.00%
  1            6       01-09-2013   100.00%
  1            5       01-12-2013   99.99%
  1            4       01-03-2014   99.97%
  1            3       01-06-2014   99.95%
  1            2       01-09-2014   98.89%
  1            1      01-12-2014   39.36%
  1            0      01-03-2015   0.00%
  1            0      01-06-2015   0.00%
  1            0      01-09-2015   0.00%
  1            0      01-12-2015   0.00%
  2            8      01-03-2013   100.00%
  2            7      01-06-2013   100.00%
  2            6      01-09-2013   100.00%
  2            5      01-12-2013   99.99%
  2            4      01-03-2014   99.97%
  2            3      01-06-2014   97.95%
  2            2      01-09-2014   94.42%
  2            1      01-12-2014   43.13%
  2            0      01-03-2015   0.00%
  2            0      01-06-2015   0.00%
  2            0      01-09-2015   0.00%
  2            0      01-12-2015   0.00%
  3            8      01-03-2013   100.00%
  3            7      01-06-2013   100.00%
  3            6      01-09-2013   100.00%
  3            5      01-12-2013   99.99%
  3            4      01-03-2014   99.96%
  3            3      01-06-2014   99.94%
  3            2      01-09-2014   98.85%
  3            1      01-12-2014   54.77%
  3            0      01-03-2015   0.00%
  3            0      01-06-2015   0.00%
  3            0      01-09-2015   0.00%
  3            0      01-12-2015   0.00%

Result:-

1st DataFrame:-

S.No     Cal     Date              DFM

  1            8       01-03-2013   100.00%
  1            7       01-06-2013   100.00%
  1            6       01-09-2013   100.00%
  1            5       01-12-2013   99.99%
  1            4       01-03-2014   99.97%
  1            3       01-06-2014   99.95%
  1            2       01-09-2014   98.89%
  1            1      01-12-2014   39.36%
  1            0      01-03-2015   0.00%
  1            0      01-06-2015   0.00%
  1            0      01-09-2015   0.00%
  1            0      01-12-2015   0.00%
  1            8      01-03-2013   100.00%
  1            7      01-06-2013   100.00%
  1            6      01-09-2013   100.00%
  1            5      01-12-2013   99.99%
  1            4      01-03-2014   99.97%
  1            3      01-06-2014   99.95%
  1            2      01-09-2014   98.89%
  1            1      01-12-2014   39.36%
  1            0      01-03-2015   0.00%
  1            0      01-06-2015   0.00%
  1            0      01-09-2015   0.00%
  1            0      01-12-2015   0.00%
  1            8      01-03-2013   100.00%
  1            7      01-06-2013   100.00%
  1            6      01-09-2013   100.00%
  1            5      01-12-2013   99.99%
  1            4      01-03-2014   99.97%
  1            3      01-06-2014   99.95%
  1            2      01-09-2014   98.89%
  1            1      01-12-2014   39.36%
  1            0      01-03-2015   0.00%
  1            0      01-06-2015   0.00%
  1            0      01-09-2015   0.00%
  1            0      01-12-2015   0.00%
  2            8      01-03-2013   100.00%
  2            7      01-06-2013   100.00%
  2            6      01-09-2013   100.00%
  2            5      01-12-2013   99.99%
  2            4      01-03-2014   99.97%
  2            3      01-06-2014   97.95%
  2            2      01-09-2014   94.42%
  2            1      01-12-2014   43.13%
  2            0      01-03-2015   0.00%
  2            0      01-06-2015   0.00%
  2            0      01-09-2015   0.00%
  2            0      01-12-2015   0.00%
  2            8      01-03-2013   100.00%
  2            7      01-06-2013   100.00%
  2            6      01-09-2013   100.00%
  2            5      01-12-2013   99.99%
  2            4      01-03-2014   99.97%
  2            3      01-06-2014   97.95%
  2            2      01-09-2014   94.42%
  2            1      01-12-2014   43.13%
  2            0      01-03-2015   0.00%
  2            0      01-06-2015   0.00%
  2            0      01-09-2015   0.00%
  2            0      01-12-2015   0.00%
  2            8      01-03-2013   100.00%
  2            7      01-06-2013   100.00%
  2            6      01-09-2013   100.00%
  2            5      01-12-2013   99.99%
  2            4      01-03-2014   99.97%
  2            3      01-06-2014   97.95%
  2            2      01-09-2014   94.42%
  2            1      01-12-2014   43.13%
  2            0      01-03-2015   0.00%
  2            0      01-06-2015   0.00%
  2            0      01-09-2015   0.00%
  2            0      01-12-2015   0.00%
  3            8      01-03-2013   100.00%
  3            7      01-06-2013   100.00%
  3            6      01-09-2013   100.00%
  3            5      01-12-2013   99.99%
  3            4      01-03-2014   99.96%
  3            3      01-06-2014   99.94%
  3            2      01-09-2014   98.85%
  3            1      01-12-2014   54.77%
  3            0      01-03-2015   0.00%
  3            0      01-06-2015   0.00%
  3            0      01-09-2015   0.00%
  3            0      01-12-2015   0.00%
  3            8      01-03-2013   100.00%
  3            7      01-06-2013   100.00%
  3            6      01-09-2013   100.00%
  3            5      01-12-2013   99.99%
  3            4      01-03-2014   99.96%
  3            3      01-06-2014   99.94%
  3            2      01-09-2014   98.85%
  3            1      01-12-2014   54.77%
  3            0      01-03-2015   0.00%
  3            0      01-06-2015   0.00%
  3            0      01-09-2015   0.00%
  3            0      01-12-2015   0.00%
  3            8      01-03-2013   100.00%
  3            7      01-06-2013   100.00%
  3            6      01-09-2013   100.00%
  3            5      01-12-2013   99.99%
  3            4      01-03-2014   99.96%
  3            3      01-06-2014   99.94%
  3            2      01-09-2014   98.85%
  3            1      01-12-2014   54.77%
  3            0      01-03-2015   0.00%
  3            0      01-06-2015   0.00%
  3            0      01-09-2015   0.00%
  3            0      01-12-2015   0.00%

    

1 answer

  • answered 2021-06-23 07:20 Mustafa Aydın

    One way is to group by "S.No" and then return a concatenated frame and reset the index at the end:

    new_df = df.groupby("S.No").apply(lambda d: pd.concat([d]*3)).reset_index(drop=True)
    

    to get

    >>> new_df
    
         S.No  Cal        Date      DFM
    0       1    8  01-03-2013  100.00%
    1       1    7  01-06-2013  100.00%
    2       1    6  01-09-2013  100.00%
    3       1    5  01-12-2013   99.99%
    4       1    4  01-03-2014   99.97%
    ..    ...  ...         ...      ...
    103     3    1  01-12-2014   54.77%
    104     3    0  01-03-2015    0.00%
    105     3    0  01-06-2015    0.00%
    106     3    0  01-09-2015    0.00%
    107     3    0  01-12-2015    0.00%
    
    [108 rows x 4 columns]