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.
More Related questions...