Python Pandas - Select a subset of rows from a dataframe

Selecting subsets of rows from a DataFrame is a fundamental operation in Pandas. You can filter rows using boolean conditions to extract data that meets specific criteria.

Basic Row Selection with Conditions

Use boolean indexing with square brackets to filter rows. The condition returns a boolean Series that selects matching rows ?

import pandas as pd

# Create sample data
data = {
    'Car': ['BMW', 'Lexus', 'Audi', 'Jaguar', 'Mustang'],
    'Reg_Price': [2500, 3500, 2500, 2000, 2500],
    'Units': [100, 80, 120, 70, 110]
}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
Original DataFrame:
       Car  Reg_Price  Units
0      BMW       2500    100
1    Lexus       3500     80
2     Audi       2500    120
3   Jaguar       2000     70
4  Mustang       2500    110

Filtering by Single Condition

Select cars with Units greater than 100 ?

import pandas as pd

# Create sample data
data = {
    'Car': ['BMW', 'Lexus', 'Audi', 'Jaguar', 'Mustang'],
    'Reg_Price': [2500, 3500, 2500, 2000, 2500],
    'Units': [100, 80, 120, 70, 110]
}
df = pd.DataFrame(data)

# Filter cars with Units > 100
high_units = df[df["Units"] > 100]
print("Cars with Units > 100:")
print(high_units)
Cars with Units > 100:
       Car  Reg_Price  Units
2     Audi       2500    120
4  Mustang       2500    110

Filtering by Another Condition

Select cars with Reg_Price less than 3000 ?

import pandas as pd

# Create sample data
data = {
    'Car': ['BMW', 'Lexus', 'Audi', 'Jaguar', 'Mustang'],
    'Reg_Price': [2500, 3500, 2500, 2000, 2500],
    'Units': [100, 80, 120, 70, 110]
}
df = pd.DataFrame(data)

# Filter cars with Reg_Price < 3000
affordable_cars = df[df["Reg_Price"] < 3000]
print("Cars with Reg_Price < 3000:")
print(affordable_cars)
Cars with Reg_Price < 3000:
       Car  Reg_Price  Units
0      BMW       2500    100
2     Audi       2500    120
3   Jaguar       2000     70
4  Mustang       2500    110

Multiple Conditions

Combine conditions using & (and) or | (or). Wrap each condition in parentheses ?

import pandas as pd

# Create sample data
data = {
    'Car': ['BMW', 'Lexus', 'Audi', 'Jaguar', 'Mustang'],
    'Reg_Price': [2500, 3500, 2500, 2000, 2500],
    'Units': [100, 80, 120, 70, 110]
}
df = pd.DataFrame(data)

# Multiple conditions: affordable AND high units
result = df[(df["Reg_Price"] < 3000) & (df["Units"] > 100)]
print("Affordable cars with high units:")
print(result)
Affordable cars with high units:
       Car  Reg_Price  Units
2     Audi       2500    120
4  Mustang       2500    110

Common Methods

Method Syntax Use Case
Single condition df[df['col'] > value] Basic filtering
Multiple conditions (AND) df[(condition1) & (condition2)] All conditions must be true
Multiple conditions (OR) df[(condition1) | (condition2)] Any condition can be true

Conclusion

Use boolean indexing with square brackets to filter DataFrame rows. Combine multiple conditions with & or | operators, wrapping each condition in parentheses for proper evaluation.

Updated on: 2026-03-26T13:34:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements