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?
IIUC, you can do it this way, using select_types, assign, and pd.concat:
pd.concat([df.select_dtypes(include=np.number)
.sum()
.to_frame()
.T
.assign(Name='Total'),df])
Output:
Name y1 y2 y3
0 Total 12 14 15
1 Ben 1 2 3
2 Jane 4 5 6
3 Sarah 7 7 6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With