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 ~~~~~~~~~~~~~~