Challenge - 5 Problems
Apply Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of apply() with axis=1 on a DataFrame
What is the output of this code snippet?
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6]
})
result = df.apply(lambda row: row['A'] + row['B'], axis=1)
print(result)Pandas
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6] }) result = df.apply(lambda row: row['A'] + row['B'], axis=1) print(result)
Attempts:
2 left
💡 Hint
Remember that axis=1 means the function is applied to each row, not each column.
✗ Incorrect
The lambda function adds the values in columns 'A' and 'B' for each row. So for row 0: 1+4=5, row 1: 2+5=7, row 2: 3+6=9.
❓ data_output
intermediate2:00remaining
Resulting DataFrame after apply() with axis=1
Given this code, what is the resulting DataFrame?
import pandas as pd
df = pd.DataFrame({
'X': [10, 20],
'Y': [1, 2]
})
def add_columns(row):
return pd.Series({'Sum': row['X'] + row['Y'], 'Diff': row['X'] - row['Y']})
result = df.apply(add_columns, axis=1)
print(result)Pandas
import pandas as pd df = pd.DataFrame({ 'X': [10, 20], 'Y': [1, 2] }) def add_columns(row): return pd.Series({'Sum': row['X'] + row['Y'], 'Diff': row['X'] - row['Y']}) result = df.apply(add_columns, axis=1) print(result)
Attempts:
2 left
💡 Hint
The function returns a Series with two keys, so the result will have two columns.
✗ Incorrect
Each row returns a Series with 'Sum' and 'Diff'. For row 0: Sum=10+1=11, Diff=10-1=9; for row 1: Sum=20+2=22, Diff=20-2=18.
🔧 Debug
advanced2:00remaining
Identify the error in apply() usage with axis=1
What error does this code raise?
import pandas as pd
df = pd.DataFrame({'A': [1, 2]})
result = df.apply(lambda row: row['B'] * 2, axis=1)
print(result)Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2]}) result = df.apply(lambda row: row['B'] * 2, axis=1) print(result)
Attempts:
2 left
💡 Hint
Check if the column 'B' exists in the DataFrame.
✗ Incorrect
The DataFrame has only column 'A'. Trying to access row['B'] causes a KeyError.
❓ visualization
advanced2:00remaining
Visualizing apply() results on rows
What does this code plot?
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
'Height': [150, 160, 170],
'Weight': [50, 60, 70]
})
df['BMI'] = df.apply(lambda row: row['Weight'] / ((row['Height']/100) ** 2), axis=1)
plt.bar(df.index, df['BMI'])
plt.xlabel('Person Index')
plt.ylabel('BMI')
plt.title('BMI of Persons')
plt.show()Pandas
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'Height': [150, 160, 170], 'Weight': [50, 60, 70] }) df['BMI'] = df.apply(lambda row: row['Weight'] / ((row['Height']/100) ** 2), axis=1) plt.bar(df.index, df['BMI']) plt.xlabel('Person Index') plt.ylabel('BMI') plt.title('BMI of Persons') plt.show()
Attempts:
2 left
💡 Hint
Look at the plt.bar call and what data it uses.
✗ Incorrect
The code calculates BMI for each person and plots a bar chart with person index on x-axis and BMI on y-axis.
🚀 Application
expert2:30remaining
Using apply() with axis=1 to classify data
Given this DataFrame, which option correctly adds a new column 'Category' that labels each row as 'High' if 'Score' > 75, else 'Low'?
import pandas as pd
df = pd.DataFrame({'Name': ['Anna', 'Bob', 'Cara'], 'Score': [80, 70, 90]})
# Add 'Category' column here
Pandas
import pandas as pd df = pd.DataFrame({'Name': ['Anna', 'Bob', 'Cara'], 'Score': [80, 70, 90]}) # Add 'Category' column here
Attempts:
2 left
💡 Hint
Remember axis=1 applies the function to each row, axis=0 applies to columns.
✗ Incorrect
Option D correctly uses apply with axis=1 to check each row's 'Score' and assign 'High' or 'Low'. Option D works but uses apply on Series, not DataFrame with axis=1. Option D uses axis=0 which applies to columns, not rows. Option D misses axis=1 and will error.