Skip to content

Add example section to groupby.rst and one example #6304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2014
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion doc/source/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
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]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be actual code, not a past of an ipython session (it WILL generate an ipython session as it goes)

e.g.

 df = DataFrame(.....)
df.groupby(df.sum(), axis=1).sum()

is all that's needed

and needs to be

.. ipython:: python


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