0
0
Pandasdata~20 mins

Arithmetic operations on columns in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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
intermediate
2: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())
A[4, 7, 9]
B[6, 7, 8]
C[1, 2, 3]
D[5, 7, 9]
Attempts:
2 left
💡 Hint
Adding two columns sums their values row-wise.
data_output
intermediate
2: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())
A[9, 0, 30]
B[9.0, -2.0, 30.0]
C[9.0, nan, nan]
D[nan, nan, nan]
Attempts:
2 left
💡 Hint
Subtracting with NaN results in NaN for that row.
visualization
advanced
3: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()
ABar plot with heights [2, 12, 30]
BHistogram of values [1, 3, 5]
CScatter plot with points (2,1), (4,3), (6,5)
DLine plot with points [2, 7, 11]
Attempts:
2 left
💡 Hint
The product of X and Y is calculated element-wise.
🔧 Debug
advanced
2: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())
AZeroDivisionError
B[5.0, inf, 6.0]
CTypeError
D[5.0, nan, 6.0]
Attempts:
2 left
💡 Hint
Division by zero in pandas results in inf, not an error.
🧠 Conceptual
expert
2: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?
AAdds 5 to each element in column 'A' and stores in 'C'
BAdds 5 to the entire column 'A' as a single value and stores in 'C'
CRaises a TypeError because 5 is not a column
DCreates column 'C' with all values equal to 5
Attempts:
2 left
💡 Hint
Pandas broadcasts scalar values across all rows in a column.