[pandas] groupby reversed

1,996 views
Skip to first unread message

Neal Becker

unread,
Jan 27, 2013, 9:27:33 AM1/27/13
to pystat...@googlegroups.com
The order produced by this groupby is the reverse of what I wanted. Since I can
do

for case,res in stuff:
...

I thought then stuff is a sequence, and reversed should work. But it doesn't

df = create_df (results)
stuff = df.groupby (['esno','order'])

for case, res in reversed (stuff):

for case, res in reversed (stuff):
File "/home/nbecker/.local/lib/python2.7/site-
packages/pandas/core/groupby.py", line 1871, in __getitem__
raise KeyError(str(key))
KeyError: '5'

2 questions:

1. Why didn't this work?

2. What's a better way?

Wes McKinney

unread,
Jan 27, 2013, 10:05:48 AM1/27/13
to pystat...@googlegroups.com
I don't think GroupBy fully implements Python's iterator protocol to
work with reverse. You could do

for case, res in reversed(list(stuff)):
...

- Wes

Neal Becker

unread,
Jan 27, 2013, 10:10:39 AM1/27/13
to pystat...@googlegroups.com
Can I convince groupby to sort differently? The doc seemed a bit unclear.

Wes McKinney

unread,
Jan 27, 2013, 10:36:12 AM1/27/13
to pystat...@googlegroups.com
The sort=True/False option only controls whether each group key is
sorted or whether to use the observed order in the data. For example:

In [1]: s = Series(['d', 'c', 'd', 'c', 'b', 'a'])

In [2]: s.groupby(s).size()
Out[2]:
a 1
b 1
c 2
d 2

In [3]: s.groupby(s, sort=False).size()
Out[3]:
d 2
c 2
b 1
a 1


The keys are only descending in the last case because they first
appear in descending order in the data. I'm not opposed to different
iteration order options, but it would have to be "convince via pull
request".

- Wes
Reply all
Reply to author
Forward
0 new messages