首頁  >  文章  >  後端開發  >  Python中DataFrame中的xs怎麼使用

Python中DataFrame中的xs怎麼使用

WBOY
WBOY轉載
2023-05-24 15:37:061329瀏覽

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怎麼使用

#

以上是Python中DataFrame中的xs怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除