pandas groupby sort within groups

What you want to do is actually again a groupby (on the result of the first groupby): sort and take the first three elements per group. Starting from the result of the first groupby: We group by the first level of the index: Then we want to sort (‘order’) each group and take the first … Read more

how to read json object in python

You should pass the file contents (i.e. a string) to json.loads(), not the file object itself. Try this: There’s also the json.load() function which accepts a file object and does the f.read() part for you under the hood.

Can’t get Python to import from a different folder

I believe you need to create a file called __init__.py in the Models directory so that python treats it as a module. Then you can do: You can include code in the __init__.py (for instance initialization code that a few different classes need) or leave it blank. But it must be there.

Beginner Python: Reading and writing to the same file

Updated Response: This seems like a bug specific to Windows – http://bugs.python.org/issue1521491. Quoting from the workaround explained at http://mail.python.org/pipermail/python-bugs-list/2005-August/029886.html the effect of mixing reads with writes on a file open for update is entirely undefined unless a file-positioning operation occurs between them (for example, a seek()). I can’t guess what you expect to happen, but seems most … Read more

Rotating a two-dimensional array in Python

Consider the following two-dimensional list: Lets break it down step by step: This list is passed into zip() using argument unpacking, so the zip call ends up being the equivalent of this: Hopefully the comments make it clear what zip does, it will group elements from each input iterable based on index, or in other words it groups the columns.