Your boolean masks are boolean (obviously) so you can use boolean operations on them. The boolean operators include (but are not limited to) &, | which can combine your masks based on either an 'and' operation or an 'or' operation. In your specific case, you need an 'and' operation. So you simply write your mask like so:
mask = (data['value2'] == 'A') & (data['value'] > 4)
This ensures you are selecting those rows for which both conditions are simultaneously satisfied. By replacing the & with |, one can select those rows for which either of the two conditions can be satisfied. You can select your result as usual:
data[mask]
Although this question is answered by the answer to the question that ayhan points out in his comment, I thought that the OP was lacking the idea of boolean operations.
Answer from Kartik on Stack OverflowPandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.mask.html
pandas.DataFrame.mask — pandas 3.0.6 documentation
The mask method is an application of the if-then idiom. For each element in the caller, if cond is False the element is used; otherwise the corresponding element from other is used.
W3Schools
w3schools.com › python › pandas › ref_df_mask.asp
Pandas DataFrame mask() Method
import pandas as pd data = { "age": ... df.mask(df["age"] > 30) Try it Yourself » · The mask() method replaces the values of the rows where the condition evaluates to True....
12:49
Pandas Mask Explained in 10 Minutes – Cleaner, Smarter Data - ...
05:22
Python Trick: Automate Writing Masks for Pandas Filtering - YouTube
08:21
Search-and-replace Pandas values with "where" and "mask" - YouTube
01:36
05 Boolean Masks and String Matching in Pandas - YouTube
02:54
How to Use Boolean Masks on Multiple Columns in Pandas DataFrames ...
Pandas
pandas.pydata.org › pandas-docs › version › 1.5 › reference › api › pandas.DataFrame.mask.html
pandas.DataFrame.mask — pandas 1.5.3 documentation
The mask method is an application of the if-then idiom. For each element in the calling DataFrame, if cond is False the element is used; otherwise the corresponding element from the DataFrame other is used.
Medium
medium.com › @heyamit10 › understanding-the-mask-method-in-pandas-48b55840e679
Understanding the mask() Method in Pandas | by Hey Amit | Medium
March 6, 2025 - Think of mask() like a highlighter, but instead of marking important text, you’re covering up data that meets certain conditions. Example 1: Replacing All Negative Numbers with NaN · Sometimes, you might have negative values in your dataset that don’t make sense — like negative sales or negative age (unless we’re in a sci-fi movie). Here’s how you can replace all negative numbers with NaN (which stands for “Not a Number”): import pandas as pd import numpy as np # Sample DataFrame with negative numbers df = pd.DataFrame({'A': [2, -3, 5, -7], 'B': [4, -1, -6, 3]}) # Masking negative numbers and replacing them with NaN df_masked = df.mask(df < 0, np.nan) # Display the result print(df_masked)
Programiz
programiz.com › python-programming › pandas › methods › mask
Pandas mask()
The mask() method is used to replace values where certain conditions are met. The mask() method in Pandas is used to replace values where certain conditions are met.
Towards Data Science
towardsdatascience.com › home › latest › boolean masking with pandas
Boolean Masking with Pandas | Towards Data Science
January 29, 2025 - One of the topics in Miki Tebeka's excellent "Faster Pandas" __ course was how to use Boolean masks to filter data in Pandas. I wanted to practice what I had learned, so I updated a recent project to use Boolean masks. I think this is a very useful technique, so I wanted to share how I updated ...
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-dataframe-mask
Python | Pandas dataframe.mask() - GeeksforGeeks
November 19, 2018 - Syntax: DataFrame.mask(cond, other=nan, inplace=False, axis=None, level=None, errors='raise', try_cast=False, raise_on_error=None) Parameters : cond : Where cond is False, keep the original value. Where True, replace with corresponding value from other. If cond is callable, it is computed on the NDFrame and should return boolean NDFrame or array. The callable must not change input NDFrame (though pandas doesn’t check it).
Brettromero
brettromero.com › pandas-where-and-mask
Pandas: Where and Mask – Brett Romero
April 6, 2021 - The mask method is the reverse of where. It is the same concept but now it updates the rows that satisfy the condition, i.e. the rows that evaluate to True. For me this makes more sense as we are targeting the rows that we want to update.
Skytowner
skytowner.com › explore › pandas_dataframe_mask_method
Pandas DataFrame | mask method with Examples
Pandas DataFrame.mask(~) replaces all values in the DataFrame that pass a certain criteria with the desired value.
Delft Stack
delftstack.com › home › howto › python pandas › pandas mask
How to Mask in Pandas | Delft Stack
February 2, 2024 - The output of this masking is generally an object that is returned as true or false based on the condition. It can be understood as an advanced If-Else scheme for a data frame. However, we will first create a dummy data frame using dates_data, along with a few rows. import pandas as pd index = pd.date_range("2013-1-1", periods=100, freq="30Min") dates_data = pd.DataFrame(data=list(range(100)), columns=["value"], index=index) dates_data["value2"] = "Alpha" dates_data["value2"].loc[0:10] = "Beta"
w3resource
w3resource.com › pandas › dataframe › dataframe-mask.php
Pandas DataFrame: mask() function - w3resource
August 19, 2022 - Pandas DataFrame - mask() function: The mask() function is used to replace values where the condition is True.
Codepointtech
codepointtech.com › home › mastering data replacement: how to use the pandas mask() function
Mastering Data Replacement: How to Use the Pandas mask() Function - codepointtech.com
July 4, 2026 - At its heart, the .mask() function in Pandas is used to replace values in a DataFrame or Series where a specified condition is True. Think of it as painting over parts of your data that meet certain criteria.
Pandas
pandas.pydata.org › pandas-docs › version › 2.3.3 › reference › api › pandas.DataFrame.mask.html
pandas.DataFrame.mask — pandas 2.3.3 documentation
... Alignment axis if needed. For Series this parameter is unused and defaults to 0. ... Alignment level if needed. ... Same type as caller or None if inplace=True. ... Return an object of same shape as self. ... The mask method is an application of the if-then idiom.
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.DataFrame.mask.html
pyspark.pandas.DataFrame.mask — PySpark 4.2.0 documentation
DataFrame.mask(cond, other=nan)[source]# Replace values where the condition is True. Parameters · condboolean DataFrame · Where cond is False, keep the original value. Where True, replace with corresponding value from other. otherscalar, DataFrame · Entries where cond is True are replaced with corresponding value from other. Returns · DataFrame · Examples · >>> from pyspark.pandas.config import set_option, reset_option >>> set_option("compute.ops_on_diff_frames", True) >>> df1 = ps.DataFrame({'A': [0, 1, 2, 3, 4], 'B':[100, 200, 300, 400, 500]}) >>> df2 = ps.DataFrame({'A': [0, -1, -2, -3, -4], 'B':[-100, -200, -300, -400, -500]}) >>> df1 A B 0 0 100 1 1 200 2 2 300 3 3 400 4 4 500 >>> df2 A B 0 0 -100 1 -1 -200 2 -2 -300 3 -3 -400 4 -4 -500 ·
YouTube
youtube.com › watch
pandas.DataFrame[mask]: Filtering the DataFrame with Boolean Arrays (Masks) - YouTube
Learn how to program more practical cases at https://datons.ai/This tutorial shows you how to create boolean arrays (masks) to select parts of the pandas.Dat...
Published: January 4, 2023