Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Series arithmetic and alignment in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Series Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A
a    NaN
b    6.0
c    8.0
d    NaN
dtype: float64
B
a    5.0
b    6.0
c    8.0
d    6.0
dtype: int64
C
a    NaN
b    6
c    8
d    NaN
dtype: int64
D
a    1
b    6
c    8
d    6
dtype: int64
Attempts:
2 left
💡 Hint
Think about how pandas aligns indexes and what happens when indexes don't match.
❓ data_output
intermediate
1: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())
A2
B0
C1
D3
Attempts:
2 left
💡 Hint
Check which indexes exist in both Series.
🔧 Debug
advanced
1: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'])
AKeyError
BTypeError
CNo error, prints NaN
DAttributeError
Attempts:
2 left
💡 Hint
Check what happens when you access a missing index in a Series.
❓ visualization
advanced
2: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()
ABar chart with bars for 'p', 'q', 'r', 's' where 'p' and 's' bars are missing or zero height
BBar chart with bars for 'p', 'q', 'r', 's' where 'p' and 's' bars are NaN and not shown
CLine plot showing values for 'p', 'q', 'r', 's' with gaps at 'p' and 's'
DScatter plot with points only for 'q' and 'r'
Attempts:
2 left
💡 Hint
Think about how matplotlib handles NaN values in bar charts.
🚀 Application
expert
3: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?
A
total = s1 + s2 + s3
print(total.fillna(0))
B
total = pd.concat([s1, s2, s3]).sum()
print(total)
C
total = s1.combine(s2, lambda x, y: x + y).combine(s3, lambda x, y: x + y)
print(total)
D
total = s1.add(s2, fill_value=0).add(s3, fill_value=0)
print(total)
Attempts:
2 left
💡 Hint
Look for a method that allows adding Series with missing indexes treated as zero.

Practice

(1/5)
1. What happens when you add two pandas Series with different labels using + operator?
easy
A. The result is a Series with all zeros
B. Values are added ignoring labels, just by position
C. An error is raised due to label mismatch
D. Values are added only where labels match; others become NaN

Solution

  1. Step 1: Understand pandas Series label alignment

    When adding two Series, pandas aligns data by their labels (index) before performing arithmetic.
  2. Step 2: Effect of label mismatch on addition

    If a label is missing in one Series, the result for that label is NaN because pandas cannot add missing data.
  3. Final Answer:

    Values are added only where labels match; others become NaN -> Option D
  4. Quick Check:

    Series addition aligns labels = Values are added only where labels match; others become NaN [OK]
Hint: Remember: pandas adds by matching labels, not positions [OK]
Common Mistakes:
  • Assuming addition ignores labels and adds by position
  • Expecting an error on label mismatch
  • Thinking missing labels fill with zero automatically
2. Which of the following is the correct syntax to add two pandas Series s1 and s2 with label alignment, allowing specification of fill_value for missing labels?
easy
A. s1 + s2
B. s1 + s2.values
C. s1.add(s2)
D. s1.append(s2)

Solution

  1. Step 1: Check syntax for adding Series with alignment and fill_value

    Using s1.add(s2) adds Series with label alignment and allows fill_value for missing labels.
  2. Step 2: Compare other options

    s1 + s2 adds with alignment but no fill_value option. s1 + s2.values ignores labels (positional). s1.append(s2) concatenates.
  3. Final Answer:

    s1.add(s2) -> Option C
  4. Quick Check:

    Use .add() for aligned addition with fill_value [OK]
Hint: Use .add() method for safe label-aligned addition [OK]
Common Mistakes:
  • Using + with raw values ignoring labels
  • Confusing append with addition
  • Not knowing .add() exists for Series
3. Given the code:
import pandas as pd
s1 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
s2 = pd.Series([1, 3, 3], index=['b', 'c', 'd'])
result = s1 + s2
print(result)

What is the output?
medium
A. a NaN b 21.0 c 33.0 d NaN
B. a 10 b 22 c 33 d 3
C. a 10 b 21 c 33 d 3
D. a NaN b 22.0 c 33.0 d NaN

Solution

  1. Step 1: Align Series by labels

    s1 has 'a':10, 'b':20, 'c':30; s2 has 'b':1, 'c':3, 'd':3. Union index: 'a','b','c','d'.
  2. Step 2: Add values where labels match, NaN otherwise

    'a': 10 + NaN = NaN, 'b':20+1=21.0, 'c':30+3=33.0, 'd': NaN + 3 = NaN. Floats due to NaN.
  3. Final Answer:

    a NaN b 21.0 c 33.0 d NaN -> Option A
  4. Quick Check:

    Series addition aligns labels, unmatched get NaN [OK]
Hint: Add Series and check labels; unmatched labels become NaN [OK]
Common Mistakes:
  • Adding values by position ignoring labels
  • Expecting no NaN for unmatched labels
  • Misreading index labels and values
4. Consider the code:
import pandas as pd
s1 = pd.Series([5, 10], index=['x', 'y'])
s2 = pd.Series([2, 4, 6], index=['x', 'y', 'z'])
result = s1 + s2
print(result)

The result contains NaN. What is the problem and how to fix it to treat missing values as zero?
medium
A. No error; code runs fine
B. NaN due to different lengths; fix by using s1.add(s2, fill_value=0)
C. Error because + cannot add Series; use concat instead
D. Error due to missing index in s1; fix by reindexing s1 to s2's index

Solution

  1. Step 1: Check the code execution

    The code runs but produces NaN for 'z' (missing in s1). Alignment by labels causes NaN for unmatched.
  2. Step 2: Fix with fill_value

    s1.add(s2, fill_value=0) fills missing labels with 0: 'x':7, 'y':14, 'z':6.
  3. Final Answer:

    NaN due to different lengths; fix by using s1.add(s2, fill_value=0) -> Option B
  4. Quick Check:

    Use .add() with fill_value to avoid NaN [OK]
Hint: Use .add(fill_value=0) to handle missing labels safely [OK]
Common Mistakes:
  • Thinking + raises error on length mismatch
  • Using concat instead of arithmetic
  • Ignoring missing labels causing NaN
5. You have two Series representing sales in two stores:
store1 = pd.Series([100, 200, 300], index=['apple', 'banana', 'orange'])
store2 = pd.Series([50, 150, 100], index=['banana', 'orange', 'grape'])

How do you create a Series showing total sales per fruit, treating missing fruits as zero?
hard
A. store1.add(store2, fill_value=0)
B. store1 + store2
C. store1.append(store2).sum()
D. store1.combine(store2, lambda x, y: x + y)

Solution

  1. Step 1: Understand the problem

    We want to add sales per fruit, but some fruits are missing in one store's Series.
  2. Step 2: Use addition with fill_value=0

    Using store1.add(store2, fill_value=0) adds values label-wise, treating missing labels as zero, so no NaN appears.
  3. Final Answer:

    store1.add(store2, fill_value=0) -> Option A
  4. Quick Check:

    Add Series with fill_value=0 to handle missing labels [OK]
Hint: Use .add(fill_value=0) to sum with missing labels as zero [OK]
Common Mistakes:
  • Using + causing NaN for missing labels
  • Using append and sum incorrectly
  • Using combine without fill_value causing NaN
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard