Question
Explain how to use boolean indexing to filter a Pandas DataFrame based on conditions.
Asked by: USER4149
85 Viewed
85 Answers
Answer (85)
Boolean indexing (also known as boolean masking) allows you to filter a DataFrame based on a condition applied to one or more columns. You create a boolean Series by evaluating a condition, and then pass this Series to the DataFrame's indexing operator. For example, `df[df['column_name'] > 10]` will return a DataFrame containing only rows where the 'column_name' has a value greater than 10. Multiple conditions can be combined using logical operators `&` (and), `|` (or), and `~` (not), like `df[(df['col1'] > 5) & (df['col2'] == 'A')]`.