首页 > 文章列表 > Python中DataFrame中的xs怎么使用

Python中DataFrame中的xs怎么使用

Python DataFrame
316 2023-04-29

Python中DataFrame中的xs怎么使用

xs用于多重索引中,先创立一个二级行索引的dataframe,如下所示:

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

数据集如下展示:

一、取one行

使用df.loc[‘one’]

使用df.xs(‘one’)

二、取one行的1行数据

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

loc的方法

三、xs也可以用于列索引