Python Pandas – How to select DataFrame rows on the basis of conditions

We can select DataFrame rows based on specific conditions using logical and relational operators. This is useful for filtering data to meet certain criteria.

Creating Sample Data

Let's create a DataFrame with car sales data to demonstrate conditional selection ?

import pandas as pd

# Create sample car sales data
data = {
    'Car': ['BMW', 'Lexus', 'Audi', 'Jaguar', 'Mustang', 'Lamborghini'],
    'Date_of_Purchase': ['10/10/2020', '10/12/2020', '10/17/2020', '10/16/2020', '10/19/2020', '10/22/2020'],
    'Reg_Price': [1000, 750, 750, 1500, 1100, 1000]
}

dataFrame = pd.DataFrame(data)
print("Original DataFrame:")
print(dataFrame)
Original DataFrame:
           Car Date_of_Purchase  Reg_Price
0          BMW        10/10/2020       1000
1        Lexus        10/12/2020        750
2         Audi        10/17/2020        750
3       Jaguar        10/16/2020       1500
4      Mustang        10/19/2020       1100
5  Lamborghini        10/22/2020       1000

Single Condition Selection

Select rows where registration price is less than 1000 using a relational operator ?

import pandas as pd

data = {
    'Car': ['BMW', 'Lexus', 'Audi', 'Jaguar', 'Mustang', 'Lamborghini'],
    'Date_of_Purchase': ['10/10/2020', '10/12/2020', '10/17/2020', '10/16/2020', '10/19/2020', '10/22/2020'],
    'Reg_Price': [1000, 750, 750, 1500, 1100, 1000]
}

dataFrame = pd.DataFrame(data)

# Select rows with registration price less than 1000
filtered_data = dataFrame[dataFrame.Reg_Price < 1000]
print("Cars with price less than 1000:")
print(filtered_data)
Cars with price less than 1000:
     Car Date_of_Purchase  Reg_Price
1  Lexus        10/12/2020        750
2   Audi        10/17/2020        750

Multiple Conditions

Use logical operators to combine multiple conditions ?

import pandas as pd

data = {
    'Car': ['BMW', 'Lexus', 'Audi', 'Jaguar', 'Mustang', 'Lamborghini'],
    'Date_of_Purchase': ['10/10/2020', '10/12/2020', '10/17/2020', '10/16/2020', '10/19/2020', '10/22/2020'],
    'Reg_Price': [1000, 750, 750, 1500, 1100, 1000]
}

dataFrame = pd.DataFrame(data)

# Multiple conditions: price between 750 and 1100
filtered_data = dataFrame[(dataFrame.Reg_Price >= 750) & (dataFrame.Reg_Price <= 1100)]
print("Cars with price between 750 and 1100:")
print(filtered_data)
Cars with price between 750 and 1100:
           Car Date_of_Purchase  Reg_Price
0          BMW        10/10/2020       1000
1        Lexus        10/12/2020        750
2         Audi        10/17/2020        750
4      Mustang        10/19/2020       1100
5  Lamborghini        10/22/2020       1000

Common Operators

Operator Description Example
== Equal to df[df.price == 1000]
< Less than df[df.price < 1000]
> Greater than df[df.price > 1000]
& Logical AND df[(df.price > 500) & (df.price < 1000)]
| Logical OR df[(df.car == 'BMW') | (df.car == 'Audi')]

Conclusion

Use square brackets with conditions to filter DataFrame rows. Combine multiple conditions with & (AND) and | (OR) operators. Always wrap individual conditions in parentheses when using multiple conditions.

Updated on: 2026-03-26T13:27:58+05:30

580 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements