DataFrame.xs(self, key, axis=0, level=None, drop_level=True)
[source]
Return cross-section from the Series/DataFrame.
This method takes a key
argument to select data at a particular level of a MultiIndex.
Parameters: |
|
---|---|
Returns: |
|
See also
DataFrame.loc
DataFrame.iloc
xs
can not be used to set values.
MultiIndex Slicers is a generic way to get/set values on any level or levels. It is a superset of xs
functionality, see MultiIndex Slicers.
>>> d = {'num_legs': [4, 4, 2, 2], ... 'num_wings': [0, 0, 2, 2], ... 'class': ['mammal', 'mammal', 'mammal', 'bird'], ... 'animal': ['cat', 'dog', 'bat', 'penguin'], ... 'locomotion': ['walks', 'walks', 'flies', 'walks']} >>> df = pd.DataFrame(data=d) >>> df = df.set_index(['class', 'animal', 'locomotion']) >>> df num_legs num_wings class animal locomotion mammal cat walks 4 0 dog walks 4 0 bat flies 2 2 bird penguin walks 2 2
Get values at specified index
>>> df.xs('mammal') num_legs num_wings animal locomotion cat walks 4 0 dog walks 4 0 bat flies 2 2
Get values at several indexes
>>> df.xs(('mammal', 'dog')) num_legs num_wings locomotion walks 4 0
Get values at specified index and level
>>> df.xs('cat', level=1) num_legs num_wings class locomotion mammal walks 4 0
Get values at several indexes and levels
>>> df.xs(('bird', 'walks'), ... level=[0, 'locomotion']) num_legs num_wings animal penguin 2 2
Get values at specified column and axis
>>> df.xs('num_wings', axis=1) class animal locomotion mammal cat walks 0 dog walks 0 bat flies 2 bird penguin walks 2 Name: num_wings, dtype: int64
© 2008–2012, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/0.25.0/reference/api/pandas.DataFrame.xs.html