鉴于我有以下 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/