0
0
Data Analysis Pythondata~20 mins

Series sorting in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Series Sorting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of sorting a Series with missing values?
Consider the following pandas Series with some missing values. What will be the output after sorting it in ascending order?
Data Analysis Python
import pandas as pd
s = pd.Series([3, None, 1, 2, None])
s_sorted = s.sort_values()
print(s_sorted)
A
1    NaN
4    NaN
2    1.0
3    2.0
0    3.0
dtype: float64
B
0    3.0
1    NaN
2    1.0
3    2.0
4    NaN
dtype: float64
C
2    1.0
3    2.0
0    3.0
1    NaN
4    NaN
dtype: float64
D
2    1.0
3    2.0
0    3.0
4    NaN
1    NaN
dtype: float64
Attempts:
2 left
💡 Hint
Remember that pandas places NaN values at the end by default when sorting ascending.
data_output
intermediate
1:30remaining
How many items are in the sorted Series after dropping duplicates?
Given the Series below, after sorting it in descending order and dropping duplicates, how many items remain?
Data Analysis Python
import pandas as pd
s = pd.Series([5, 3, 5, 2, 3, 1])
s_sorted_unique = s.sort_values(ascending=False).drop_duplicates()
print(len(s_sorted_unique))
A4
B6
C5
D3
Attempts:
2 left
💡 Hint
Count unique values after sorting descending.
🔧 Debug
advanced
2:00remaining
What error does this sorting code raise?
Examine the code below. What error will it raise when executed?
Data Analysis Python
import pandas as pd
s = pd.Series(['a', 'b', 3, 'd'])
s_sorted = s.sort_values()
print(s_sorted)
AKeyError: 'sort_values'
BValueError: Cannot sort mixed types
CNo error, outputs sorted Series with mixed types
DTypeError: '<' not supported between instances of 'int' and 'str'
Attempts:
2 left
💡 Hint
Think about comparing strings and integers during sorting.
visualization
advanced
2:30remaining
Which plot shows the Series sorted in ascending order?
You have this Series: s = pd.Series([4, 1, 3, 2]). Which plot correctly shows the Series after sorting it in ascending order?
Data Analysis Python
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series([4, 1, 3, 2])
s_sorted = s.sort_values()
s_sorted.plot(kind='bar')
plt.show()
ABar plot with bars heights in order: 4, 3, 2, 1 from left to right
BBar plot with bars heights in order: 1, 2, 3, 4 from left to right
CBar plot with bars heights in order: 4, 1, 3, 2 from left to right
DBar plot with bars heights all equal
Attempts:
2 left
💡 Hint
Sorting ascending means smallest value first on the left.
🧠 Conceptual
expert
1:30remaining
What is the effect of the 'inplace=True' parameter in Series.sort_values()?
When you use s.sort_values(inplace=True) on a pandas Series s, what happens?
AThe original Series s is sorted and modified directly; the method returns None.
BA new sorted Series is returned; the original Series s remains unchanged.
CThe Series is sorted but a copy is returned; the original Series s is also modified.
DThe method raises an error because inplace is not supported for Series.
Attempts:
2 left
💡 Hint
Think about whether the original data changes or a new object is created.