這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)使用pandas怎么層次化索引,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)建一個Series,并用一個由列表或數(shù)組組成的列表作為索引。
data=Series(np.random.randn(10), index=[['a','a','a','b','b','b','c','c','d','d'], [1,2,3,1,2,3,1,2,2,3]]) data Out[6]: a 1 -2.842857 2 0.376199 3 -0.512978 b 1 0.225243 2 -1.242407 3 -0.663188 c 1 -0.149269 2 -1.079174 d 2 -0.952380 3 -1.113689 dtype: float64
這就是帶MultiIndex索引的Series的格式化輸出形式。索引之間的“間隔”表示“直接使用上面的標簽”。
data.index Out[7]: MultiIndex(levels=[['a', 'b', 'c', 'd'], [1, 2, 3]], labels=[[0, 0, 0, 1, 1, 1, 2, 2, 3, 3], [0, 1, 2, 0, 1, 2, 0, 1, 1, 2]])
對于一個層次化索引的對象,選取數(shù)據(jù)子集的操作很簡單:
data['b'] Out[8]: 1 0.225243 2 -1.242407 3 -0.663188 dtype: float64 data['b':'c'] Out[10]: b 1 0.225243 2 -1.242407 3 -0.663188 c 1 -0.149269 2 -1.079174 dtype: float64 data.ix[['b','d']] __main__:1: DeprecationWarning: .ix is deprecated. Please use .loc for label based indexing or .iloc for positional indexing See the documentation here: http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated Out[11]: b 1 0.225243 2 -1.242407 3 -0.663188 d 2 -0.952380 3 -1.113689 dtype: float64
甚至可以在“內(nèi)層”中進行選取:
data[:,2] Out[12]: a 0.376199 b -1.242407 c -1.079174 d -0.952380 dtype: float64
層次化索引在數(shù)據(jù)重塑和基于分組的操作中扮演重要角色。
可以通過unstack方法被重新安排到一個DataFrame中:
data.unstack() Out[13]: 1 2 3 a -2.842857 0.376199 -0.512978 b 0.225243 -1.242407 -0.663188 c -0.149269 -1.079174 NaN d NaN -0.952380 -1.113689 #unstack的逆運算是stack data.unstack().stack() Out[14]: a 1 -2.842857 2 0.376199 3 -0.512978 b 1 0.225243 2 -1.242407 3 -0.663188 c 1 -0.149269 2 -1.079174 d 2 -0.952380 3 -1.113689 dtype: float64
對于DataFrame,每條軸都可以有分層索引:
frame=DataFrame(np.arange(12).reshape((4,3)), index=[['a','a','b','b'],[1,2,1,2]], columns=[['Ohio','Ohio','Colorado'], ['Green','Red','Green']]) frame Out[16]: Ohio Colorado Green Red Green a 1 0 1 2 2 3 4 5 b 1 6 7 8 2 9 10 11
各層都可以有名字。如果指定了名稱,它們會顯示在控制臺中(不要將索引名稱和軸標簽混為一談!)
frame.index.names=['key1','key2'] frame.columns.names=['state','color'] frame Out[22]: state Ohio Colorado color Green Red Green key1 key2 a 1 0 1 2 2 3 4 5 b 1 6 7 8 2 9 10 11
由于有了分部的列索引,可以輕松選取列分組:
frame['Ohio'] Out[23]: color Green Red key1 key2 a 1 0 1 2 3 4 b 1 6 7 2 9 10
重排分級排序
有時需要重新調(diào)整某條軸上各級別的順序,或根據(jù)指定級別上的值對數(shù)據(jù)進行排序。swaplevel接受兩個級別編號或名稱,并返回一個互換了級別的新對象(但數(shù)據(jù)不會發(fā)生變化):
frame.swaplevel('key1','key2') Out[24]: state Ohio Colorado color Green Red Green key2 key1 1 a 0 1 2 2 a 3 4 5 1 b 6 7 8 2 b 9 10 11
sortlevel則根據(jù)單個級別中的值對數(shù)據(jù)進行排序。交換級別時,常用得到sortlevel,這樣最終結(jié)果也是有序的了:
frame.swaplevel(0,1) Out[27]: state Ohio Colorado color Green Red Green key2 key1 1 a 0 1 2 2 a 3 4 5 1 b 6 7 8 2 b 9 10 11 #交換級別0,1(也就是key1,key2) #然后對axis=0進行排序 frame.swaplevel(0,1).sortlevel(0) __main__:1: FutureWarning: sortlevel is deprecated, use sort_index(level= ...) Out[28]: state Ohio Colorado color Green Red Green key2 key1 1 a 0 1 2 b 6 7 8 2 a 3 4 5 b 9 10 11
根據(jù)級別匯總統(tǒng)計
有時需要重新調(diào)整某條軸上各級別的順序,或根據(jù)指定級別上的值對數(shù)據(jù)進行排序。swaplevel接受兩個級別編號或名稱,并返回一個互換了級別的新對象(但數(shù)據(jù)不會發(fā)生變化):
frame.sum(level='key2') Out[29]: state Ohio Colorado color Green Red Green key2 1 6 8 10 2 12 14 16 frame.sum(level='color',axis=1) Out[30]: color Green Red key1 key2 a 1 2 1 2 8 4 b 1 14 7 2 20 10
使用DataFrame的列
將DataFrame的一個或多個列當做行索引來用,或?qū)⑿兴饕兂蒁ataframe 的列。
frame=DataFrame({'a':range(7),'b':range(7,0,-1), 'c':['one','one','one','two','two','two','two'], 'd':[0,1,2,0,1,2,3]}) frame Out[32]: a b c d 0 0 7 one 0 1 1 6 one 1 2 2 5 one 2 3 3 4 two 0 4 4 3 two 1 5 5 2 two 2 6 6 1 two 3
DataFrame的set_index函數(shù)會將其一個或多個列轉(zhuǎn)換為行索引,并創(chuàng)建一個新的DataFrame:
frame2=frame.set_index(['c','d']) frame2 Out[34]: a b c d one 0 0 7 1 1 6 2 2 5 two 0 3 4 1 4 3 2 5 2 3 6 1
默認情況下,那些列會從DataFrame中移除,但也可以將其保留下來:
frame.set_index(['c','d'],drop=False) Out[35]: a b c d c d one 0 0 7 one 0 1 1 6 one 1 2 2 5 one 2 two 0 3 4 two 0 1 4 3 two 1 2 5 2 two 2 3 6 1 two 3
reset_index的功能和set_index剛好相反,層次化索引的級別會被轉(zhuǎn)移到列里面:
frame2.reset_index() Out[36]: c d a b 0 one 0 0 7 1 one 1 1 6 2 one 2 2 5 3 two 0 3 4 4 two 1 4 3 5 two 2 5 2 6 two 3 6 1
上述就是小編為大家分享的使用pandas怎么層次化索引了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
當前標題:使用pandas怎么層次化索引-創(chuàng)新互聯(lián)
地址分享:http://www.yijiale78.com/article28/hhojp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站維護、營銷型網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、微信小程序、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容