pythonintermediate

Pandas Vectorised Operations vs Apply

Compare apply vs vectorised pandas operations for performance-critical column transformations.

python
import pandas as pd
import numpy as np

df = pd.DataFrame({'price': np.random.rand(1_000_000) * 100, 'qty': np.random.randint(1, 10, 1_000_000)})

# Fast: vectorised
df['revenue'] = df['price'] * df['qty']
df['tier'] = np.where(df['revenue'] > 500, 'high', 'low')
print(df.head())

Use Cases

  • feature engineering
  • column transformations
  • ETL pre-processing

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.