James Kang James Kang - 1 year ago 119
Python Question

Pandas Multiindex dataframe remove rows

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?

Answer Source

You can just use loc:

df.loc[['a']]

The resulting output:

       col
i1 i2     
a  c     5
   d     6
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download