0
0
Pandasdata~10 mins

apply() on columns in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - apply() on columns
Start with DataFrame
Select a column
Apply function to each value
Collect results into new Series
Replace or add column in DataFrame
End
The apply() method takes a function and runs it on each value in a DataFrame column, then collects the results back into a Series or DataFrame column.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3]})
df['B'] = df['A'].apply(lambda x: x * 2)
print(df)
This code doubles each value in column 'A' and stores the result in a new column 'B'.
Execution Table
StepInput Value (x)Function AppliedResultDataFrame State
11x * 22{'A': [1, 2, 3], 'B': [2]}
22x * 24{'A': [1, 2, 3], 'B': [2, 4]}
33x * 26{'A': [1, 2, 3], 'B': [2, 4, 6]}
4---Completed applying function to all rows
💡 All values in column 'A' processed; new column 'B' created with doubled values.
Variable Tracker
VariableStartAfter 1After 2After 3Final
df['A'][1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3]
df['B'][][2][2, 4][2, 4, 6][2, 4, 6]
Key Moments - 2 Insights
Why does apply() run the function on each value separately instead of the whole column at once?
apply() is designed to apply the function element-wise on each value in the column, as shown in execution_table rows 1-3 where each input value is processed individually.
What happens if the function returns a different type or size than expected?
The result will be collected into a Series matching the original column length. If the function returns unexpected types, the new column may have mixed types or cause errors. This is why the function should return one value per input.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of df['B'] after step 2?
A[4]
B[2]
C[2, 4]
D[]
💡 Hint
Check the DataFrame State column at step 2 in the execution_table.
At which step does the apply() function finish processing all values?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Look for the row in execution_table where the DataFrame State says 'Completed applying function to all rows'.
If the lambda function was changed to 'x + 1', what would be the value of df['B'] after step 3?
A[3, 4, 5]
B[2, 3, 4]
C[1, 2, 3]
D[0, 1, 2]
💡 Hint
Add 1 to each original value in df['A'] which are [1, 2, 3].
Concept Snapshot
apply() on columns:
- Use df['col'].apply(func) to run func on each value.
- func receives one value at a time.
- Returns a Series with results.
- Can create or replace columns.
- Useful for element-wise transformations.
Full Transcript
This visual execution shows how pandas apply() works on DataFrame columns. Starting with a DataFrame with column 'A', we apply a function that doubles each value. The function runs on each value one by one, producing results collected into a new column 'B'. The execution table traces each step, showing input, function, result, and DataFrame state. Variable tracker shows how df['B'] grows after each step. Key moments clarify why apply() works element-wise and what happens if function returns unexpected results. The quiz tests understanding of the stepwise changes and effects of changing the function. The snapshot summarizes the key points for quick recall.