Bird
Raised Fist0
Pandasdata~20 mins

Why custom functions matter in Pandas - Challenge Your Understanding

Choose your learning style10 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.

Practice

(1/5)
1. Why do we use custom functions when working with pandas DataFrames?
easy
A. To reuse code and make data changes clear and simple
B. To make the DataFrame larger automatically
C. To delete all data quickly
D. To change the pandas version

Solution

  1. Step 1: Understand the purpose of custom functions

    Custom functions let us write a piece of code once and use it many times, which saves effort.
  2. Step 2: Connect to pandas usage

    In pandas, custom functions help apply the same change or calculation to many rows or columns easily and clearly.
  3. Final Answer:

    To reuse code and make data changes clear and simple -> Option A
  4. Quick Check:

    Custom functions improve code reuse and clarity [OK]
Hint: Custom functions simplify repeated data changes [OK]
Common Mistakes:
  • Thinking custom functions automatically change DataFrame size
  • Believing custom functions delete data
  • Confusing custom functions with pandas version control
2. Which of the following is the correct way to define a simple custom function in Python to double a number?
easy
A. function double(x) { return x * 2 }
B. def double x: return x * 2
C. def double(x): return x * 2
D. double = x => x * 2

Solution

  1. Step 1: Identify Python function syntax

    Python functions start with 'def', followed by the function name and parentheses with parameters.
  2. Step 2: Check each option

    def double(x): return x * 2 uses correct Python syntax. function double(x) { return x * 2 } is JavaScript syntax. def double x: return x * 2 misses parentheses. double = x => x * 2 is JavaScript arrow function syntax.
  3. Final Answer:

    def double(x): return x * 2 -> Option C
  4. Quick Check:

    Python function syntax = def name(params): [OK]
Hint: Python functions start with 'def' and use parentheses [OK]
Common Mistakes:
  • Using JavaScript syntax in Python
  • Omitting parentheses in function definition
  • Confusing arrow functions with Python syntax
3. Given the code below, what will be the output of df['new']?
import pandas as pd
def add_five(x):
    return x + 5
df = pd.DataFrame({'num': [1, 2, 3]})
df['new'] = df['num'].apply(add_five)
print(df['new'])
medium
A. [1, 2, 3]
B. [6, 7, 8]
C. [5, 6, 7]
D. Error: add_five is not defined

Solution

  1. Step 1: Understand the function add_five

    The function adds 5 to the input number.
  2. Step 2: Apply the function to each element in 'num'

    Applying add_five to [1, 2, 3] results in [6, 7, 8].
  3. Final Answer:

    [6, 7, 8] -> Option B
  4. Quick Check:

    1+5=6, 2+5=7, 3+5=8 [OK]
Hint: Apply adds function result to each element [OK]
Common Mistakes:
  • Thinking apply changes original column without assignment
  • Confusing function name or missing definition
  • Expecting original values without addition
4. What is wrong with this code snippet?
import pandas as pd
def multiply_by_two(x):
    x * 2
df = pd.DataFrame({'val': [4, 5, 6]})
df['result'] = df['val'].apply(multiply_by_two)
print(df['result'])
medium
A. The DataFrame is not imported correctly
B. The DataFrame is missing a column
C. apply() cannot be used with custom functions
D. The function multiply_by_two does not return a value

Solution

  1. Step 1: Check the function multiply_by_two

    The function calculates x * 2 but does not return the result.
  2. Step 2: Understand apply behavior

    apply expects the function to return a value to create the new column. Without return, it returns None for each row.
  3. Final Answer:

    The function multiply_by_two does not return a value -> Option D
  4. Quick Check:

    Functions must return values for apply to work [OK]
Hint: Always return a value in your custom function [OK]
Common Mistakes:
  • Forgetting the return statement in functions
  • Assuming apply works without returned values
  • Thinking DataFrame import affects apply
5. You have a DataFrame with a column 'score' containing numbers. You want to create a new column 'grade' that assigns 'Pass' if the score is 50 or more, and 'Fail' otherwise. Which custom function and pandas code correctly achieve this?
hard
A. def grade_func(x): return 'Pass' if x >= 50 else 'Fail' df['grade'] = df['score'].apply(grade_func)
B. def grade_func(x): if x >= 50: 'Pass' else: 'Fail' df['grade'] = df['score'].apply(grade_func)
C. def grade_func(x): return x >= 50 or 'Pass' and 'Fail' df['grade'] = df['score'].apply(grade_func)
D. def grade_func(x): return 'Pass' if x > 50 else 'Fail' df['grade'] = df['score'].apply(grade_func)

Solution

  1. Step 1: Check the function logic for grading

    def grade_func(x): return 'Pass' if x >= 50 else 'Fail' df['grade'] = df['score'].apply(grade_func) correctly returns 'Pass' if x is 50 or more, else 'Fail'. def grade_func(x): if x >= 50: 'Pass' else: 'Fail' df['grade'] = df['score'].apply(grade_func) misses return statement. def grade_func(x): return x >= 50 or 'Pass' and 'Fail' df['grade'] = df['score'].apply(grade_func) uses incorrect logical operators, resulting in True or 'Fail'. def grade_func(x): return 'Pass' if x > 50 else 'Fail' df['grade'] = df['score'].apply(grade_func) uses > 50, which excludes 50.
  2. Step 2: Confirm pandas apply usage

    All options use apply correctly, but only def grade_func(x): return 'Pass' if x >= 50 else 'Fail' df['grade'] = df['score'].apply(grade_func) has correct function logic and syntax.
  3. Final Answer:

    def grade_func(x): return 'Pass' if x >= 50 else 'Fail' df['grade'] = df['score'].apply(grade_func) -> Option A
  4. Quick Check:

    Use return with correct condition for grading [OK]
Hint: Use return and correct condition in custom function [OK]
Common Mistakes:
  • Forgetting return in function
  • Using wrong comparison operator
  • Confusing logical expressions without return
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard