0
0
Pandasdata~20 mins

Creating Series from list and dictionary in Pandas - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Series Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1: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'])
A10
BKeyError
C30
D20
Attempts:
2 left
💡 Hint
Remember that the index labels are used to access values in a Series.
data_output
intermediate
1: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))
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Each key-value pair in the dictionary becomes one item in the Series.
🔧 Debug
advanced
1: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'])
AValueError: Length of passed values is 2, index implies 3
BTypeError: unsupported operand type(s)
CKeyError: 'c'
DNo error, creates Series with NaN
Attempts:
2 left
💡 Hint
Check if the length of data matches the length of the index.
🧠 Conceptual
advanced
1: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())
A['1', '2', '3']
B[1, 2, 3]
C['a', 'b', 'c']
DRangeIndex(start=0, stop=3, step=1)
Attempts:
2 left
💡 Hint
Dictionary keys become the index labels as they are.
🚀 Application
expert
2: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)
AKeyError: 'd'
B
a    100
b    200
c    300
d    NaN
dtype: float64
C
a    100.0
c    300.0
d      NaN
dtype: float64
D
a    100
c    300
d    0
dtype: int64
Attempts:
2 left
💡 Hint
When index has labels not in dictionary keys, pandas fills with NaN.