How to use xs in DataFrame in Python

WBOY
Release: 2023-05-24 15:37:06
forward
1367 people have browsed it

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
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template