Challenge - 5 Problems
Series Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Series Addition with Different Indexes
What is the output of the following code?
import pandas as pd s1 = pd.Series([1, 2, 3], index=['a', 'b', 'c']) s2 = pd.Series([4, 5, 6], index=['b', 'c', 'd']) result = s1 + s2 print(result)
Data Analysis Python
import pandas as pd s1 = pd.Series([1, 2, 3], index=['a', 'b', 'c']) s2 = pd.Series([4, 5, 6], index=['b', 'c', 'd']) result = s1 + s2 print(result)
Attempts:
2 left
💡 Hint
Think about how pandas aligns indexes and what happens when indexes don't match.
✗ Incorrect
When adding two Series, pandas aligns them by their index labels. If an index label is missing in one Series, the result is NaN for that label. The result is float64 because NaN is a float.
❓ data_output
intermediate1:30remaining
Number of Non-NaN Elements After Series Subtraction
Given two Series with different indexes, how many non-NaN values are in the result of their subtraction?
import pandas as pd s1 = pd.Series([10, 20, 30], index=['x', 'y', 'z']) s2 = pd.Series([5, 15], index=['y', 'z']) result = s1 - s2 print(result.dropna().count())
Data Analysis Python
import pandas as pd s1 = pd.Series([10, 20, 30], index=['x', 'y', 'z']) s2 = pd.Series([5, 15], index=['y', 'z']) result = s1 - s2 print(result.dropna().count())
Attempts:
2 left
💡 Hint
Check which indexes exist in both Series.
✗ Incorrect
Only indexes 'y' and 'z' exist in both Series, so subtraction results in valid numbers for these two. Index 'x' has no match in s2, so result is NaN there.
🔧 Debug
advanced1:30remaining
Identify the Error in Series Multiplication
What error does the following code raise?
import pandas as pd s1 = pd.Series([1, 2, 3], index=['a', 'b', 'c']) s2 = pd.Series([4, 5], index=['b', 'c']) result = s1 * s2 print(result['a'])
Data Analysis Python
import pandas as pd s1 = pd.Series([1, 2, 3], index=['a', 'b', 'c']) s2 = pd.Series([4, 5], index=['b', 'c']) result = s1 * s2 print(result['a'])
Attempts:
2 left
💡 Hint
Check what happens when you access a missing index in a Series.
✗ Incorrect
The multiplication aligns indexes, so 'a' is missing in s2, resulting in NaN in result. Accessing result['a'] returns NaN, which prints fine. So no error here. But the question asks what error is raised. Actually, no error is raised when printing NaN. So option C is correct.
❓ visualization
advanced2:30remaining
Visualizing Series Addition with Missing Data
You have two Series with some overlapping and some unique indexes. Which plot best shows the result of their addition, highlighting NaN values?
import pandas as pd import matplotlib.pyplot as plt s1 = pd.Series([2, 4, 6], index=['p', 'q', 'r']) s2 = pd.Series([1, 3, 5], index=['q', 'r', 's']) result = s1 + s2 result.plot(kind='bar') plt.show()
Data Analysis Python
import pandas as pd import matplotlib.pyplot as plt s1 = pd.Series([2, 4, 6], index=['p', 'q', 'r']) s2 = pd.Series([1, 3, 5], index=['q', 'r', 's']) result = s1 + s2 result.plot(kind='bar') plt.show()
Attempts:
2 left
💡 Hint
Think about how matplotlib handles NaN values in bar charts.
✗ Incorrect
When plotting a Series with NaN values using bar chart, bars for NaN indexes are not shown (missing). So 'p' and 's' bars will be missing because their values are NaN after addition.
🚀 Application
expert3:00remaining
Combining Multiple Series with Different Indexes
You have three Series representing sales in different regions with overlapping and unique dates as indexes. You want to create a single Series showing total sales per date, treating missing data as zero. Which code snippet achieves this correctly?
import pandas as pd s1 = pd.Series([100, 200], index=['2024-01-01', '2024-01-02']) s2 = pd.Series([150, 250], index=['2024-01-02', '2024-01-03']) s3 = pd.Series([50, 100], index=['2024-01-01', '2024-01-03']) # Which option correctly sums these Series treating missing values as zero?
Data Analysis Python
import pandas as pd s1 = pd.Series([100, 200], index=['2024-01-01', '2024-01-02']) s2 = pd.Series([150, 250], index=['2024-01-02', '2024-01-03']) s3 = pd.Series([50, 100], index=['2024-01-01', '2024-01-03']) # Which option correctly sums these Series treating missing values as zero?
Attempts:
2 left
💡 Hint
Look for a method that allows adding Series with missing indexes treated as zero.
✗ Incorrect
Using add() with fill_value=0 correctly sums Series treating missing indexes as zero. Option D sums first then fills NaN, which can cause wrong sums. Option D does not handle missing values properly. Option D concatenates Series vertically and sums all values, losing index alignment.