I have a dataframe:
Name y1 y2 y3
1 Ben 01 02 03
2 Jane 04 05 06
3 Sarah 07 07 06
I am trying to add in a row in my dataframe which provides a total of the rows in each column. My code is:
import pandas as pd
df = pd.DataFrame(np.insert(df.values, 0, values=[df.sum(axis=0)], axis=0))
df.set_value(0, 0,'total')
df.head()
This is successful, but also removes my column names like this:
0 1 2 3
0 Total 12 14 15
1 Ben 01 02 03
2 Jane 04 05 06
3 Sarah 07 07 06
rather than returning this as desired:
Name y1 y2 y3
0 Total 12 14 15
1 Ben 01 02 03
2 Jane 04 05 06
3 Sarah 07 07 06
I have tried inserting
Index(['Name'], name=df.index.name)
to
df = pd.DataFrame(np.insert(df.values, 0, values=[df.sum(axis=0)], Index(['Name'], name=df.index.name) axis=0))
but this just returns the error
TypeError: unhashable type: 'Index'
Where am I going wrong?