pandas sum

Solutions on MaxInterview for pandas sum by the best coders in the world

showing results for - "pandas sum"
Asad
07 Aug 2016
1df.loc['Total'] = pd.Series(df['MyColumn'].sum(), index = ['MyColumn'])
2print (df)
3         X  MyColumn      Y      Z
40        A      84.0   13.0   69.0
51        B      76.0   77.0  127.0
62        C      28.0   69.0   16.0
73        D      28.0   28.0   31.0
84        E      19.0   20.0   85.0
95        F      84.0  193.0   70.0
10Total  NaN     319.0    NaN    NaN
11
Salvatore
17 Sep 2018
1import pandas as pd
2
3data = {'Month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
4        'Bill Commission': [1500,2200,3500,1800,3000,2800],
5        'Maria Commission': [3200,4100,2500,3000,4700,3400], 
6        'Jack Commission': [1700,3100,3300,2700,2400,3100]
7        }
8
9df = pd.DataFrame(data,columns=['Month','Bill Commission','Maria Commission','Jack Commission'])
10sum_column = df.sum(axis=0)
11print (sum_column)
12
Jimmy
25 Oct 2016
1# select numeric columns and calculate the sums
2sums = df.select_dtypes(pd.np.number).sum().rename('total')
3
4# append sums to the data frame
5df.append(sums)
6#         X  MyColumn      Y      Z
7#0        A      84.0   13.0   69.0
8#1        B      76.0   77.0  127.0
9#2        C      28.0   69.0   16.0
10#3        D      28.0   28.0   31.0
11#4        E      19.0   20.0   85.0
12#5        F      84.0  193.0   70.0
13#total  NaN     319.0  400.0  398.0
14
Ismael
09 Nov 2017
1df.at['Total', 'MyColumn'] = df['MyColumn'].sum()
2print (df)
3         X  MyColumn      Y      Z
40        A      84.0   13.0   69.0
51        B      76.0   77.0  127.0
62        C      28.0   69.0   16.0
73        D      28.0   28.0   31.0
84        E      19.0   20.0   85.0
95        F      84.0  193.0   70.0
10Total  NaN     319.0    NaN    NaN
11
Miguel
11 Apr 2017
1>>> sum(ugds)16200904.0>>> ugds.sum()16200904.0
Bryan
05 May 2017
1import pandas as pd
2
3data = {'Month': ['Jan ','Feb ','Mar ','Apr ','May ','Jun '],
4        'Bill Commission': [1500,2200,3500,1800,3000,2800],
5        'Maria Commission': [3200,4100,2500,3000,4700,3400], 
6        'Jack Commission': [1700,3100,3300,2700,2400,3100]
7        }
8
9df = pd.DataFrame(data,columns=['Month','Bill Commission','Maria Commission','Jack Commission'])
10print (df)
11