1

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
jpp
  • 159,742
  • 34
  • 281
  • 339
Magg_rs
  • 323
  • 2
  • 3
  • 12

2 Answers2

1

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()]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Yes . I wanted 9 rows , 19 rows and 9 rows to be added as I have already one. This is correct – Magg_rs Jun 24 '18 at 14:05
0

Using numpy.repeat, you can create a list of indices for rows you wish to append. Then feed to pd.DataFrame.loc and append to your original dataframe.

vals = ['30DD', '20DD', '10DD']
counts = [30, 20, 10]

df = df.append(df.loc[np.repeat(vals, counts)])
jpp
  • 159,742
  • 34
  • 281
  • 339