Masking DataFrame Columns with MultiIndex Conditions Using Pandas

You can use the following code to set everything to 0, except for column A and B, and (quux, two), (corge, three) in index C:

mask = pd.DataFrame(True, index=df1.index, columns=df1.columns)
idx = pd.MultiIndex.from_tuples([
    ('C', 'quux', 'two'),
    ('C', 'corge', 'three')
])

mask.loc[idx, ['A', 'B']] = False

df1[mask] = 0

print(df1)

This will create a mask where the values in columns A and B at indices corresponding to (quux, two) and (corge, three) in index C are set to True, and all other values are set to False. The mask DataFrame is then used to select only the rows with these conditions.

Note that we’re passing a list of tuples for idx, which corresponds to the indices you specified in your question. This allows us to specify multiple indices at once, rather than having to do separate indexings for each one.


Last modified on 2023-07-01