Get total of Pandas column Get total of Pandas column python python

Get total of Pandas column


You should use sum:

Total = df['MyColumn'].sum()print (Total)319

Then you use loc with Series, in that case the index should be set as the same as the specific column you need to sum:

df.loc['Total'] = pd.Series(df['MyColumn'].sum(), index = ['MyColumn'])print (df)         X  MyColumn      Y      Z0        A      84.0   13.0   69.01        B      76.0   77.0  127.02        C      28.0   69.0   16.03        D      28.0   28.0   31.04        E      19.0   20.0   85.05        F      84.0  193.0   70.0Total  NaN     319.0    NaN    NaN

because if you pass scalar, the values of all rows will be filled:

df.loc['Total'] = df['MyColumn'].sum()print (df)         X  MyColumn      Y      Z0        A        84   13.0   69.01        B        76   77.0  127.02        C        28   69.0   16.03        D        28   28.0   31.04        E        19   20.0   85.05        F        84  193.0   70.0Total  319       319  319.0  319.0

Two other solutions are with at, and ix see the applications below:

df.at['Total', 'MyColumn'] = df['MyColumn'].sum()print (df)         X  MyColumn      Y      Z0        A      84.0   13.0   69.01        B      76.0   77.0  127.02        C      28.0   69.0   16.03        D      28.0   28.0   31.04        E      19.0   20.0   85.05        F      84.0  193.0   70.0Total  NaN     319.0    NaN    NaN

df.ix['Total', 'MyColumn'] = df['MyColumn'].sum()print (df)         X  MyColumn      Y      Z0        A      84.0   13.0   69.01        B      76.0   77.0  127.02        C      28.0   69.0   16.03        D      28.0   28.0   31.04        E      19.0   20.0   85.05        F      84.0  193.0   70.0Total  NaN     319.0    NaN    NaN

Note: Since Pandas v0.20, ix has been deprecated. Use loc or iloc instead.


Another option you can go with here:

df.loc["Total", "MyColumn"] = df.MyColumn.sum()#         X  MyColumn      Y       Z#0        A     84.0    13.0    69.0#1        B     76.0    77.0   127.0#2        C     28.0    69.0    16.0#3        D     28.0    28.0    31.0#4        E     19.0    20.0    85.0#5        F     84.0   193.0    70.0#Total  NaN    319.0     NaN     NaN

You can also use append() method:

df.append(pd.DataFrame(df.MyColumn.sum(), index = ["Total"], columns=["MyColumn"]))

enter image description here


Update:

In case you need to append sum for all numeric columns, you can do one of the followings:

Use append to do this in a functional manner (doesn't change the original data frame):

# select numeric columns and calculate the sumssums = df.select_dtypes(pd.np.number).sum().rename('total')# append sums to the data framedf.append(sums)#         X  MyColumn      Y      Z#0        A      84.0   13.0   69.0#1        B      76.0   77.0  127.0#2        C      28.0   69.0   16.0#3        D      28.0   28.0   31.0#4        E      19.0   20.0   85.0#5        F      84.0  193.0   70.0#total  NaN     319.0  400.0  398.0

Use loc to mutate data frame in place:

df.loc['total'] = df.select_dtypes(pd.np.number).sum()df#         X  MyColumn      Y      Z#0        A      84.0   13.0   69.0#1        B      76.0   77.0  127.0#2        C      28.0   69.0   16.0#3        D      28.0   28.0   31.0#4        E      19.0   20.0   85.0#5        F      84.0  193.0   70.0#total  NaN     638.0  800.0  796.0


Similar to getting the length of a dataframe, len(df), the following worked for pandas and blaze:

Total = sum(df['MyColumn'])

or alternatively

Total = sum(df.MyColumn)print Total