Flag values that fall outside an allowed band. Given a list of numbers and bounds [lo, hi], mark each value True if it is below lo or above hi, False otherwise. By hand, evaluate the two-sided condition with or in a loop. With pandas, combine two boolean Series with | to produce the same flags in one expression.

By hand

With pandas

Build two boolean Series — df['x'] < lo flags values below the lower bound, df['x'] > hi flags values above the upper bound — then combine them with |. Parentheses around each comparison are required because | has higher operator precedence than < and >.

naive.py
values = [3, 15, 7, 42, 11, 2, 28, 9]
lo = 5
hi = 25
result = []
for v in values:
    result.append(v < lo or v > hi)
print('RESULT:', result)
library.py
import pandas as pd
from dalib.display import set_display
set_display()

values = [3, 15, 7, 42, 11, 2, 28, 9]
lo = 5
hi = 25
df = pd.DataFrame({'x': values})
flags = (df['x'] < lo) | (df['x'] > hi)
result = flags.tolist()
print('index:', flags.index.tolist())
print('dtype:', flags.dtype)
print('values:', flags.tolist())
print('RESULT:', result)
index: [0, 1, 2, 3, 4, 5, 6, 7]
dtype: bool
values: [True, False, False, True, False, True, True, False]
RESULT: [True, False, False, True, False, True, True, False]

Implementation notes

  • Parentheses around each comparison are mandatory: (df['x'] < lo) | (df['x'] > hi). Without them, Python parses the expression incorrectly because | binds tighter than < and >.
  • To flag values within the band instead, negate with ~flags, or use (df['x'] >= lo) & (df['x'] <= hi) directly.
  • Cross-reference: combine-two-masks (numpy chapter) for the equivalent np.logical_or approach.