0
0
Pandasdata~20 mins

xs() for cross-section selection in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cross-Section Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of xs() on MultiIndex DataFrame
What is the output of the following code snippet using xs() to select a cross-section from a MultiIndex DataFrame?
Pandas
import pandas as pd

index = pd.MultiIndex.from_tuples([('A', 1), ('A', 2), ('B', 1), ('B', 2)], names=['letter', 'number'])
data = {'value': [10, 20, 30, 40]}
df = pd.DataFrame(data, index=index)

result = df.xs('A', level='letter')
print(result)
A
letter  value
A       10
A       20
B
   value
letter       
A        10
A        20
C
value
0     10
1     20
D
   value
number       
1        10
2        20
Attempts:
2 left
💡 Hint
Remember that xs() selects data at a particular level of a MultiIndex.
data_output
intermediate
1:30remaining
Number of rows after xs() selection
Given the DataFrame below, how many rows does the result have after applying df.xs(2, level='number')?
Pandas
import pandas as pd

index = pd.MultiIndex.from_tuples([('X', 1), ('X', 2), ('Y', 1), ('Y', 2), ('Z', 2)], names=['letter', 'number'])
data = {'score': [5, 10, 15, 20, 25]}
df = pd.DataFrame(data, index=index)

result = df.xs(2, level='number')
A3
B2
C5
D1
Attempts:
2 left
💡 Hint
Count how many rows have the index level 'number' equal to 2.
🔧 Debug
advanced
1:30remaining
Identify the error in xs() usage
What error does the following code raise when using xs() incorrectly?
Pandas
import pandas as pd

index = pd.MultiIndex.from_tuples([('a', 1), ('b', 2)], names=['letter', 'number'])
data = {'val': [100, 200]}
df = pd.DataFrame(data, index=index)

result = df.xs('c', level='letter')
AKeyError
BTypeError
CValueError
DAttributeError
Attempts:
2 left
💡 Hint
Check what happens when the key is not found in the specified index level.
🚀 Application
advanced
2:00remaining
Using xs() to select columns in a MultiIndex DataFrame
Given a DataFrame with MultiIndex columns, which xs() call correctly selects the cross-section for the column level 'metric' with value 'height'?
Pandas
import pandas as pd

arrays = [['height', 'height', 'weight', 'weight'], ['min', 'max', 'min', 'max']]
columns = pd.MultiIndex.from_arrays(arrays, names=['metric', 'stat'])
 data = [[150, 180, 60, 80], [160, 190, 65, 85]]
df = pd.DataFrame(data, columns=columns)

# Which xs() call selects the 'height' metric columns?
Adf.xs('height', level='stat', axis=0)
Bdf.xs('height', level='metric', axis=1)
Cdf.xs('height', level='metric', axis=0)
Ddf.xs('height', level='stat', axis=1)
Attempts:
2 left
💡 Hint
Remember that columns are selected with axis=1 and the level name must match the MultiIndex level.
🧠 Conceptual
expert
1:30remaining
Effect of drop_level parameter in xs()
What is the effect of setting drop_level=False in the xs() method when selecting a cross-section from a MultiIndex DataFrame?
AThe DataFrame is converted to a Series.
BThe selected level is removed from the index after selection.
CThe selected level remains in the index after selection.
DThe selection returns all levels except the specified one.
Attempts:
2 left
💡 Hint
Think about whether the index level you select stays or is dropped.