Skip to content

DOC: Add recipe for shifting groups of values based on the index #6917

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 1 commit into from
Apr 21, 2014
Merged
Changes from all commits
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
13 changes: 13 additions & 0 deletions doc/source/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ The :ref:`grouping <groupby>` docs.
`Create a value counts column and reassign back to the DataFrame
<http://stackoverflow.com/questions/17709270/i-want-to-create-a-column-of-value-counts-in-my-pandas-dataframe>`__

`Shift groups of the values in a column based on the index
<http://stackoverflow.com/q/23198053/190597>`__

.. ipython:: python

df = pd.DataFrame(
{u'line_race': [10L, 10L, 8L, 10L, 10L, 8L],
u'beyer': [99L, 102L, 103L, 103L, 88L, 100L]},
index=[u'Last Gunfighter', u'Last Gunfighter', u'Last Gunfighter',
u'Paynter', u'Paynter', u'Paynter']); df

df['beyer_shifted'] = df.groupby(level=0)['beyer'].apply(lambda grp: grp.shift(1))
df

Expanding Data
~~~~~~~~~~~~~~
Expand Down