Applying Functions
Pandas Basics
1 min read
This section is 1 min read, full guide is 30 min read
Published Sep 29 2025, updated Aug 17 2026
21
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
PandasPython
What is .apply()?
.apply()applies a function to a Pandas object (Series or DataFrame).- It works elementwise, rowwise, or columnwise, depending on how you use it.
- It’s more flexible than vectorised operations, but often slower.
On a Series
When used on a Series, the function is applied elementwise:
import pandas as pds = pd.Series([1, 2, 3, 4])s.apply(lambda x: x**2)Output:
0 11 42 93 16dtype: int64On a DataFrame
When used on a DataFrame, you can choose axis:
axis=0(default) → apply function to each column.axis=1→ apply function to each row.
Column example:
df = pd.DataFrame({ "A": [1, 2, 3], "B": [10, 20, 30]})# Apply columnwise (axis=0)df.apply(sum, axis=0)Output:
A 6B 60dtype: int64Row example:
# Apply rowwise (axis=1)df.apply(lambda row: row["B"] - row["A"], axis=1)Output:
0 91 182 27dtype: int64Returning New Columns
You can assign results back to new columns:
df["Diff"] = df.apply(lambda row: row["B"] - row["A"], axis=1)Returning DataFrames
If your function returns a Series, .apply() can expand it into multiple columns:
def stats(row): return pd.Series({"Sum": row.sum(), "Mean": row.mean()})df_stats = df.apply(stats, axis=1)