From 85cb20d0f7582e29140708e84c86876b56cc21aa Mon Sep 17 00:00:00 2001 From: unutbu Date: Mon, 21 Apr 2014 17:24:04 -0400 Subject: [PATCH] DOC: Add recipe for shifting groups of values based on the index --- doc/source/cookbook.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/doc/source/cookbook.rst b/doc/source/cookbook.rst index 41a270a37d187..5f0b65218d374 100644 --- a/doc/source/cookbook.rst +++ b/doc/source/cookbook.rst @@ -191,6 +191,19 @@ The :ref:`grouping ` docs. `Create a value counts column and reassign back to the DataFrame `__ +`Shift groups of the values in a column based on the index +`__ + +.. 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 ~~~~~~~~~~~~~~