I have this dataframe:
Data = {'name': ['a', 'b'],
'number1': [5, 3],
'number2': [3, 2]
}
df = pd.DataFrame(Data, columns = ['name','number1', 'number2'])
I want to add another row which would contain sums of columns number1
and number2
. So my desired result is this:
Data2 = {'name': ['a', 'b', 'total'],
'number1': [5, 3, 8],
'number2': [3, 2, 5]
}
df2 = pd.DataFrame(Data2, columns = ['name','number1', 'number2'])
I tried the solution offered in this thread: Pandas dataframe total row
But this line
df.loc["Total"] = df.sum()
also makes a sum of names so in the end I get Total
as my index and in names column I get a value ab
(because a+b). Is there a way to get a df
that looks exactly the same as my desired result df
?