在MultiIndex数据框中按特定的索引级别过滤行 [英] Filtering rows by a particular index level in a MultiIndex dataframe
问题描述
给出一个multIndex数据框:
Given a multIndex dataframe:
mux = pd.MultiIndex.from_arrays([
list('aaaabbbbbccdddddd'),
list('tuvwlmnopxyfghijk')
], names=['one', 'two'])
df = pd.DataFrame({'col': np.arange(len(mux))}, mux)
df
col
one two
a t 0
u 1
v 2
w 3
b l 4
m 5
n 6
o 7
p 8
c x 9
y 10
d f 11
g 12
h 13
i 14
j 15
k 16
是否可以保持对应于数据帧第0级的第i个值的行?
Is it possible to keep rows corresponding to upto the ith value of the 0th level of the dataframe?
对于i = 2,我的预期输出是:
For i = 2, my expected output is:
col
one two
a t 0
u 1
v 2
w 3
b l 4
m 5
n 6
o 7
p 8
请注意,仅保留与a和b有关的行,其他所有内容均被删除.我希望这个问题很清楚,但如果不是这样,请随时进行澄清.
Note that only rows pertaining to a and b are retained, everything else is dropped. I hope the problem is clear, but if it isn't, please feel free to ask for clarifications.
我尝试过:
idx = pd.IndexSlice
df.iloc[(idx[:2], slice(None))]
但是,这只给了我整个df中的前两行,而不是第0级中前两个值的所有行.
But that gives me only the first two rows in the entire df, not all rows of the first two values in the 0th level.
推荐答案
一种解决方法是返回第0级的索引值,然后使用这些值索引到原始数据帧中:
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 3
b l 4
m 5
n 6
o 7
p 8
如评论中所述,这仅适用于第0级,不适用于第1级.可能会有一个更通用的解决方案,可以与其他级别一起使用.
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.
这篇关于在MultiIndex数据框中按特定的索引级别过滤行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!