Challenge - 5 Problems
Apply Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of apply() with a custom function on DataFrame column
What is the output of the following code snippet?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'score': [45, 82, 77, 90, 56]}) def grade(x): if x >= 80: return 'A' elif x >= 60: return 'B' else: return 'C' df['grade'] = df['score'].apply(grade) print(df['grade'].tolist())
Attempts:
2 left
💡 Hint
Check the conditions in the grade function carefully for each score.
✗ Incorrect
Scores 45 and 56 are below 60, so grade 'C'. Scores 82 and 90 are 80 or above, so grade 'A'. Score 77 is between 60 and 79, so grade 'B'.
❓ data_output
intermediate2:00remaining
Resulting DataFrame after apply() with lambda function
What is the resulting DataFrame after applying the lambda function below?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'value': [10, 15, 20, 25]}) df['double'] = df['value'].apply(lambda x: x * 2) print(df)
Attempts:
2 left
💡 Hint
The lambda function multiplies each value by 2.
✗ Incorrect
Each 'value' is doubled in the 'double' column.
🔧 Debug
advanced2:00remaining
Identify the error in apply() usage with a custom function
What error does the following code raise?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'temp': [30, 22, 25]}) def convert(x): return x * 9/5 + 32 df['temp_f'] = df.apply(convert) print(df)
Attempts:
2 left
💡 Hint
Check how apply() is used on the DataFrame without specifying axis.
✗ Incorrect
apply() without axis defaults to applying function on columns (Series), so 'x' is a Series, and multiplying Series by float works, but the function expects a scalar. Actually, the function works on Series but returns a Series, so the assignment fails. But in this case, the error is TypeError because the function tries to multiply DataFrame by float if axis is not specified. Actually, the error is because apply() applies function to each column (Series), so x is a Series, and the function returns a Series, so the assignment fails. The actual error is TypeError due to unsupported operand types when trying to multiply DataFrame by float. The correct usage is to specify axis=1 or apply on a Series.
🚀 Application
advanced2:00remaining
Using apply() to create a new column based on multiple columns
Given the DataFrame below, which option correctly creates a new column 'status' that is 'Pass' if score >= 50 and attendance >= 75, else 'Fail'?
Data Analysis Python
import pandas as pd df = pd.DataFrame({'score': [55, 48, 70, 40], 'attendance': [80, 70, 90, 85]})
Attempts:
2 left
💡 Hint
Remember to use axis=1 to apply function row-wise.
✗ Incorrect
Option D correctly applies the function row-wise (axis=1) and checks both conditions with 'and'. Option D misses axis=1, so applies column-wise causing error. Option D only checks score, ignoring attendance. Option D uses axis=0 and 'or' instead of 'and'.
🧠 Conceptual
expert2:00remaining
Understanding apply() behavior with axis parameter
Which statement best describes the behavior of the apply() function when used on a DataFrame with axis=0 and axis=1?
Attempts:
2 left
💡 Hint
Think about rows and columns as Series objects.
✗ Incorrect
In pandas, axis=0 means apply function to each column (each column is a Series). axis=1 means apply function to each row (each row is a Series).