Pandas Series: xs() function

Cross-section from the Series/DataFrame in Pandas

The xs() function is used to get cross-section from the Series/DataFrame.

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


Series.xs(self, key, axis=0, level=None, drop_level=True)


Name Description Type/Default Value Required / Optional
key Label contained in the index, or partially in a MultiIndex.
 label or tuple of label Required
axis Axis to retrieve cross-section on. {0 or ‘index’, 1 or ‘columns’}
Default Value: 0
level In case of a key partially contained in a MultiIndex, indicate which levels are used. Levels can be referred by label or position. object
Default Value: defaults to first n levels (n=1 or len(key))
drop_level If False, returns object with same levels as self. bool
Default Value: True

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


xs can not be used to set values.

MultiIndex Slicers is a generic way to get/set values on any level or levels.


Download the above Notebook from here.

Previous: Get item and drop from frame
Next: Addition of Pandas series and other