Python / Data Science Essentials Interview Questions
What are the different ways to filter rows in a Pandas DataFrame?
Row filtering is one of the most frequent DataFrame operations. Pandas provides several syntaxes, each with different readability and performance trade-offs.
import pandas as pd
df = pd.DataFrame({
'city': ['NYC', 'LA', 'NYC', 'Chicago', 'LA'],
'revenue': [120, 85, 200, 55, 140],
'category': ['A', 'B', 'A', 'C', 'B'],
})
# Boolean indexing — most common
df[df['revenue'] > 100]
# Compound conditions — use & | ~ (not and/or)
df[(df['city'] == 'NYC') & (df['revenue'] > 100)]
# isin — membership test
df[df['city'].isin(['NYC', 'LA'])]
# between — inclusive range
df[df['revenue'].between(80, 150)]
# str methods for text filtering
df[df['city'].str.startswith('N')]
df[df['city'].str.contains('C', case=False)]
# query() — string-based, readable for complex conditions
df.query('city == "NYC" and revenue > 100')
df.query('revenue > @threshold', local_dict={'threshold': 100})
# @ prefix references a Python variable inside query string
# filter() — filter columns or index labels (NOT rows by content)
df.filter(like='rev') # columns whose name contains 'rev'
df.filter(regex='^c') # columns starting with 'c'query() is readable for ad-hoc analysis and slightly faster for very large DataFrames because it avoids creating the intermediate boolean array. However, it does not support all Python expressions and can be harder to debug. For production pipelines, explicit boolean indexing is more explicit and testable.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
