Challenge - 5 Problems
Master of Arithmetic Operations on Columns
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of adding two DataFrame columns
What is the output of this code snippet?
Pandas
import pandas as pd df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4, 5, 6] }) df['C'] = df['A'] + df['B'] print(df['C'].tolist())
Attempts:
2 left
💡 Hint
Adding two columns sums their values row-wise.
✗ Incorrect
The new column 'C' is created by adding corresponding elements of columns 'A' and 'B'. So, 1+4=5, 2+5=7, 3+6=9.
❓ data_output
intermediate2:00remaining
Result of subtracting columns with missing values
Given this DataFrame, what is the output of subtracting column 'B' from 'A'?
Pandas
import pandas as pd import numpy as np df = pd.DataFrame({ 'A': [10, np.nan, 30], 'B': [1, 2, np.nan] }) df['D'] = df['A'] - df['B'] print(df['D'].tolist())
Attempts:
2 left
💡 Hint
Subtracting with NaN results in NaN for that row.
✗ Incorrect
When subtracting, if either value is NaN, the result is NaN. Only the first row has both numbers, so 10-1=9.0; others are NaN.
❓ visualization
advanced3:00remaining
Visualizing product of two columns
Which plot correctly shows the product of columns 'X' and 'Y' as a new column 'Z'?
Pandas
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({ 'X': [2, 4, 6], 'Y': [1, 3, 5] }) df['Z'] = df['X'] * df['Y'] plt.bar(df.index, df['Z']) plt.xlabel('Index') plt.ylabel('Product Z') plt.title('Bar plot of product of X and Y') plt.show()
Attempts:
2 left
💡 Hint
The product of X and Y is calculated element-wise.
✗ Incorrect
The product column Z is [2*1=2, 4*3=12, 6*5=30]. The bar plot shows these values as heights.
🔧 Debug
advanced2:00remaining
Identify error in column division code
What error does this code raise?
Pandas
import pandas as pd df = pd.DataFrame({'A': [10, 20, 30], 'B': [2, 0, 5]}) df['C'] = df['A'] / df['B'] print(df['C'].tolist())
Attempts:
2 left
💡 Hint
Division by zero in pandas results in inf, not an error.
✗ Incorrect
Pandas returns inf for division by zero, so the second element is inf, not an error or NaN.
🧠 Conceptual
expert2:30remaining
Effect of broadcasting in arithmetic operations on columns
Given a DataFrame df with columns 'A' and 'B', what happens when you run df['C'] = df['A'] + 5?
Attempts:
2 left
💡 Hint
Pandas broadcasts scalar values across all rows in a column.
✗ Incorrect
Adding a scalar to a column adds that scalar to each element individually.