1

This is my data frame

                                               A         B       C  D 
0                                    TG-bck-full       3648   10064  0
1    pNLRep2-Caprh74kan.SGR.(S.30MAY19.N.4Dec19)      10726    4083  0
2           pHELP-KanV4.SGR.(S.04MAY19.N.4Dec19)      13269    1795  0
3                                              1  248956422     248  0
4                                             10  133797422     120  0
..                                           ...        ...     ... ..
196                                   KI270394.1        970       0  0
197                            AY601635.1:1-4344       4344       2  0
198                                   CP011113.2    4587291       9  0
199                                     cassette       4981  164534  0
200                                            *          0       0  0

I need to build a code so that Row 3-196 are collapsed into one row, and the sum of B and C should be in that one row. So, in the end, it should look like

                                               A         B          C  D 
0                                    TG-bck-full       3648        10064  0
1    pNLRep2-Caprh74kan.SGR.(S.30MAY19.N.4Dec19)      10726        4083  0
2           pHELP-KanV4.SGR.(S.04MAY19.N.4Dec19)      13269        1795  0
3           (Not sure what would come here)            3099755062   25021
4                              AY601635.1:1-4344       4344        2  0
5                                     CP011113.2    4587291        9  0
6                                       cassette       4981        164534  0
7                                              *          0        0  0

Please note the sum of the rows should be only the values between rows 3-196

1 Answer 1

0

Use concat with select rows by DataFrame.loc:

df = pd.concat([df.loc[:2], df.loc[3:196].sum().to_frame().T.assign(A='All'), df.loc[187:]])
1
  • 1
    Thanks a lot, this really made my work easier Commented Dec 21, 2021 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.