isakkarlsson
isakkarlsson

Reputation: 1109

Pandas: use array index all values

I want to select all rows with a particular index. My DataFrame look like this:

>>> df
                            Code
Patient Date                        
1       2003-01-12 00:00:00  a
        2003-02-13 00:00:00  b
        2003-02-14 00:00:00  ba
2       2001-1-17 22:00:00  z
        2002-1-21 00:00:00  d
        2003-1-21 00:00:00  a
        2005-12-1 00:00:00  ba

Selecting one of the first (Patient) index works:

>>> df.loc[1]
                            Code
Patient Date                        
1       2003-01-12 00:00:00  a
        2003-02-13 00:00:00  b
        2003-02-14 00:00:00  ba

But selecting multiple of the first (Patient) index does not:

>>> df.loc[[1, 2]]
                            Code
Patient Date                        
1       2003-01-12 00:00:00  a
2       2001-1-17 22:00:00  z

However, I would like to get the entire dataframe (as the result would be if [1,1,1,2] i.e, the original dataframe).

When using a single index it works fine. For example:

>>> df.reset_index().set_index("Patient").loc[[1, 2]]
                   Date     Code
Patient                          
1       2003-01-12 00:00:00  a
        2003-02-13 00:00:00  b
        2003-02-14 00:00:00  ba
2       2001-1-17 22:00:00  z
        2002-1-21 00:00:00  d
        2003-1-21 00:00:00  a
        2005-12-1 00:00:00  ba

TL;DR Why do I have to repeat the index when using multiple indexes but not when I use a single index?

EDIT: Apparently it can be done similar to:

>>> df.loc[df.index.get_level_values("Patient").isin([1, 2])]

But this seems quite dirty to me. Is this the way - or is any other, better, way possible?

Upvotes: 0

Views: 145

Answers (1)

isakkarlsson
isakkarlsson

Reputation: 1109

For Pandas verison 0.14 the recommended way, according to the above comment, is:

df.loc[([1,2],),:]

Upvotes: 1

Related Questions