Skip to content

Commit 6fe9539

Browse files
committed
Merge pull request #6917 from unutbu/cookbook-apply-shift
DOC: Add recipe for shifting groups of values based on the index
2 parents 5fd6e6c + 85cb20d commit 6fe9539

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

doc/source/cookbook.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ The :ref:`grouping <groupby>` docs.
191191
`Create a value counts column and reassign back to the DataFrame
192192
<http://stackoverflow.com/questions/17709270/i-want-to-create-a-column-of-value-counts-in-my-pandas-dataframe>`__
193193

194+
`Shift groups of the values in a column based on the index
195+
<http://stackoverflow.com/q/23198053/190597>`__
196+
197+
.. ipython:: python
198+
199+
df = pd.DataFrame(
200+
{u'line_race': [10L, 10L, 8L, 10L, 10L, 8L],
201+
u'beyer': [99L, 102L, 103L, 103L, 88L, 100L]},
202+
index=[u'Last Gunfighter', u'Last Gunfighter', u'Last Gunfighter',
203+
u'Paynter', u'Paynter', u'Paynter']); df
204+
205+
df['beyer_shifted'] = df.groupby(level=0)['beyer'].apply(lambda grp: grp.shift(1))
206+
df
194207
195208
Expanding Data
196209
~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)