如何在 Pandas 中查询 MultiIndex 索引列值 [英] How to query MultiIndex index columns values in pandas
问题描述
代码示例:
In [171]: A = np.array([1.1, 1.1, 3.3, 3.3, 5.5, 6.6])
In [172]: B = np.array([111, 222, 222, 333, 333, 777])
In [173]: C = randint(10, 99, 6)
In [174]: df = pd.DataFrame(zip(A, B, C), columns=['A', 'B', 'C'])
In [175]: df.set_index(['A', 'B'], inplace=True)
In [176]: df
Out[176]:
C
A B
1.1 111 20
222 31
3.3 222 24
333 65
5.5 333 22
6.6 777 74
现在,我想检索 A 值:
Q1:在 [3.3, 6.6] 范围内 - 预期返回值:[3.3, 5.5, 6.6] 或 [3.3, 3.3, 5.5, 6.6](最后包含在内),以及 [3.3, 5.5] 或[3.3, 3.3, 5.5] 如果不是.
Q2:在 [2.0, 4.0] 范围内 - 预期回报值:[3.3] 或 [3.3, 3.3]
Now, I want to retrieve A values:
Q1: in range [3.3, 6.6] - expected return value: [3.3, 5.5, 6.6] or [3.3, 3.3, 5.5, 6.6] in case last inclusive, and [3.3, 5.5] or [3.3, 3.3, 5.5] if not.
Q2: in range [2.0, 4.0] - expected return value: [3.3] or [3.3, 3.3]
对于任何其他 MultiIndex 维度都相同,例如 B 值:
Q3:在 [111, 500] 范围内重复,作为范围内的数据行数 - 预期返回值:[111, 222, 222, 333, 333]
Same for any other MultiIndex dimension, for example B values:
Q3: in range [111, 500] with repetitions, as number of data rows in range - expected return value: [111, 222, 222, 333, 333]
更正式:
让我们假设 T 是一个包含 A、B 和 C 列的表.该表包含 n 行.表格单元格是数字,例如 A double、B 和 C 整数.让我们创建一个表 T 的 DataFrame,让我们将它命名为 DF.让我们设置 DF 的 A 和 B 列索引(没有重复,即没有单独的 A 和 B 列作为索引,并作为数据分开),即在这种情况下的 A 和 B MultiIndex.
Let us assume T is a table with columns A, B and C. The table includes n rows. Table cells are numbers, for example A double, B and C integers. Let's create a DataFrame of table T, let us name it DF. Let's set columns A and B indexes of DF (without duplication, i.e. no separate columns A and B as indexes, and separate as data), i.e. A and B in this case MultiIndex.
问题:
- 如何编写对索引的查询,例如在标签区间 [120.0, 540.0] 中查询索引 A(或 B)?存在标签 120.0 和 540.0.我必须澄清,我只对作为查询响应的索引列表感兴趣!
- 如何相同,但如果标签 120.0 和 540.0 不存在,但有低于 120、高于 120 且低于 540 或高于 540 的值的标签?
- 如果 Q1 和 Q2 的答案是唯一的索引值,现在相同,但有重复,作为索引范围内的数据行数.
对于不是索引的列,我知道上述问题的答案,但在索引的情况下,经过在网络上的长期研究和对 pandas 的功能进行实验后,我没有成功.我现在看到的唯一方法(无需额外编程)是将 A 和 B 的副本作为除索引之外的数据列.
I know the answers to the above questions in the case of columns which are not indexes, but in the indexes case, after a long research in the web and experimentation with the functionality of pandas, I did not succeed. The only method (without additional programming) I see now is to have a duplicate of A and B as data columns in addition to index.
推荐答案
通过 MultiIndex 值查询df,例如 where (A > 1.7) 和 (B <666):
To query the df by the MultiIndex values, for example where (A > 1.7) and (B < 666):
In [536]: result_df = df.loc[(df.index.get_level_values('A') > 1.7) & (df.index.get_level_values('B') < 666)]
In [537]: result_df
Out[537]:
C
A B
3.3 222 43
333 59
5.5 333 56
因此,获取例如 'A' 索引值(如果仍然需要):
Hence, to get for example the 'A' index values, if still required:
In [538]: result_df.index.get_level_values('A')
Out[538]: Index([3.3, 3.3, 5.5], dtype=object)
问题是,在大型数据帧中,按索引选择的性能比已排序的常规行选择差 10%.而在重复性工作中,循环往复,延迟不断累积.见示例:
The problem is, that in large data frames the performance of by index selection worse by 10% than the sorted regular rows selection. And in repetitive work, looping, the delay accumulated. See example:
In [558]: df = store.select(STORE_EXTENT_BURSTS_DF_KEY)
In [559]: len(df)
Out[559]: 12857
In [560]: df.sort(inplace=True)
In [561]: df_without_index = df.reset_index()
In [562]: %timeit df.loc[(df.index.get_level_values('END_TIME') > 358200) & (df.index.get_level_values('START_TIME') < 361680)]
1000 loops, best of 3: 562 µs per loop
In [563]: %timeit df_without_index[(df_without_index.END_TIME > 358200) & (df_without_index.START_TIME < 361680)]
1000 loops, best of 3: 507 µs per loop
这篇关于如何在 Pandas 中查询 MultiIndex 索引列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!