Which query generates the DataFrame 'df_orders' to show orders with "Quantity" greater than 10 and "Price" less than 50?
Which query generates the DataFrame 'df_orders' to show orders with "Quantity" greater than 10 and "Price" less than 50?
Solution
Filtering a DataFrame in pandas uses boolean indexing: df[condition]. To combine two conditions, each one goes in its own brackets and they are joined with & (AND). Option 2 does this, keeping only rows where Quantity is above 10 and Price is below 50.
Option 1: it has no df_orders[ ... ] around the condition, so it only builds a boolean Series (and has an unmatched bracket). It does not filter any rows.
Option 3: the comparison signs are reversed (<10, >50). Also, without brackets, & binds tighter than the comparison operators, so it raises an error.
Option 4: | means OR, so it returns rows meeting either condition, not both.
Always wrap each condition in parentheses when using & or | in pandas.
Related questions:
2025: 16 May Shift 1
2025: 3 June Shift 2
2026: 13 May Shift 2