0
0
Data Analysis Pythondata~20 mins

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

Choose your learning style9 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.