pandas多層索引的創(chuàng)建和取值以及排序的實(shí)現(xiàn)
多層索引的創(chuàng)建
普通-多個(gè)index創(chuàng)建
- 在創(chuàng)建數(shù)據(jù)的時(shí)候加入一個(gè)index列表,這個(gè)index列表里面是多個(gè)索引列表
Series多層索引的創(chuàng)建方法
import pandas as pd s = pd.Series([1,2,3,4,5,6],index=[['張三','張三','李四','李四','王五','王五'], ['期中','期末','期中','期末','期中','期末']]) # print(s) s
張三 期中 1
期末 2
李四 期中 3
期末 4
王五 期中 5
期末 6
dtype: int64
利用 numpy中的隨機(jī)數(shù)
import numpy as np data = np.random.randint(0,100,size=(6,3)) # np.random.randint(0,100,size=(6,3))是使用numpy中的隨機(jī)模塊random中,生成隨機(jī)整數(shù)方法randint, # 里面的參數(shù)size是指定生成6行3列的數(shù)據(jù),并且每個(gè)數(shù)字的范圍在0到100之間 data
array([[44, 66, 67], [82, 52, 0], [34, 78, 23], [38, 4, 43], [60, 62, 40], [57, 9, 11]])
Dataframe多層索引創(chuàng)建
import pandas as pd import numpy as np data = np.random.randint(0,100,size=(6,3)) df = pd.DataFrame(data,index=[['張三','張三','李四','李四','王五','王五'], ['期中','期末','期中','期末','期中','期末']], columns=['Java','Web','Python']) df
Java | Web | Python | ||
---|---|---|---|---|
張三 | 期中 | 68 | 4 | 90 |
期末 | 33 | 63 | 73 | |
李四 | 期中 | 30 | 13 | 68 |
期末 | 14 | 18 | 48 | |
王五 | 期中 | 34 | 66 | 26 |
期末 | 89 | 10 | 35 |
簡(jiǎn)化創(chuàng)建-from_product()
import pandas as pd import numpy as np data = np.random.randint(0,100,size=(6,3)) names = ['張三','李四','王五'] exam = ['期中','期末'] index = pd.MultiIndex.from_product([names,exam]) df = pd.DataFrame(data,index=index,columns=['Java','Web','Python']) # print(df) df
Java | Web | Python | ||
---|---|---|---|---|
張三 | 期中 | 51 | 78 | 47 |
期末 | 39 | 53 | 36 | |
李四 | 期中 | 33 | 60 | 83 |
期末 | 90 | 55 | 3 | |
王五 | 期中 | 37 | 45 | 66 |
期末 | 6 | 82 | 71 |
from_product()在這個(gè)里面的列表中位置不同, 產(chǎn)生的索引頁(yè)會(huì)不同
index = pd.MultiIndex.from_product([exam, names]) df = pd.DataFrame(data,index=index,columns=['Java','Web','Python']) # print(df) df
Java | Web | Python | ||
---|---|---|---|---|
期中 | 張三 | 51 | 78 | 47 |
李四 | 39 | 53 | 36 | |
王五 | 33 | 60 | 83 | |
期末 | 張三 | 90 | 55 | 3 |
李四 | 37 | 45 | 66 | |
王五 | 6 | 82 | 71 |
from_product([exam,names])會(huì)將列表中第一個(gè)元素作為最外層索引,依次類(lèi)推
多層索引的取值
獲取到我們想要的數(shù)據(jù)
獲取多層索引Series中的數(shù)據(jù)
創(chuàng)建數(shù)據(jù)
import pandas as pd s = pd.Series([1,2,3,4,5,6],index=[['張三','張三','李四','李四','王五','王五'], ['期中','期末','期中','期末','期中','期末']]) print(s)
張三 期中 1
期末 2
李四 期中 3
期末 4
王五 期中 5
期末 6
dtype: int64
可以直接使用[]的方式取最外面的一個(gè)層級(jí) s[‘張三']
s['李四'] # 注意:[]取值方式,不可直接使用最外層以外的其他層級(jí),例如:s['期末']
期中 3
期末 4
dtype: int64
使用['外索引', '內(nèi)索引'], 獲取某個(gè)數(shù)據(jù)
注意:[‘張三',‘期末']他們的順序不能變。剝洋蔥原則,從外到內(nèi)一層一層的剝。
s['李四', '期中'] # 李四期中分值 # 注意:['張三','期末']他們的順序不能變。剝洋蔥原則,從外到內(nèi)一層一層的剝。
3
使用[]的切片,獲取數(shù)據(jù)s[:,‘期中']
s[:,'期中'] # 第一個(gè)值為全部的外索引
張三 1
李四 3
王五 5
dtype: int64
使用 loc
- loc 使用的是標(biāo)簽suoyin
- iloc使用的是位置索引
# loc 使用方式與 [] 的方式基本一樣 s.loc['張三'] s.loc['張三','期中'] s.loc[:,'期中'] # iloc 的取值并不會(huì)受多層索引影響,只會(huì)根據(jù)數(shù)據(jù)的位置索引進(jìn)行取值, 不推薦
張三 1
李四 3
王五 5
dtype: int64
多層索引DataFrame的取值
在對(duì)多層索引DataFrame的取值是,推薦使用 loc() 函數(shù)
import pandas as pd import numpy as np #size參數(shù)是指定生成6行3列的數(shù)組 data = np.random.randint(0,100,size=(6,3)) names = ['張三','李四','王五'] exam = ['期中','期末'] index = pd.MultiIndex.from_product([names,exam]) df = pd.DataFrame(data,index=index,columns=['Java','Web','Python']) df
Java | Web | Python | ||
---|---|---|---|---|
張三 | 期中 | 3 | 40 | 52 |
期末 | 74 | 38 | 85 | |
李四 | 期中 | 7 | 28 | 16 |
期末 | 9 | 25 | 0 | |
王五 | 期中 | 13 | 24 | 8 |
期末 | 49 | 46 | 1 |
三種方式都可以獲取張三期中各科成績(jī)
# df.loc['張三','期中'] # df.loc['張三'].loc['期中'] # df.loc[('張三','期中')]
注意:DataFrame中對(duì)行索引的時(shí)候和Series有一個(gè)同樣的注意點(diǎn),就是無(wú)法直接對(duì)二級(jí)索引直接進(jìn)行索引,必須讓二級(jí)索引變成一級(jí)索引后才能對(duì)其進(jìn)行索引
多層索引的排序
- 使用sort_index() 排序
- level參數(shù)可以指定是否按照指定的層級(jí)進(jìn)行排列
- 第一層索引值為0, 第二層索引的值為1
創(chuàng)建數(shù)據(jù)
import pandas as pd data = np.random.randint(0,100,size=(9,3)) key1 = ['b','c','a'] key2 = [2,1,3] index = pd.MultiIndex.from_product([key1,key2]) df = pd.DataFrame(data,index=index,columns=['Java','Web','Python']) df
Java | Web | Python | ||
---|---|---|---|---|
b | 2 | 56 | 82 | 81 |
1 | 84 | 16 | 55 | |
3 | 35 | 25 | 86 | |
c | 2 | 76 | 1 | 76 |
1 | 36 | 28 | 94 | |
3 | 79 | 70 | 97 | |
a | 2 | 25 | 17 | 30 |
1 | 38 | 38 | 78 | |
3 | 41 | 75 | 90 |
排序
- DataFrame按行索引排序的方法是sort_index()
- 如果直接使用的話,不傳參數(shù), 會(huì)把每一層索引根據(jù)值進(jìn)行升序排序
df.sort_index()
Java | Web | Python | ||
---|---|---|---|---|
a | 1 | 18 | 60 | 74 |
2 | 66 | 87 | 27 | |
3 | 96 | 18 | 64 | |
b | 1 | 72 | 58 | 52 |
2 | 22 | 31 | 22 | |
3 | 31 | 12 | 83 | |
c | 1 | 6 | 54 | 96 |
2 | 9 | 47 | 18 | |
3 | 31 | 63 | 4 |
# 當(dāng)level=0時(shí),ascending=False, 會(huì)根據(jù)第一層索引值進(jìn)行降序排序 df.sort_index(level=0,ascending=False)
Java | Web | Python | ||
---|---|---|---|---|
c | 3 | 79 | 70 | 97 |
2 | 76 | 1 | 76 | |
1 | 36 | 28 | 94 | |
b | 3 | 35 | 25 | 86 |
2 | 56 | 82 | 81 | |
1 | 84 | 16 | 55 | |
a | 3 | 41 | 75 | 90 |
2 | 25 | 17 | 30 | |
1 | 38 | 38 | 78 |
# 當(dāng)level=1時(shí),會(huì)根據(jù)第二層索引值進(jìn)行降序排序 df.sort_index(level=1,ascending=False) # 數(shù)據(jù)會(huì)根據(jù)第二層索引值進(jìn)行相應(yīng)的降序排列, # 如果索引值相同時(shí)會(huì)根據(jù)其他層索引值排列
Java | Web | Python | ||
---|---|---|---|---|
c | 3 | 79 | 70 | 97 |
b | 3 | 35 | 25 | 86 |
a | 3 | 41 | 75 | 90 |
c | 2 | 76 | 1 | 76 |
b | 2 | 56 | 82 | 81 |
a | 2 | 25 | 17 | 30 |
c | 1 | 36 | 28 | 94 |
b | 1 | 84 | 16 | 55 |
a | 1 | 38 | 38 | 78 |
通過(guò)level設(shè)置排序的索引層級(jí),其他層索引也會(huì)根據(jù)其排序規(guī)則進(jìn)行排序
到此這篇關(guān)于pandas多層索引的創(chuàng)建和取值以及排序的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)pandas多層索引內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python?異步之如何獲取當(dāng)前和正在運(yùn)行任務(wù)詳解
這篇文章主要為大家介紹了Python?異步之如何獲取當(dāng)前和正在運(yùn)行任務(wù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Python List remove()實(shí)例用法詳解
在本篇內(nèi)容里小編給大家整理了一篇關(guān)于Python List remove()方法及實(shí)例,有需要的朋友們跟著學(xué)習(xí)下。2021-08-08Pytorch-Geometric中的Message?Passing使用及說(shuō)明
這篇文章主要介紹了Pytorch-Geometric中的Message?Passing使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12python利用Opencv實(shí)現(xiàn)人臉識(shí)別功能
這篇文章主要為大家詳細(xì)介紹了python利用Opencv實(shí)現(xiàn)人臉識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Python實(shí)現(xiàn)統(tǒng)計(jì)英文單詞個(gè)數(shù)及字符串分割代碼
這篇文章主要介紹了Python實(shí)現(xiàn)統(tǒng)計(jì)英文單詞個(gè)數(shù)及字符串分割方法,本文分別給出代碼實(shí)例,需要的朋友可以參考下2015-05-05