๐Ÿ€Zerve chosen as NCAA's Agentic Data Platform for 2026 Hackathonยท๐ŸงฎMeet the Zerve Team at Data Decoded Londonยท๐Ÿ“ˆWe're hiring โ€” awesome new roles just gone live!
Back
Pandas

How to Apply Function to Multiple Columns in Pandas

Answer

Use df[['col1', 'col2']].apply(func) to apply a function across multiple columns. Use axis=1 to apply row-wise (across columns), or axis=0 (default) to apply column-wise. For element-wise operations, use applymap() or vectorized operations.

Why This Happens

You often need to transform several columns the same way โ€” normalizing numeric columns, cleaning text fields, or calculating a new column from multiple inputs. Applying functions efficiently avoids slow Python loops and keeps your code clean.

Solution

The rule: use vectorized operations when possible (fastest), .apply() with axis=1 for row-wise logic, and avoid apply() with slow Python functions on large dataframes.

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'a': [1, 2, 3],
    'b': [4, 5, 6],
    'c': [7, 8, 9]
})

# โœ… Apply function to each column (column-wise, default)
df[['a', 'b']].apply(np.sum)  # sum of each column

# โœ… Apply function to each row (row-wise)
df[['a', 'b']].apply(lambda row: row['a'] + row['b'], axis=1)

# โœ… Simpler: just use vectorized operations when possible
df['total'] = df['a'] + df['b'] + df['c']

# โœ… Apply same transformation to multiple columns
cols = ['a', 'b', 'c']
df[cols] = df[cols].apply(lambda x: x * 2)

# โœ… Element-wise function with applymap (deprecated in newer pandas, use map)
df[['a', 'b']].applymap(lambda x: x ** 2)  # older pandas
df[['a', 'b']].map(lambda x: x ** 2)  # pandas 2.1+

# โœ… Create new column from multiple columns
df['result'] = df.apply(lambda row: row['a'] * row['b'] if row['c'] > 7 else 0, axis=1)

# โœ… Faster alternative: np.where for conditional logic
df['result'] = np.where(df['c'] > 7, df['a'] * df['b'], 0)

Better Workflow

In Zerve, you can run all three approaches (row-wise apply, column-wise apply, vectorized) as parallel blocks feeding from the same data source. Change your dataset size in one block, and only the downstream blocks re-run โ€” no re-executing everything from the top. The canvas view makes dependencies explicit, so you can instantly compare performance across methods.

Better workflow

Related Topics

Decision-grade data work

Explore, analyze and deploy your first project in minutes