pandas 1.4.2

NotesParametersReturnsBackRef
xs(self, key, axis=0, level=None, drop_level: 'bool_t' = True)

This method takes a :None:None:`key` argument to select data at a particular level of a MultiIndex.

Notes

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 <advanced.mi_slicers> .

Parameters

key : label or tuple of label

Label contained in the index, or partially in a MultiIndex.

axis : {0 or 'index', 1 or 'columns'}, default 0

Axis to retrieve cross-section on.

level : object, defaults to first n levels (n=1 or len(key))

In case of a key partially contained in a MultiIndex, indicate which levels are used. Levels can be referred by label or position.

drop_level : bool, default True

If False, returns object with same levels as self.

Returns

Series or DataFrame

Cross-section from the original Series or DataFrame corresponding to the selected index levels.

Return cross-section from the Series/DataFrame.

See Also

DataFrame.iloc

Purely integer-location based indexing for selection by position.

DataFrame.loc

Access a group of rows and columns by label(s) or a boolean array.

Examples

This example is valid syntax, but we were not able to check execution
>>> 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

This example is valid syntax, but we were not able to check execution
>>> 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

This example is valid syntax, but we were not able to check execution
>>> df.xs(('mammal', 'dog'))
            num_legs  num_wings
locomotion
walks              4          0

Get values at specified index and level

This example is valid syntax, but we were not able to check execution
>>> df.xs('cat', level=1)
                   num_legs  num_wings
class  locomotion
mammal walks              4          0

Get values at several indexes and levels

This example is valid syntax, but we were not able to check execution
>>> df.xs(('bird', 'walks'),
...  level=[0, 'locomotion']) num_legs num_wings animal penguin 2 2

Get values at specified column and axis

This example is valid syntax, but we were not able to check execution
>>> 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
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.generic.NDFrame.xs

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/generic.py#3722
type: <class 'function'>
Commit: