Pandas · Combine conditions
Google Search Analytics
Course overviewGoogle Search · Filter and sort searches

Combine conditions

Combine row-level conditions with AND, OR, and NOT using Pandas Boolean operators.

Step 1 of 3 · Learn

Match both conditions

Use & when both conditions must be true. Put each comparison in parentheses; Python's and does not combine two Pandas Boolean Series.

indexteamscore
0A10
1B30
2A30
indexteamscore
2A30
Both conditions must be true on the same row.

Match either condition

Use | when either condition can match. Group an OR expression before combining it with another condition, so the extra condition applies to both alternatives.

indexteamscore
0A10
1B20
2C30
indexteamscore
0A10
1B20
Either condition can keep a row; a matching row appears only once.

Exclude a match

Use ~ to reverse a Boolean condition. Parentheses make clear which condition is being reversed; ~ is not Python's scalar not operator.

indexteamscore
0A10
1B20
2C30
indexteamscore
0A10
2C30
The matching B row is excluded; the other rows remain.
▷ Your turn

Return search_id, device, and response_ms for mobile searches taking more than 300 milliseconds. Both conditions must match. Preserve source row order and save the DataFrame as result.

LANGUAGEPython · Pandas

Loading Python and Pandas…

Run the code to see DataFrame results here.