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

数据集如下展示:

Python中DataFrame中的xs用法

一、取one行

使用df.loc[‘one’]

Python中DataFrame中的xs用法

使用df.xs(‘one’)

Python中DataFrame中的xs用法

二、取one行的1行数据

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

Python中DataFrame中的xs用法

loc的方法

Python中DataFrame中的xs用法

三、xs也可以用于列索引

Python中DataFrame中的xs用法

原文地址:https://blog.csdn.net/wzk4869/article/details/129029235

相关文章:

  • 2022-12-23
  • 2021-04-16
  • 2022-01-12
  • 2021-11-20
  • 2022-12-23
  • 2021-06-17
  • 2021-08-06
  • 2021-05-22
猜你喜欢
  • 2022-12-23
  • 2021-09-16
  • 2021-05-01
  • 2021-12-19
  • 2021-08-29
  • 2021-09-07
相关资源
相似解决方案