TravisVOX
TravisVOX

Reputation: 21631

Working with set_index in Pandas DataFrame

Using an imported CSV file, I indexed the DataFrame like this...

 rdata.set_index(['race_date', 'track_code', 'race_number', 'horse_name'])

This is what a section of the DataFrame looks like...

 race_date  track_code race_number horse_name          work_date  work_track
 2007-08-24 BM         8           Count Me Twice     2007-05-31         PLN
                                   Count Me Twice     2007-06-09         PLN
                                   Count Me Twice     2007-06-16         PLN
                                   Count Me Twice     2007-06-23         PLN
                                   Count Me Twice     2007-08-05         PLN
                                   Judge's Choice     2007-06-07          BM
                                   Judge's Choice     2007-06-14          BM
                                   Judge's Choice     2007-07-08          BM
                                   Judge's Choice     2007-08-18          BM

Why isn't the 'horse_name' column being grouped like the date, track and race? Perhaps it's by design, thus how can I slice this larger DataFrame by race to have a new DataFrame with 'horse_name' as its index?

Upvotes: 9

Views: 20759

Answers (1)

Viktor Kerkez
Viktor Kerkez

Reputation: 46566

It's not a bug. This is exactly how it's intended to work.

DataFrame has to show show every single item in it's data. So if the index has one level, that level will be fully expanded. If it has two levels, first level will be grouped and the second will be fully expanded, if it has tree levels, first two will be grouped and the third will be expanded, and so on.

So this is why the horse name is not grouped. How would you be able to see all the items in the DataFrame if you group also by the horse name :)

Try doing:

 rdata.set_index(['race_date', 'track_code', 'race_number'])

or:

 rdata.set_index(['race_date', 'track_code'])

You'll see that the last level of the index is always fully expanded, to enable you to see all the items in the DataFrame.

Upvotes: 10

Related Questions