问题
I have Multiiindex DF as follows:
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
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?
回答1:
One way is to use the index
method 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
回答2:
You are looking for .xs:
df.xs('a', axis=0, level=0, drop_level=False)
Which gives:
col
i1 i2
a c 5
d 6
回答3:
You can just use loc
:
df.loc[['a']]
The resulting output:
col
i1 i2
a c 5
d 6
回答4:
Let's use slice
idx_to_keep = ['a']
df.loc[slice(*idx_to_keep,)]
Output:
col
i1 i2
a c 5
d 6
来源:https://stackoverflow.com/questions/45333949/pandas-multiindex-dataframe-remove-rows