[英]Pandas Multiindex dataframe remove rows
I have Multiiindex DF as follows: 我有Multiiindex DF如下:
tuples = list(zip(*[['a', 'a', 'b', 'b'], ['c', 'd', 'c', 'd']]))
index = pd.MultiIndex.from_tuples(tuples, names=['i1', 'i2'])
df = pd.DataFrame([5, 6, 7, 8], index=index[:4], columns=['col'])
col
i1 i2
a c 5
d 6
b c 7
d 8
Would like to keep rows whose index (level 0) is in 想保留索引(级别0)所在的行
idx_to_keep = ['a']
Should be a straightforward task, but I can't think of any other way than 应该是一个简单的任务,但我想不出任何其他方式
idx_to_drop = np.setdiff1d(pd.unique(df.index.levels[0]), idx_to_keep)
df.drop(idx_to_drop, inplace = True)
col
i1 i2
a c 5
d 6
Can I do better? 我可以做得更好吗?
One way is to use the index
method get_level_values()
: 一种方法是使用
index
方法get_level_values()
:
df
col
i1 i2
a c 5
d 6
b c 7
d 8
df[df.index.get_level_values(0).isin(idx_to_keep)]
col
i1 i2
a c 5
d 6
You can just use loc
: 你可以使用
loc
:
df.loc[['a']]
The resulting output: 结果输出:
col
i1 i2
a c 5
d 6
Let's use slice
我们来使用
slice
idx_to_keep = ['a']
df.loc[slice(*idx_to_keep,)]
Output: 输出:
col
i1 i2
a c 5
d 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.