0
0
Data Analysis Pythondata~20 mins

apply() function for custom logic in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Apply Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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())
A['B', 'A', 'B', 'A', 'C']
B['C', 'A', 'B', 'A', 'C']
C['C', 'A', 'A', 'A', 'C']
D['C', 'B', 'B', 'A', 'C']
Attempts:
2 left
💡 Hint
Check the conditions in the grade function carefully for each score.
data_output
intermediate
2: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)
A
   value  double
0     10     100
1     15     225
2     20     400
3     25     625
B
   value  double
0     10      10
1     15      15
2     20      20
3     25      25
C
   value  double
0     10      20
1     15      30
2     20      40
3     25      50
D
   value  double
0     10      5
1     15      7
2     20     10
3     25     12
Attempts:
2 left
💡 Hint
The lambda function multiplies each value by 2.
🔧 Debug
advanced
2: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)
ATypeError: unsupported operand type(s) for *: 'DataFrame' and 'float'
BValueError: The truth value of a DataFrame is ambiguous
CKeyError: 'temp_f'
DNo error, prints DataFrame with 'temp_f' column
Attempts:
2 left
💡 Hint
Check how apply() is used on the DataFrame without specifying axis.
🚀 Application
advanced
2: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]})
Adf['status'] = df.apply(lambda row: 'Pass' if row.score >= 50 or row.attendance >= 75 else 'Fail', axis=0)
Bdf['status'] = df.apply(lambda x: 'Pass' if x['score'] >= 50 and x['attendance'] >= 75 else 'Fail')
Cdf['status'] = df['score'].apply(lambda x: 'Pass' if x >= 50 else 'Fail')
Ddf['status'] = df.apply(lambda row: 'Pass' if row['score'] >= 50 and row['attendance'] >= 75 else 'Fail', axis=1)
Attempts:
2 left
💡 Hint
Remember to use axis=1 to apply function row-wise.
🧠 Conceptual
expert
2: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?
Aaxis=0 applies the function to each column (Series), axis=1 applies the function to each row (Series)
Baxis=0 applies the function to each row (Series), axis=1 applies the function to each column (Series)
Caxis=0 applies the function element-wise, axis=1 applies the function to the entire DataFrame
Daxis=0 and axis=1 both apply the function to the entire DataFrame but in different orders
Attempts:
2 left
💡 Hint
Think about rows and columns as Series objects.