0

Suppose the data is as follows

import pandas as pd
data = {'Sub_Id': ['SM01', 'SM02', 'SM03'], 'Sub_name': ['English', 'Physics', 'Maths'], 'Marks': ['62', '33', '86']}
table = pd.DataFrame(data)
print(table)

and the output is

  Sub_Id Sub_name Marks
0   SM01  English    62
1   SM02  Physics    33
2   SM03    Maths    86

I want to add an extra row which will give output as

  Sub_Id Sub_name Marks
0   SM01  English    62
1   SM02  Physics    33
2   SM03    Maths    86
3     Total_marks   181

Here the 4th row 1st element Total_marks is common to both Sub_Id and Sub_name

Is this possible through pandas or tabulate or Tableit?

  • Are you trying to merge, or merely add the column? – Peter S Apr 29 '20 at 03:30
  • 1
    ```table.loc[table.index[-1]+ 1,:] = ['Total_Marks','',table.Marks.astype(int).sum()]```? – sammywemmy Apr 29 '20 at 03:33
  • @PLZHELP Will merge and add produce different outputs? Talking about my problem I think it seems like merging – Pratik Pingale Apr 29 '20 at 03:35
  • Try sam's solution – Peter S Apr 29 '20 at 03:36
  • 1
    Think twice before doing this. This breaks the abstraction of your DataFrame. You cannot then again sum the `Marks` column. If you do any other analysis on this DataFrame will have to keep in mind the row that doesn't belong. – Steven Rumbalski Apr 29 '20 at 03:41
  • @sammywemmy this works but doesn't solve my problem. I'm trying to ask that is merging column for a specific row possible in python? Same as that of Excel. – Pratik Pingale Apr 29 '20 at 03:45
  • 1
    _Is this possible through pandas or tabulate or Tableit?_ Probably, yes. Please see [ask], [help/on-topic]. – AMC Apr 29 '20 at 03:49

0 Answers0