Transform each value in a column by applying a formula element-by-element — here, rounding each price to the nearest ten. With pandas, Series.apply calls the function once per element and collects the results.

By hand

Loop over prices, compute scaled for each value via round(p / 10) * 10, and append to result. Each iteration shows both the intermediate scaled value and the growing result list.

naive.py
prices = [14, 27, 33, 56]
result = []
for p in prices:
    scaled = round(p / 10) * 10
    result.append(scaled)
print('RESULT:', result)

With pandas

df['price'].apply(lambda x: round(x / 10) * 10) returns a new Series with the transformed values. The index and dtype are preserved.

library.py
import pandas as pd
from dalib.display import set_display
set_display()

prices = [14, 27, 33, 56]
df = pd.DataFrame({'price': prices})
result = df['price'].apply(lambda x: round(x / 10) * 10)
print('index:', result.index.tolist())
print('values:', result.tolist())
print('dtype:', result.dtype)
print('RESULT:', result.tolist())
index: [0, 1, 2, 3]
values: [10, 30, 30, 60]
dtype: int64
RESULT: [10, 30, 30, 60]

Implementation notes

  • Series.apply is an elementwise API — it calls the Python function once per element, not as a vectorized kernel. For simple arithmetic, prefer a vectorized expression: df['price'] * 2 is faster than df['price'].apply(lambda x: x * 2) because it avoids per-element Python calls.
  • Use .apply when the transformation cannot be expressed as built-in vectorized operations — e.g. multi-step logic or calls to external functions.
  • Cross-reference: map-transform (python-data-basics) for the pure-Python version; map-values (this chapter) for dict-based remapping via Series.map.