Consider the following example data:
data = {"Taxon": ["Firmicutes"]*5,
"Patient": range(5),
"Tissue": np.random.randint(0, 1000, size=5),
"Stool": np.random.randint(0, 1000, size=5)}
df = pd.DataFrame(data).set_index(["Taxon", "Patient"])
print(df)
Stool Tissue
Taxon Patient
Firmicutes 0 740 389
1 786 815
2 178 265
3 841 484
4 211 534
So, How can I query the dataframe only with the second level index Patient
only? For example, I'd like to know all the data with respect to Patient 2
.
I've tried data[data.index.get_level_values(1)==2]
, and it worked fine. But is there any way to achieve the same with one these (loc
,iloc
or ix
) indexing methods?