Completed
Series — Indexing/Slicing
Questions From This Challenge
Q1mcq1 mark
Given s = pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd']), what does s['b'] return?
20 ✓
10
30
'b'
Q2mcq1 mark
Given s = pd.Series([10, 20, 30, 40]) with a default integer index, what does s[2] return?
30 ✓
20
3
An error, since 2 is not a label
Q3mcq1 mark
For s = pd.Series([1, 2, 3, 4, 5]), which elements does the positional slice s[1:4] include?
Elements at positions 1, 2 and 3 (index labels 1, 2, 3) ✓
Elements at positions 1, 2, 3 and 4
Only the element at position 4
Elements at positions 0 through 4
Q4mcq1 mark
For s = pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd']), which elements does the label-based slice s['b':'d'] include?
Elements labeled 'b', 'c' and 'd' (end label included) ✓
Elements labeled 'b' and 'c' only (end label excluded)
Only the element labeled 'd'
An error, since label slicing is not supported on a Series
Q5mcq1 mark
What is the key difference between positional slicing (e.g. s[1:3]) and label-based slicing (e.g. s['a':'c']) on a Series?
Positional slicing excludes the stop position, while label-based slicing includes the stop label ✓
Positional slicing includes the stop position, while label-based slicing excludes the stop label
Both always include the stop value
Both always exclude the stop value
Q6mcq1 mark
Given s = pd.Series([100, 200, 300], index=['p', 'q', 'r']), which expression correctly selects a Series containing only the values at labels 'p' and 'r'?
s[['p', 'r']] ✓
s['p', 'r']
s.select('p', 'r')
s[('p', 'r')]
Q7mcq1 mark
Given s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']), what does s[:2] return?
The elements at positions 0 and 1, i.e. labels 'a' and 'b' ✓
The elements at positions 2 and 3
Only the element at position 2
The entire Series
Q8output1 mark
What is the output of the following code?
import pandas as pd s = pd.Series([10, 20, 30, 40, 50]) print(s[2])
Correct output:
30Q9output1 mark
What is the output of the following code?
import pandas as pd s = pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e']) print(s['c'])
Correct output:
3Q10output1 mark
What is the output of the following code?
import pandas as pd s = pd.Series([1, 2, 3, 4, 5]) print(s[1:4])
Correct output:
1 2
2 3
3 4
dtype: int64Q11output1 mark
What is the output of the following code?
import pandas as pd s = pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd']) print(s['b':'d'])
Correct output:
b 20
c 30
d 40
dtype: int64Q12output1 mark
What is the output of the following code?
import pandas as pd s = pd.Series([5, 15, 25, 35], index=['w', 'x', 'y', 'z']) print(s[['w', 'y']])
Correct output:
w 5
y 25
dtype: int64Q13output1 mark
What is the output of the following code?
import pandas as pd s = pd.Series([2, 4, 6, 8, 10]) print(s[-2:])
Correct output:
3 8
4 10
dtype: int64Q14output1 mark
What is the output of the following code?
import pandas as pd s = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) print(s[:2])
Correct output:
a 1
b 2
dtype: int64Q15output1 mark
What is the output of the following code, where the Series has explicit integer index labels rather than the default 0,1,2,...?
import pandas as pd s = pd.Series(['A', 'B', 'C', 'D', 'E'], index=[10, 20, 30, 40, 50]) print(s[20])
Correct output:
B