本文介绍了如何使用ix索引到 pandas 多索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经设置了一些这样的代码:
I have setup some code like this:
import pandas as pd
import numpy as np
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'],
['aaa', 'bbb', 'ccc', 'ccc', 'ddd', 'eee', 'eee', 'eee' ]]
tuples = zip(*arrays)
index = pd.MultiIndex.from_tuples(tuples, names=['A', 'B', 'C'])
df = pd.DataFrame(np.random.randn(8, 4), index=index)
df
Out[161]:
0 1 2 3
A B C
bar one aaa 0.682220 -0.598889 -0.600635 -0.488069
two bbb -0.134557 1.614224 -0.191303 0.073813
baz one ccc -1.006877 -0.137264 -0.319274 1.465952
two ccc 0.107222 0.358468 0.165108 -0.258715
foo one ddd 0.360562 1.759095 -1.385394 -0.646850
two eee -1.113520 0.221483 2.226704 -0.994636
qux one eee -0.609271 -0.888330 0.824189 1.772536
two eee -0.008346 -0.688091 0.263303 1.242485
我想根据条件与A,B和C组的组合找到匹配的行.
I want find matching rows based on combinations of criteria with the groups A, B and C.
例如用sql术语:选择*其中A in('foo','qux')and C ='eee'
e.g. in sql terms: select * where A in ('foo', 'qux') and C='eee'
我可以用ix实现吗?例如像这样:
Can I acheive this with ix? e.g. something like:
df.ix(['foo', 'qux'],:,'eee')
对于非常大的数据集,实现此目标的理想方式是什么?
What is the idomatic way of achieving this for very large datasets?
(我目前正在使用pandas 0.7,但如果绝对必要可以升级)
(I'm currently using pandas 0.7 but can upgrade if absolutely necessary)
推荐答案
自熊猫0.14起,您可以将选择器的元组传递给df.loc
来切片MultiIndex:
As of Pandas 0.14, you can pass a tuple of selectors to df.loc
to slice a MultiIndex:
In [782]: df.loc[(['foo','qux'], slice(None), 'eee'), :]
Out[782]:
0 1 2 3
A B C
foo two eee 1.615853 -1.327626 0.772608 -0.406398
qux one eee 0.472890 0.746417 0.095389 -1.446869
two eee 0.711915 0.228739 1.763126 0.558106
这篇关于如何使用ix索引到 pandas 多索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!