Let's say you have a dataframe of the form:
Jan Feb Mar Apr May Jun
Budget
Milk 10 20 31 52 7 11
Eggs 1 5 1 16 4 58
Bread 22 36 17 8 21 16
Butter 4 5 8 11 36 2
And you would like to add a Total row at the bottom which contains the sum of the columns. This is how I would do this task.
# Append a new row containing sum of each column
df.append(pd.Series(df.sum(numeric_only= True),name='Total'))
This will produce the dataframe of the following format.
Jan Feb Mar Apr May Jun
Budget
Milk 10 20 31 52 7 11
Eggs 1 5 1 16 4 58
Bread 22 36 17 8 21 16
Butter 4 5 8 11 36 2
Total 37 66 57 87 68 87
As was pointed out by @kevin-choon-liang-yew, append is scheduled for depreciation, in which case you would need to use the concat
function to append a new row as shown below:
pd.concat([df, pd.DataFrame(pd.Series(df.sum(numeric_only= True)).to_dict(),index=['Total'])])
returning the final dataframe.