0
0
Pandasdata~10 mins

apply() on rows (axis=1) in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - apply() on rows (axis=1)
Start with DataFrame
Define function to apply
Call apply() with axis=1
Function runs on each row
Collect results into Series or DataFrame
Use or assign results
This flow shows how apply() runs a function on each row of a DataFrame when axis=1 is set.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

result = df.apply(lambda row: row['A'] + row['B'], axis=1)
This code adds values in columns A and B for each row using apply() with axis=1.
Execution Table
StepRow IndexRow DataFunction AppliedResult
10{'A': 1, 'B': 4}1 + 45
21{'A': 2, 'B': 5}2 + 57
32{'A': 3, 'B': 6}3 + 69
4--All rows processedSeries([5, 7, 9])
💡 All rows processed, apply() returns a Series with results.
Variable Tracker
VariableStartAfter 1After 2After 3Final
rowNoneA 1 B 4 Name: 0, dtype: int64A 2 B 5 Name: 1, dtype: int64A 3 B 6 Name: 2, dtype: int64None
resultEmpty579Series([5, 7, 9])
Key Moments - 3 Insights
Why do we use axis=1 in apply()?
axis=1 tells apply() to run the function on each row, not on each column. See execution_table steps 1-3 where each row is processed.
What type of object is passed to the function inside apply()?
Each row is passed as a Series object representing that row's data, as shown in execution_table column 'Row Data'.
What does apply() return when used with axis=1?
It returns a Series with the function results for each row, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of the function applied on row index 1?
A5
B9
C7
D6
💡 Hint
Check the 'Result' column for row index 1 in execution_table step 2.
At which step does apply() finish processing all rows?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look at the exit_note and the last row in execution_table.
If axis=0 was used instead of axis=1, how would the function be applied?
AOn each column
BOn the entire DataFrame at once
COn each row
DIt would cause an error
💡 Hint
axis=0 means apply function on columns, not rows.
Concept Snapshot
apply() with axis=1 runs a function on each row of a DataFrame.
The function receives a row as a Series.
Results are collected into a Series or DataFrame.
Use axis=1 to operate row-wise, axis=0 for column-wise.
Common for row-based calculations or transformations.
Full Transcript
We start with a DataFrame containing columns A and B. We define a function that adds the values in columns A and B for each row. Using apply() with axis=1, the function runs on each row one by one. For row 0, values 1 and 4 are added to get 5. For row 1, 2 and 5 sum to 7. For row 2, 3 and 6 sum to 9. The results are collected into a Series with values 5, 7, and 9. The key is axis=1 tells apply() to work row-wise. Each row is passed as a Series to the function. The final output is a Series with the function results for each row.