0
0
Pandasdata~20 mins

Why custom functions matter in Pandas - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of applying a custom function with pandas apply
What is the output of the following code that uses a custom function with pandas apply on a DataFrame column?
Pandas
import pandas as pd

def add_five(x):
    return x + 5

df = pd.DataFrame({'A': [1, 2, 3]})
df['B'] = df['A'].apply(add_five)
print(df)
A
   A  B
0  1  6
1  2  7
2  3  8
B
   A  B
0  6  1
1  7  2
2  8  3
C
   A  B
0  1  5
1  2  6
2  3  7
D
   A  B
0  1  1
1  2  2
2  3  3
Attempts:
2 left
💡 Hint
Remember that apply passes each element of the column to the function.
data_output
intermediate
2:00remaining
Result of using a lambda function with apply on a DataFrame
What is the resulting DataFrame after applying this lambda function to column 'score'?
Pandas
import pandas as pd

df = pd.DataFrame({'score': [50, 80, 90]})
df['grade'] = df['score'].apply(lambda x: 'Pass' if x >= 60 else 'Fail')
print(df)
A
   score grade
0     50  Fail
1     80  Pass
2     90  Pass
B
   score grade
0     50  Pass
1     80  Pass
2     90  Pass
C
   score grade
0     50  Fail
1     80  Fail
2     90  Pass
D
   score grade
0     50  Fail
1     80  Pass
2     90  Fail
Attempts:
2 left
💡 Hint
Check the condition inside the lambda function carefully.
🔧 Debug
advanced
2:00remaining
Identify the error in this custom function used with apply
What error will this code raise when applying the custom function to the DataFrame column?
Pandas
import pandas as pd

def divide_by_zero(x):
    return x / 0

df = pd.DataFrame({'num': [1, 2, 3]})
df['result'] = df['num'].apply(divide_by_zero)
ATypeError
BZeroDivisionError
CKeyError
DNo error, outputs NaN
Attempts:
2 left
💡 Hint
Think about what happens when dividing by zero in Python.
🚀 Application
advanced
2:30remaining
Using a custom function to categorize data in pandas
You want to categorize ages into 'Child', 'Adult', and 'Senior' using a custom function with apply. Which code correctly creates a new column 'category' with these labels?
Pandas
import pandas as pd

df = pd.DataFrame({'age': [5, 25, 70]})
A
def categorize(age):
    if age < 18:
        return 'Child'
    elif age < 65:
        return 'Adult'
    else:
        return 'Senior'

df['category'] = df['age'].apply(categorize)
B
def categorize(age):
    if age <= 18:
        return 'Child'
    elif age <= 65:
        return 'Adult'
    else:
        return 'Senior'

df['category'] = df['age'].apply(categorize)
C
def categorize(age):
    if age <= 18:
        return 'Child'
    elif age < 65:
        return 'Adult'
    else:
        return 'Senior'

df['category'] = df['age'].apply(categorize)
D
def categorize(age):
    if age < 18:
        return 'Child'
    elif age <= 65:
        return 'Adult'
    else:
        return 'Senior'

df['category'] = df['age'].apply(categorize)
Attempts:
2 left
💡 Hint
Check the boundary conditions carefully for age groups.
🧠 Conceptual
expert
2:00remaining
Why use custom functions with pandas apply instead of vectorized operations?
Which statement best explains why custom functions are important when working with pandas apply?
ACustom functions automatically optimize memory usage in pandas DataFrames.
BCustom functions always run faster than vectorized operations in pandas.
CCustom functions allow applying complex logic that cannot be done with built-in vectorized operations.
DCustom functions prevent the need to import pandas for data manipulation.
Attempts:
2 left
💡 Hint
Think about the flexibility custom functions provide.