Home > Mobile >  Grouping certain rows in a column then repeat them in pandas
Grouping certain rows in a column then repeat them in pandas

Time:03-03

I wish to repeat the rows from 11 to 17 many times, but I want that every repeat have 1 in column 'F' as you see in the desired dataframe. I thoght about Groupby() But I am not sure, which cretria I should pass to groupby() I want to have control over three columns.

     B     C    D  E  F  J
0    1    CM  MEG  A  1  C
1    2   HM1  MEG  A  1  H
2    3   HM2  MEG  A  1  H
3    4   HM3  MEG  A  1  H
4    5    OM  MEG  A  1  O
5    6    CA  MEG  A  1  C
6    7   HA1  MEG  A  1  H
7    8   HA2  MEG  A  1  H
8    9    CB  MEG  A  1  C
9   10   HB1  MEG  A  1  H
10  11   HB2  MEG  A  1  H
11  12   OEE   EG  A  2  O
12  13   CAE   EG  A  2  C
13  14  HA1E   EG  A  2  H
14  15  HA2E   EG  A  2  H
15  16   CBE   EG  A  2  C
16  17  HB1E   EG  A  2  H
17  18  HB2E   EG  A  2  H
18  19    OE  EGH  A  3  O
19  20    CA  EGH  A  3  C
20  21   HA1  EGH  A  3  H
21  22   HA2  EGH  A  3  H
22  23    CB  EGH  A  3  C
23  24   HB1  EGH  A  3  H
24  25   HB2  EGH  A  3  H

DISIRED DATAFRAME:

   B     C    D  E  F  J
0    1    CM  MEG  A  1  C
1    2   HM1  MEG  A  1  H
2    3   HM2  MEG  A  1  H
3    4   HM3  MEG  A  1  H
4    5    OM  MEG  A  1  O
5    6    CA  MEG  A  1  C
6    7   HA1  MEG  A  1  H
7    8   HA2  MEG  A  1  H
8    9    CB  MEG  A  1  C
9   10   HB1  MEG  A  1  H
10  11   HB2  MEG  A  1  H
11  12   OE   EG  A  2  O
12  13   CA   EG  A  2  C
13  14  HA1   EG  A  2  H
14  15  HA2   EG  A  2  H
15  16   CB   EG  A  2  C
16  17  HB1   EG  A  2  H
17  18  HB2   EG  A  2  H

18  19   OE   EG  A  3  O
19  20   CA   EG  A  3  C
20  21  HA1   EG  A  3  H
21  22  HA2   EG  A  3  H
22  23   CB   EG  A  3  C
23  24  HB1   EG  A  3  H
24  25  HB2   EG  A  3  H

25  26   OE   EG  A  4  O
26  27   CA   EG  A  4  C
27  28  HA1   EG  A  4  H
28  29  HA2   EG  A  4  H
29  30   CB   EG  A  4  C
30  31  HB1   EG  A  4  H
31  32  HB2   EG  A  4  H

32  33    OE  EGH  A  5  O
33  34    CA  EGH  A  5  C
34  35   HA1  EGH  A  5  H
35  36   HA2  EGH  A  5  H
36  37    CB  EGH  A  5  C
37  38   HB1  EGH  A  5  H
38  39   HB2  EGH  A  5  H

CodePudding user response:

IIUC, you could do:

start = 11
stop  = 17
repeat = 3

d = df.iloc[start:stop 1]

pd.concat([df.iloc[:start]] 
          [d.assign(F=d['F'] i) for i in range(repeat)] 
          [df.iloc[stop:]], ignore_index=True
         )

output:

     B     C    D  E  F  J
0    1    CM  MEG  A  1  C
1    2   HM1  MEG  A  1  H
2    3   HM2  MEG  A  1  H
3    4   HM3  MEG  A  1  H
4    5    OM  MEG  A  1  O
5    6    CA  MEG  A  1  C
6    7   HA1  MEG  A  1  H
7    8   HA2  MEG  A  1  H
8    9    CB  MEG  A  1  C
9   10   HB1  MEG  A  1  H
10  11   HB2  MEG  A  1  H
11  12   OEE   EG  A  2  O
12  13   CAE   EG  A  2  C
13  14  HA1E   EG  A  2  H
14  15  HA2E   EG  A  2  H
15  16   CBE   EG  A  2  C
16  17  HB1E   EG  A  2  H
17  18  HB2E   EG  A  2  H
18  12   OEE   EG  A  3  O
19  13   CAE   EG  A  3  C
20  14  HA1E   EG  A  3  H
21  15  HA2E   EG  A  3  H
22  16   CBE   EG  A  3  C
23  17  HB1E   EG  A  3  H
24  18  HB2E   EG  A  3  H
25  12   OEE   EG  A  4  O
26  13   CAE   EG  A  4  C
27  14  HA1E   EG  A  4  H
28  15  HA2E   EG  A  4  H
29  16   CBE   EG  A  4  C
30  17  HB1E   EG  A  4  H
31  18  HB2E   EG  A  4  H
32  18  HB2E   EG  A  2  H
33  19    OE  EGH  A  3  O
34  20    CA  EGH  A  3  C
35  21   HA1  EGH  A  3  H
36  22   HA2  EGH  A  3  H
37  23    CB  EGH  A  3  C
38  24   HB1  EGH  A  3  H
39  25   HB2  EGH  A  3  H
  • Related