Pandas DataFrame.xs
Pandas DataFrame.xs
The DataFrame.xs
method in pandas is used to retrieve a cross-section (row or column) of a DataFrame. This is particularly useful when working with hierarchical (MultiIndex) data.
Syntax
The syntax for DataFrame.xs
is:
DataFrame.xs(key, axis=0, level=None, drop_level=True)
Here, DataFrame
refers to the pandas DataFrame from which a cross-section is being retrieved.
Parameters
Parameter | Description |
---|---|
key | The label or key to select data along the specified axis. |
axis | The axis to retrieve the cross-section from. 0 for rows and 1 for columns. Defaults to 0 . |
level | The level(s) to select in case of a MultiIndex. Defaults to None . |
drop_level | If True , removes the selected level from the output. Defaults to True . |
Returns
A Series or DataFrame representing the cross-section.
Examples
Accessing a Row by Label
Use xs
to access a specific row by its label.
Python Program
import pandas as pd
# Create a DataFrame with a MultiIndex
arrays = [['A', 'A', 'B', 'B'], ['X', 'Y', 'X', 'Y']]
index = pd.MultiIndex.from_arrays(arrays, names=('Group', 'Subgroup'))
data = {
'Value1': [10, 20, 30, 40],
'Value2': [50, 60, 70, 80]
}
df = pd.DataFrame(data, index=index)
# Access a cross-section for group 'A'
print("Cross-section for Group 'A':")
print(df.xs('A'))
Output
Cross-section for Group 'A':
Value1 Value2
Subgroup
X 10 50
Y 20 60
Accessing Data Along a Specific Level
Use the level
parameter to access data at a specific level of the MultiIndex.
Python Program
import pandas as pd
# Create a DataFrame with a MultiIndex
arrays = [['A', 'A', 'B', 'B'], ['X', 'Y', 'X', 'Y']]
index = pd.MultiIndex.from_arrays(arrays, names=('Group', 'Subgroup'))
data = {
'Value1': [10, 20, 30, 40],
'Value2': [50, 60, 70, 80]
}
df = pd.DataFrame(data, index=index)
# Access data along the 'Subgroup' level
print("Cross-section for Subgroup 'X':")
print(df.xs('X', level='Subgroup'))
Output
Cross-section for Subgroup 'X':
Value1 Value2
Group
A 10 50
B 30 70
Accessing Data Along Columns
Set axis=1
to retrieve a cross-section along columns.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Group': ['A', 'A', 'B', 'B'],
'Subgroup': ['X', 'Y', 'X', 'Y'],
'Value1': [10, 20, 30, 40],
'Value2': [50, 60, 70, 80]
}
df = pd.DataFrame(data)
# Set 'Group' and 'Subgroup' as the index
df.set_index(['Group', 'Subgroup'], inplace=True)
# Access a column using xs
print("Cross-section for column 'Value1':")
print(df.xs('Value1', axis=1))
Output
Cross-section for column 'Value1':
Group Subgroup
A X 10
Y 20
B X 30
Y 40
Name: Value1, dtype: int64
Using drop_level=False
Set drop_level=False
to retain the selected level in the output.
Python Program
import pandas as pd
# Create a DataFrame with a MultiIndex
arrays = [['A', 'A', 'B', 'B'], ['X', 'Y', 'X', 'Y']]
index = pd.MultiIndex.from_arrays(arrays, names=('Group', 'Subgroup'))
data = {
'Value1': [10, 20, 30, 40],
'Value2': [50, 60, 70, 80]
}
df = pd.DataFrame(data, index=index)
# Access a cross-section without dropping the level
print("Cross-section for Group 'A' without dropping the level:")
print(df.xs('A', drop_level=False))
Output
Cross-section for Group 'A' without dropping the level:
Value1 Value2
Group Subgroup
A X 10 50
Y 20 60
Summary
In this tutorial, we explored the DataFrame.xs
method in pandas. Key takeaways include:
- Using
xs
to retrieve cross-sections of a DataFrame along rows or columns. - Accessing data at specific levels in MultiIndex DataFrames.
- Controlling whether to drop the selected level using
drop_level
.
The DataFrame.xs
method is a powerful tool for working with MultiIndex DataFrames and extracting specific subsets of data.