From e9c5d25521d2e78e098bad6be69199d7c0dc9000 Mon Sep 17 00:00:00 2001 From: unutbu Date: Mon, 3 Nov 2014 14:06:49 -0500 Subject: [PATCH] DOC: Select row with maximum value from each group --- doc/source/cookbook.rst | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/source/cookbook.rst b/doc/source/cookbook.rst index edff461d7989d..8378873db9a65 100644 --- a/doc/source/cookbook.rst +++ b/doc/source/cookbook.rst @@ -588,6 +588,19 @@ Unlike agg, apply's callable is passed a sub-DataFrame which gives you access to df['beyer_shifted'] = df.groupby(level=0)['beyer'].shift(1) df +`Select row with maximum value from each group +`__ + +.. ipython:: python + + df = pd.DataFrame({'host':['other','other','that','this','this'], + 'service':['mail','web','mail','mail','web'], + 'no':[1, 2, 1, 2, 1]}).set_index(['host', 'service']) + mask = df.groupby(level=0).agg('idxmax') + df_count = df.loc[mask['no']].reset_index() + df_count + + Expanding Data ************** @@ -1202,4 +1215,4 @@ of the data values: {'height': [60, 70], 'weight': [100, 140, 180], 'sex': ['Male', 'Female']}) - df \ No newline at end of file + df