Skip to content

Commit 2d04ec8

Browse files
committed
Update groupby.rst
1 parent b5b7436 commit 2d04ec8

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

doc/source/groupby.rst

+26-1
Original file line numberDiff line numberDiff line change
@@ -722,4 +722,29 @@ To see the order in which each row appears within its group, use the
722722
723723
df.groupby('A').cumcount()
724724
725-
df.groupby('A').cumcount(ascending=False) # kwarg only
725+
df.groupby('A').cumcount(ascending=False) # kwarg only
726+
727+
Examples
728+
~~~~~~~~
729+
730+
Regroup columns of a DataFrame according to their sum, and sum the aggregated ones.
731+
732+
.. ipython::
733+
734+
In [2]: dat = {'a':[1,0,0], 'b':[0,1,0], 'c':[1,0,0], 'd':[2,3,4]}
735+
736+
In [3]: df = pd.DataFrame(dat)
737+
738+
In [4]: df
739+
Out[4]:
740+
a b c d
741+
0 1 0 1 2
742+
1 0 1 0 3
743+
2 0 0 0 4
744+
745+
In [5]: df.groupby(df.sum(), axis=1).sum()
746+
Out[5]:
747+
1 9
748+
0 2 2
749+
1 1 3
750+
2 0 4

0 commit comments

Comments
 (0)