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.
)
&w=1200&q=75)
&w=1200&q=75)
&w=1200&q=75)