Home  >  Article  >  Backend Development  >  How to use xs in DataFrame in Python

How to use xs in DataFrame in Python

WBOY
WBOYforward
2023-05-24 15:37:061364browse

xs Used in multiple indexes, first create a dataframe with a secondary row index, as shown below:

np.arrays = [['one', 'one', 'one', 'two', 'two', 'two'], [1, 2, 3, 1, 2, 3]]
df = pd.DataFrame(np.random.randn(6, 2), index = pd.MultiIndex.from_tuples(list(zip(*np.arrays))), columns = ['A', 'B'])
df

The data set is displayed as follows:

How to use xs in DataFrame in Python

1. Take one row

Use df.loc[‘one’]

How to use xs in DataFrame in Python

Use df.xs(‘one’)

How to use xs in DataFrame in Python

2. Get 1 row of data from row one

Use df .xs(('one', 1))

How to use xs in DataFrame in Python

Use the loc method

How to use xs in DataFrame in Python

3. xs can also be used for column index

How to use xs in DataFrame in Python

The above is the detailed content of How to use xs in DataFrame in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete