I'm suspicious that this is trivial, but I yet to discover the incantation that will let me select rows from a Pandas dataframe based on the values of a hierarchical key. So, for example, imagine we have the following dataframe:
import pandas
df = pandas.DataFrame({'group1': ['a','a','a','b','b','b'],
'group2': ['c','c','d','d','d','e'],
'value1': [1.1,2,3,4,5,6],
'value2': [7.1,8,9,10,11,12]
})
df = df.set_index(['group1', 'group2'])
df looks as we would expect:
If df were not indexed on group1 I could do the following:
df['group1' == 'a']
But that fails on this dataframe with an index. So maybe I should think of this like a Pandas series with a hierarchical index:
df['a','c']
Nope. That fails as well.
So how do I select out all the rows where:
- group1 == 'a'
- group1 == 'a' & group2 == 'c'
- group2 == 'c'
- group1 in ['a','b','c']
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…