Back To Blog Posts
Column And Row Sums In Pandas And Numpy
I feel like I am constantly looking it up, so now it is documented:
If you want to do a row sum in pandas, given the dataframe df:
df.sum(axis=1)
and a column sum:
df.sum(axis=0)
If you want to do a row sum in numpy[1], given the matrix X:
import numpy as np
np.sum(X,axis=1)
and column sums:
import numpy as np
np.sum(X,axis=0)
[1] http://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html