0
0
Data Analysis Pythondata~10 mins

apply() function for custom logic in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - apply() function for custom logic
Start with DataFrame
Define custom function
Call apply() on DataFrame or Series
apply() sends each row/column/value to custom function
Custom function processes input and returns result
apply() collects all results into new Series/DataFrame
Use or store the transformed data
The apply() function takes a custom function and runs it on each row, column, or element, then collects the results.
Execution Sample
Data Analysis Python
import pandas as pd

def add_bonus(salary):
    return salary + 100

salaries = pd.Series([1000, 2000, 3000])
bonuses = salaries.apply(add_bonus)
This code adds 100 bonus to each salary using apply() with a custom function.
Execution Table
StepInput to add_bonusFunction Outputbonuses Series after step
110001100[1100, NaN, NaN]
220002100[1100, 2100, NaN]
330003100[1100, 2100, 3100]
4End of apply()All values processed[1100, 2100, 3100]
💡 All elements processed, apply() returns new Series with updated values
Variable Tracker
VariableStartAfter 1After 2After 3Final
salarypd.Series([1000, 2000, 3000])1000200030003000
bonusesEmpty[1100, NaN, NaN][1100, 2100, NaN][1100, 2100, 3100][1100, 2100, 3100]
Key Moments - 2 Insights
Why does apply() return a new Series instead of modifying the original?
apply() creates a new Series to keep original data unchanged, as shown in execution_table rows 1-4 where bonuses build up separately.
What exactly is passed to the custom function in apply()?
Each element of the Series is passed one by one to the custom function, as seen in execution_table column 'Input to add_bonus'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of add_bonus when input is 2000?
A2000
B2100
C100
D3000
💡 Hint
Check the row where Input to add_bonus is 2000 in the execution_table.
At which step does the bonuses Series first have all values filled?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'bonuses Series after step' column in execution_table.
If the custom function returned salary * 2 instead of salary + 100, what would bonuses be after step 3?
A[2000, 4000, 6000]
B[1100, 2100, 3100]
C[100, 200, 300]
D[1000, 2000, 3000]
💡 Hint
Think about doubling each input value from the variable_tracker salaries.
Concept Snapshot
apply() runs a custom function on each element (or row/column) of a DataFrame or Series.
It returns a new Series or DataFrame with the results.
Original data stays unchanged.
Use apply() to add custom logic easily.
Example: series.apply(func) where func processes one value at a time.
Full Transcript
The apply() function in pandas lets you run your own function on each element of a Series or each row/column of a DataFrame. You start with your data, define a function that does something to one value, then call apply() with that function. Internally, apply() sends each value to your function, collects the results, and returns a new Series or DataFrame. This way, you can add custom logic without changing the original data. For example, adding a fixed bonus to salaries by defining add_bonus(salary) and applying it to each salary. The execution table shows each step: input value, function output, and how the new Series builds up. This helps beginners see how apply() works step-by-step.