[英]Query Multiindex in Pandas Dataframe - Special indexing
I have a specific query for a multi-indexed dataframe which I cannot get my head around how I can achieve it.我有一个针对多索引 dataframe 的特定查询,我无法理解如何实现它。 The various explanations on different websites do unfortunately not help with this.
不幸的是,不同网站上的各种解释对此无济于事。 Working example:
工作示例:
col1 = ['30/01/2021','30/01/2021','31/01/2021','31/01/2021','01/02/2021','01/02/2021']
col2 = ['USD','EUR','USD','EUR','USD','EUR']
col3 = ['3M','1M','1M','3M','3M','6M']
col4 = [0.9,0.95,0.89,0.91,0.94,0.92]
df = pd.DataFrame(index = [col1, col2, col3], data = col4, columns = ['Value'])
I can now apply "simple" indexing like:我现在可以应用“简单”索引,例如:
df.loc[:,['USD','EUR'],:]
which gets me all entries for the currencies USD and EUR:这让我获得了货币美元和欧元的所有条目:
Value
30/01/2021 USD 3M 0.90
31/01/2021 USD 1M 0.89
01/02/2021 USD 3M 0.94
30/01/2021 EUR 1M 0.95
31/01/2021 EUR 3M 0.91
01/02/2021 EUR 6M 0.92
or或者
df.loc[:,['USD','EUR'],['1M','3M']]
which gets me all entries for the currencies USD and EUR that are 1M or 3M:这让我获得了 1M 或 3M 货币 USD 和 EUR 的所有条目:
Value
31/01/2021 USD 1M 0.89
30/01/2021 USD 3M 0.90
01/02/2021 USD 3M 0.94
30/01/2021 EUR 1M 0.95
31/01/2021 EUR 3M 0.91
However, what I actually want to have is all entries with (currency USD and time 1M) AND all entries with (currency EUR and time 3M).但是,我真正想要的是所有带有(货币美元和时间 1M)的条目和所有带有(货币欧元和时间 3M)的条目。
That is, I desire to have as result (that the date is the same is a coincidence of this example. So I cannot simply look for the date)也就是说,我希望得到结果(日期相同是这个例子的巧合。所以我不能简单地查找日期)
Value
31/01/2021 USD 1M 0.89
31/01/2021 EUR 3M 0.91
How can I achieve that?我怎样才能做到这一点? I tried various versions of tuples and lists combinations, but as said, couldn't figure it out.
我尝试了各种版本的元组和列表组合,但如前所述,无法弄清楚。
Thank you for any guidance.感谢您的任何指导。
You can create MultiIndex
by both lists by MultiIndex.from_arrays
and then match to second and third level by removing first level of original MultiIndex
by MultiIndex.droplevel
, test by Index.isin
and filter in boolean indexing
:您可以通过
MultiIndex
通过两个列表创建 MultiIndex ,然后通过MultiIndex.from_arrays
删除原始MultiIndex
的第一级,通过MultiIndex.droplevel
测试并在Index.isin
boolean indexing
中过滤来匹配第二和第三级:
a = ['USD','EUR']
b = ['1M','3M']
mux = pd.MultiIndex.from_arrays([a, b])
df = df[df.index.droplevel(0).isin(mux)]
print (df)
Value
31/01/2021 USD 1M 0.89
EUR 3M 0.91
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.