python - 按索引列的条件过滤 pandas Dataframe 中的值

标签 python pandas

鉴于我有以下 pandas DataFrame:

arrays = [np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
          np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']),
          np.array([0.01, 0.2, 0.3, -0.5, 0.6, -0.7, -0.8, 0.9])]

tuples = list(zip(*arrays))
df_index = pd.MultiIndex.from_tuples(tuples, names=['A', 'B', 'measure'])

df = pd.DataFrame(np.random.randn(8, 4), index=df_index)
print(df)

如何过滤所有值,例如 measure 列(索引的一部分)大于 0.2?

我试过:

df.loc[:,:,0.1:0.9]

(以及它的其他变体,但我收到错误“IndexingError:太多索引器”

谢谢, 杰拉德

最佳答案

In [3]: df.query("measure > 0.2")
Out[3]:
                        0         1         2         3
A   B   measure
baz one 0.3      0.623507  0.602585 -0.792142  2.066095
foo one 0.6      0.138192 -0.159108 -1.796944  1.668463
qux two 0.9     -0.162210 -2.293951  0.602990  1.622783

In [6]: df.loc[pd.IndexSlice[:,:,0.200001:], :]
Out[6]:
                        0         1         2         3
A   B   measure
baz one 0.3      0.623507  0.602585 -0.792142  2.066095
foo one 0.6      0.138192 -0.159108 -1.796944  1.668463
qux two 0.9     -0.162210 -2.293951  0.602990  1.622783

关于python - 按索引列的条件过滤 pandas Dataframe 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46812673/

相关文章:

python - 南方(对于Django)可以将数据行插入数据库吗?

python - 使用 Pandas 查找具有 Null 的 2 列之间的差异

python - 将数据框传递给 pandas 中的 SQL 时,如何检查记录是否存在?

python - pd.cut 类别为 plt.xticklabels

python - 使用 zipfile 读取受密码保护的 Word 文档

python - 在Redis中存储html并保留引号

Python多重继承中的错误 "TypeError: could not convert X to Y"

python - 在Python中打印小数点到8位精度?

python - 重新采样时间序列并显示一天中的时间

Python Pandas - 加速 csv 连接