Skip to content

Method for df #1766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gerigk opened this issue Aug 14, 2012 · 2 comments
Closed

Method for df #1766

gerigk opened this issue Aug 14, 2012 · 2 comments
Labels
Enhancement Ideas Long-Term Enhancement Discussions Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@gerigk
Copy link

gerigk commented Aug 14, 2012

See http://stackoverflow.com/questions/11941492/selecting-rows-from-a-pandas-dataframe-with-a-compound-hierarchical-index

let's say I want to filter a DataFrame such that level[k] only contains the values x, y or z.
A vectorized possibility is:

df[np.in1d(df.index.labels[k], match([x, y, z], df.index.levels[k]))]

but I guess it would be nice to have some pandas method wrapping it or implementing it more efficiently.

@lodagro
Copy link
Contributor

lodagro commented Sep 7, 2012

What about putting this in df.ix[]? And when indexing a MultiIndex a tuple needs to be used. For each of the tuple values the same rules apply as for a single level index (including slicing and boolean indexing). When no tuple is used to index a MultiIndex it must be an int and its a position (always) or a boolean index.

suppose df is a frame:
- with a four level MultiIndex on both axis
- level2 of axis0 has five distinct values.

df.ix[('a', slice[None), [True, True, False, False, False]), slice(3, 7, 2)]
from axis0 select rows with:
- level value = 'a'
- all level 1 values
- only two first values of level 2
- all level 3 values
from axis 1 select positions 3 and 5

Now one can already use a tuple but it is not enforced, without tuple indexing a Multiindex works, but not always (try indexing without tuple on more than two levels) Also when the first level is integer, and you want to select a single row one needs to specify all levels, which can be a lot of typing.

@jreback
Copy link
Contributor

jreback commented Sep 21, 2013

closing in favor of #2425

@jreback jreback closed this as completed Sep 21, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Ideas Long-Term Enhancement Discussions Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

3 participants