-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Wildcard for indexing with MultiIndex #371
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
Comments
from @jseabold:
|
I added a |
So this looks like
|
Thanks. level=0 has an issue in the example below Selecting on multiple levels can be done with chaining xs (but tricky on the level number of course, key/label referencing could help here). In [7]: df
Out[7]:
A B C D E
one two three four
a b 10.0032 5 -0.5109 -2.3358 -0.4645 0.05076 0.3640
a q 20 4 0.4473 1.4152 0.2834 1.00661 0.1744
x q 30 3 -0.6662 -0.5243 -0.3580 0.89145 2.5838
In [8]: df.xs('a', level=0)
Out[8]:
Empty DataFrame
Columns: Index([A, B, C, D, E], dtype=object)
Index: MultiIndex([], dtype=object)
In [9]: df.xs('a')
Out[9]:
A B C D E
two three four
b 10.0032 5 -0.5109 -2.336 -0.4645 0.05076 0.3640
q 20 4 0.4473 1.415 0.2834 1.00661 0.1744
In [10]: df.xs('q', level=1)
Out[10]:
A B C D E
one three four
a 20 4 0.4473 1.4152 0.2834 1.0066 0.1744
x 30 3 -0.6662 -0.5243 -0.3580 0.8915 2.5838
In [11]: df.xs('q', level=1).xs(4, level=2)
Out[11]:
A B C D E
one three
a 20 0.4473 1.415 0.2834 1.007 0.1744
In [12]: |
OK I fixed the bug you described. I also modified the parsers so that this works:
I'm gonna have a quick look to see if multi-level |
Look at me go:
|
You do really go :-) Excellent! |
* master: (313 commits) TST: more Python 2.5 sadness TST: Python 2.5 float formatting changed TST: cast to i8 when checking margins BUG: DataFrame.join on keys produce wrong result, does not preserve order DOC: release notes ENH: xs level can take multiple levels, pass multiple levels to MultiIndex.droplevel, GH pandas-dev#371 BUG: fix bugs related to comments in pandas-dev#371 BUG: fix TextParser with list buglet, enable parsing of DataFrame output with index names BUG: convert tuples in concat to MultiIndex BUG: don't lose index names when adding row margin ENH: add margins to crosstab ENH: add crosstab function and test ENH: crosstab prototype function, API needs fleshing out, GH pandas-dev#170 BUG: fix buglet with xs with level, GH pandas-dev#371 TST: add test_sql.py module TST: testing, cleanup of io.sql module TST: indexing testing with minor Series.__getitem__ refactoring ENH: hack toward pandas-dev#629 BUG: check for non-contiguous memory in SeriesGrouper, causing segfault ENH: add ability to pass list of dicts to DataFrame.append (GH pandas-dev#464) ...
Expose more calls in the BSONStore
based on mailing list discussion, adding here for better traceability.
Is there a way to use wild cards when using MultiIndex?
As an example, let`s work with the following DataFrame:
Selecting all rows where level 0 of the MultiIndex == 'a' is easy:
But what if i want to select all rows where level 3 of the MultiIndex == 'c', and i don`t care what the other levels are?
This is what can be done:
Wes idea is that this merits a new API function or adding to an existing one.
e.g. to modify DataFrame.xs to work like:
Other idea is to do something something similar as in numpy, and use : as wildcard.
For example in numpy:
y[:, :, 0] indexes all elements with "level 2" == 0.
This would translate to df.ix[:, :, 'c'] on the first example.
Another example on a DataFrames with MultIndex with a lot of levels and selecting on more than one level: df.ix[:, :, 'c', :, 10]
This approach looks familiar, and no other api method is needed for indexing when using wildcard.
The text was updated successfully, but these errors were encountered: