I am going to go further and say ... "No", I donβt think there is a direct way to do this, the pandastic way (and the pythonic one too) should be explicit:
pd.DataFrame(df.sum(), columns=['sum'])
or, more elegantly, using a dictionary (remember that this copies the summed array):
pd.DataFrame({'sum': df.sum()})
As @root notes that it uses faster:
pd.DataFrame(np.sum(df.values, axis=0), columns=['sum'])
(Since zen of python claims: "practicality is superior to cleanliness," so if you care about this time, use this.)
However, perhaps the most pandastic way is to simply use the series! :)
.
A bit of %timeit for your tiny example:
In [11]: %timeit pd.DataFrame(df.sum(), columns=['sum']) 1000 loops, best of 3: 356 us per loop In [12]: %timeit pd.DataFrame({'sum': df.sum()}) 1000 loops, best of 3: 462 us per loop In [13]: %timeit pd.DataFrame(np.sum(df.values, axis=0), columns=['sum']) 1000 loops, best of 3: 205 us per loop
and for a bit more:
In [21]: df = pd.DataFrame(np.random.randn(100000, 3), columns=list('abc')) In [22]: %timeit pd.DataFrame(df.sum(), columns=['sum']) 100 loops, best of 3: 7.99 ms per loop In [23]: %timeit pd.DataFrame({'sum': df.sum()}) 100 loops, best of 3: 8.3 ms per loop In [24]: %timeit pd.DataFrame(np.sum(df.values, axis=0), columns=['sum']) 100 loops, best of 3: 2.47 ms per loop