Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep column titles when inserting a sum row in Pandas dataframe

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?

like image 428
Alex Avatar asked Dec 14 '25 10:12

Alex


1 Answers

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
like image 167
Scott Boston Avatar answered Dec 15 '25 22:12

Scott Boston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!