From 948255b4835eae0eff850676aa6edc289f247ca6 Mon Sep 17 00:00:00 2001 From: sinhrks Date: Wed, 27 Jul 2016 07:30:04 +0900 Subject: [PATCH] DOC: Fix groupby nth --- pandas/core/groupby.py | 55 ++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 6179857978b7b..fffa57626d5f0 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -1205,32 +1205,55 @@ def nth(self, n, dropna=None): Examples -------- - >>> df = DataFrame([[1, np.nan], [1, 4], [5, 6]], columns=['A', 'B']) + + >>> df = pd.DataFrame({'A': [1, 1, 2, 1, 2], + ... 'B': [np.nan, 2, 3, 4, 5]}, columns=['A', 'B']) >>> g = df.groupby('A') >>> g.nth(0) - A B - 0 1 NaN - 2 5 6 + B + A + 1 NaN + 2 3.0 >>> g.nth(1) - A B - 1 1 4 + B + A + 1 2.0 + 2 5.0 >>> g.nth(-1) - A B - 1 1 4 - 2 5 6 + B + A + 1 4.0 + 2 5.0 + >>> g.nth([0, 1]) + B + A + 1 NaN + 1 2.0 + 2 3.0 + 2 5.0 + + Specifying ``dropna`` allows count ignoring NaN + >>> g.nth(0, dropna='any') - B - A - 1 4 - 5 6 + B + A + 1 2.0 + 2 3.0 NaNs denote group exhausted when using dropna - >>> g.nth(1, dropna='any') + >>> g.nth(3, dropna='any') B - A + A 1 NaN - 5 NaN + 2 NaN + + Specifying ``as_index=False`` in ``groupby`` keeps the original index. + + >>> df.groupby('A', as_index=False).nth(1) + A B + 1 1 2.0 + 4 2 5.0 """ if isinstance(n, int):