From 2d04ec84fe977fe0d5428f712776017d09bc8aff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Mazi=C3=A8res?= Date: Sat, 8 Feb 2014 10:08:26 +0100 Subject: [PATCH 1/2] Update groupby.rst --- doc/source/groupby.rst | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/doc/source/groupby.rst b/doc/source/groupby.rst index 41539b5ce283e..d41a02ad19060 100644 --- a/doc/source/groupby.rst +++ b/doc/source/groupby.rst @@ -722,4 +722,29 @@ To see the order in which each row appears within its group, use the df.groupby('A').cumcount() - df.groupby('A').cumcount(ascending=False) # kwarg only \ No newline at end of file + df.groupby('A').cumcount(ascending=False) # kwarg only + +Examples +~~~~~~~~ + +Regroup columns of a DataFrame according to their sum, and sum the aggregated ones. + +.. ipython:: + + In [2]: dat = {'a':[1,0,0], 'b':[0,1,0], 'c':[1,0,0], 'd':[2,3,4]} + + In [3]: df = pd.DataFrame(dat) + + In [4]: df + Out[4]: + a b c d + 0 1 0 1 2 + 1 0 1 0 3 + 2 0 0 0 4 + + In [5]: df.groupby(df.sum(), axis=1).sum() + Out[5]: + 1 9 + 0 2 2 + 1 1 3 + 2 0 4 From 82e6d70619effeaa33697b5ae4461bf02b99ec88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Mazi=C3=A8res?= Date: Sat, 8 Feb 2014 21:57:21 +0100 Subject: [PATCH 2/2] Update groupby.rst Edit following this remark --- doc/source/groupby.rst | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/doc/source/groupby.rst b/doc/source/groupby.rst index d41a02ad19060..d2f60b791eb7c 100644 --- a/doc/source/groupby.rst +++ b/doc/source/groupby.rst @@ -729,22 +729,7 @@ Examples Regroup columns of a DataFrame according to their sum, and sum the aggregated ones. -.. ipython:: - - In [2]: dat = {'a':[1,0,0], 'b':[0,1,0], 'c':[1,0,0], 'd':[2,3,4]} - - In [3]: df = pd.DataFrame(dat) - - In [4]: df - Out[4]: - a b c d - 0 1 0 1 2 - 1 0 1 0 3 - 2 0 0 0 4 +.. ipython:: python - In [5]: df.groupby(df.sum(), axis=1).sum() - Out[5]: - 1 9 - 0 2 2 - 1 1 3 - 2 0 4 + df = pd.DataFrame({'a':[1,0,0], 'b':[0,1,0], 'c':[1,0,0], 'd':[2,3,4]}) + df.groupby(df.sum(), axis=1).sum()