Search code examples
pythonpython-3.xpandasdataframereplication

Replicating rows based on index multiplies the rows instead of replicating


I have dataframe, where i would like to replicate few rows:

         X      Y       diff        No
Index                              
1d       0.000   0.017  0.000e+00   0
2D       0.083   0.017  3.000e-03   1
3D       0.250   0.017  7.200e-03   2
6D       0.500   0.019  2.400e-03   3
1DD      1.000   0.020  2.400e-03   4
2DD      2.000   0.023  1.300e-03   5
3DD      3.000   0.024  1.000e-03   6
5DD      5.000   0.026  6.500e-04   7
7DD      7.000   0.027  2.667e-04   8
10DD     10.000  0.028  1.200e-04   9
20DD     20.000  0.029  1.200e-04   10
30DD     30.000  0.031  0.000e+00   11

I want to replicate 30DD 30 times and 20DD 20 times and 10DD 10 times with same index name.

I tried this, instead of replicating it multiplies

for i in range(4):
    test1 = df.append(df.ix['30DD']*30)

      X      Y       diff        No
Index                              
1d       0.000   0.017  0.000e+00   0
2D       0.083   0.017  3.000e-03   1
3D       0.250   0.017  7.200e-03   2
6D       0.500   0.019  2.400e-03   3
1DD      1.000   0.020  2.400e-03   4
2DD      2.000   0.023  1.300e-03   5
3DD      3.000   0.024  1.000e-03   6
5DD      5.000   0.026  6.500e-04   7
7DD      7.000   0.027  2.667e-04   8
10DD     10.000  0.028  1.200e-04   9
20DD     20.000  0.029  1.200e-04   10
30DD     30.000  0.031  0.000e+00   11
30DD     900     0.918  0           330

Solution

  • Add new rows, but subtract 1, because append to original DataFrame:

    vals = ['30DD'] * 29 + ['20DD'] * 19 + ['10DD'] * 9
    df = df.append(df.loc[vals])
    

    Last if want sorting values by numbers of index values:

    df = df.iloc[df.index.str.extract('(\d+)').astype(int).squeeze().argsort()]