Challenge - 5 Problems
Series Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of Series created from list with index
What is the output of this code snippet?
Pandas
import pandas as pd s = pd.Series([10, 20, 30], index=['a', 'b', 'c']) print(s['b'])
Attempts:
2 left
💡 Hint
Remember that the index labels are used to access values in a Series.
✗ Incorrect
The Series is created with index labels 'a', 'b', 'c'. Accessing s['b'] returns the value 20.
❓ data_output
intermediate1:00remaining
Length of Series created from dictionary
How many items are in the Series created from this dictionary?
Pandas
import pandas as pd d = {'x': 1, 'y': 2, 'z': 3} s = pd.Series(d) print(len(s))
Attempts:
2 left
💡 Hint
Each key-value pair in the dictionary becomes one item in the Series.
✗ Incorrect
The dictionary has 3 key-value pairs, so the Series has length 3.
🔧 Debug
advanced1:30remaining
Error raised when creating Series with mismatched index
What error does this code raise?
Pandas
import pandas as pd s = pd.Series([1, 2], index=['a', 'b', 'c'])
Attempts:
2 left
💡 Hint
Check if the length of data matches the length of the index.
✗ Incorrect
The data list has length 2 but the index has length 3, causing a ValueError.
🧠 Conceptual
advanced1:30remaining
Behavior of Series when dictionary keys are not strings
What will be the index of the Series created from this dictionary?
Pandas
import pandas as pd d = {1: 'a', 2: 'b', 3: 'c'} s = pd.Series(d) print(s.index.tolist())
Attempts:
2 left
💡 Hint
Dictionary keys become the index labels as they are.
✗ Incorrect
The Series index uses the dictionary keys as labels, so the index is [1, 2, 3].
🚀 Application
expert2:00remaining
Creating Series with partial index from dictionary
Given the dictionary and index below, what is the output of the Series?
Pandas
import pandas as pd d = {'a': 100, 'b': 200, 'c': 300} index = ['a', 'c', 'd'] s = pd.Series(d, index=index) print(s)
Attempts:
2 left
💡 Hint
When index has labels not in dictionary keys, pandas fills with NaN.
✗ Incorrect
The Series uses the given index. 'd' is missing in dictionary, so its value is NaN.