ref: Ways to filter Pandas DataFrame by column values
-
Filter by Column Value:
- To select rows based on a specific column value, use the index chain method. For example, to filter rows where sales are over 300:
Python
greater_than = df[df['Sales'] > 300]
- This will return rows with sales greater than 300.
- To select rows based on a specific column value, use the index chain method. For example, to filter rows where sales are over 300:
Python
-
Filter by Multiple Conditions:
- Use the
&
(and) or|
(or) operators to filter based on multiple conditions. For instance: Pythonand_operator = df[(df['Sales'] > 300) & (df['Units'] > 20)] or_operator = df[(df['Sales'] > 300) | (df['Units'] > 20)]
and_operator
filters rows where sales are greater than 300 and units are greater than 20.or_operator
filters rows where either sales or units exceed the specified thresholds.
- Use the