How to Use xs for Cross Section in pandas DataFrames
Use the
xs() method in pandas to select a cross section from a DataFrame by specifying a label along a particular axis. It works well for selecting rows or columns by label, especially in multi-index DataFrames.Syntax
The xs() method syntax is:
DataFrame.xs(key, axis=0, level=None, drop_level=True)
Where:
key: The label to select.axis: 0 for rows (default), 1 for columns.level: For MultiIndex, specify which level to select from.drop_level: Whether to drop the level from the result (default True).
python
df.xs(key, axis=0, level=None, drop_level=True)
Example
This example shows how to use xs() to select a row by label and a cross section from a MultiIndex DataFrame.
python
import pandas as pd # Simple DataFrame data = {'A': [1, 2, 3], 'B': [4, 5, 6]} df = pd.DataFrame(data, index=['x', 'y', 'z']) # Select row 'y' row_y = df.xs('y') # MultiIndex DataFrame arrays = [['bar', 'bar', 'baz', 'baz'], ['one', 'two', 'one', 'two']] index = pd.MultiIndex.from_arrays(arrays, names=['first', 'second']) df_multi = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]}, index=index) # Select cross section where first level is 'bar' cross_section = df_multi.xs('bar', level='first') print('Single index selection:\n', row_y) print('\nMultiIndex cross section:\n', cross_section)
Output
Single index selection:
A 2
B 5
Name: y, dtype: int64
MultiIndex cross section:
A B
second
one 1 5
two 2 6
Common Pitfalls
Common mistakes when using xs() include:
- Not specifying the
levelparameter for MultiIndex, which can cause errors or unexpected results. - Using
axis=1incorrectly when trying to select columns by label. - Forgetting that
xs()returns a reduced dimension, so the result may be a Series instead of a DataFrame.
Example of a wrong and right usage:
python
import pandas as pd # MultiIndex DataFrame arrays = [['a', 'a', 'b', 'b'], ['x', 'y', 'x', 'y']] index = pd.MultiIndex.from_arrays(arrays, names=['level1', 'level2']) df = pd.DataFrame({'val': [10, 20, 30, 40]}, index=index) # Wrong: missing level parameter try: wrong = df.xs('a') except Exception as e: print('Error:', e) # Right: specify level right = df.xs('a', level='level1') print('\nCorrect cross section:\n', right)
Output
Error: 'a' is not in list
Correct cross section:
val
level2
x 10
y 20
Quick Reference
| Parameter | Description | Default |
|---|---|---|
| key | Label to select | Required |
| axis | 0 for rows, 1 for columns | 0 |
| level | Level name or number for MultiIndex | None |
| drop_level | Drop the level from result | True |
Key Takeaways
Use xs() to select a cross section by label from rows or columns in a DataFrame.
Specify the level parameter when working with MultiIndex to avoid errors.
xs() returns a reduced dimension, often a Series when selecting a single label.
Use axis=1 to select columns by label with xs().
Common errors come from missing level or wrong axis parameters.