Filtering Rows By A Particular Index Level In A Multiindex Dataframe
Given a multIndex dataframe: mux = pd.MultiIndex.from_arrays([ list('aaaabbbbbccdddddd'), list('tuvwlmnopxyfghijk') ], names=['one', 'two']) df = pd.DataFrame({'col': np.a
Solution 1:
One way to go about this is to return the index values for the 0th level and then index into the original data frame with those:
df.loc[df.index.levels[0][:2].values]
col
one two
a t 0
u 1
v 2
w 3b l 4
m 5
n 6
o 7p8
As mentioned in the comments this only works for the 0th level and not the 1st. There may be a more a generalizable solution that would work with other levels.
Post a Comment for "Filtering Rows By A Particular Index Level In A Multiindex Dataframe"