How do you select rows from a DataFrame 'df' where the 'Salary' column is greater than 50000?
How do you select rows from a DataFrame 'df' where the 'Salary' column is greater than 50000?
Solution
df['Salary'] > 50000 creates a Boolean Series with True for each row where Salary exceeds 50000 and False otherwise. Passing it inside df[...] keeps only the rows marked True. This is called Boolean indexing.
Option 1: select_rows() is not a pandas method.
Option 3: df["Salary>50000"] looks up a column literally named "Salary>50000" and raises a KeyError. Conditions work only as expressions, not as strings in [].
Option 4: rows() is not a pandas method. Row access is done with loc, iloc or Boolean indexing.
df.query("Salary > 50000") is another valid way to do this, because query() does accept the condition as a string.
Related questions:
2026: 31 May Shift 1
2025: 22 May Shift 1