1

I have grouped sum data in a dataframe, by the following:

groups = df.groupby(['name'])

Now I can get the head of the groups by groups.head(2) which gives the first two rows.

But how do I get a group by a specific name? i.e. if I want the single group where the group name is 'ruby', I can't just do groups['ruby']

redrubia
  • 2,256
  • 6
  • 33
  • 47

1 Answers1

1

How about:

groups.get_group('name')

For more elaboration, see this related question

Community
  • 1
  • 1
8one6
  • 13,078
  • 12
  • 62
  • 84
  • 1
    You should provide an explanation, else people may misunderstand your reasoning, and mark your answer as 'low-quality' – New Alexandria Dec 19 '13 at 20:00
  • Thanks for the suggestion, @NewAlexandria. What would you add to a one-liner like this? Link to the docs? I think [this SO question](http://stackoverflow.com/questions/14734533/how-to-access-pandas-groupby-dataframe-by-key) is more complete than I could hope to be. – 8one6 Dec 19 '13 at 20:35
  • I usually just write something like "it sounds like you're trying to do X, and so ...." or "i looks like this is just a regular call based on the YYY standard lib." That way, if your assumptions are off, it's more transparent. Particularly where someone, 2 yr form now, may have slightly different assumptions — your errata will be clarifying. – New Alexandria Dec 19 '13 at 20:37
  • Actually, seeing that other answer, I would have just marked this a dupe. At least you got some rep in before the door was closed :) – New Alexandria Dec 19 '13 at 20:40