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)生的索引頁會(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è)元素作為最外層索引,依次類推
多層索引的取值
獲取到我們想要的數(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),就是無法直接對(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 |
通過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學(xué)習(xí)筆記之open()函數(shù)打開文件路徑報(bào)錯(cuò)問題
這篇文章主要介紹了Python學(xué)習(xí)筆記之open()函數(shù)打開文件路徑報(bào)錯(cuò)問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Python區(qū)塊鏈創(chuàng)世塊創(chuàng)建教程
這篇文章主要為大家介紹了Python區(qū)塊鏈創(chuàng)世塊創(chuàng)建教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python利用xmltodict實(shí)現(xiàn)字典和xml互相轉(zhuǎn)換的示例代碼
xmltodict是一個(gè)Python第三方庫,用于處理XML數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
Python標(biāo)準(zhǔn)庫中隱藏的利器(示例詳解)
在命令行中直接使用Python標(biāo)準(zhǔn)庫的模塊,最大的好處就是就是不用寫代碼,就能使用其中的功能,當(dāng)臨時(shí)需要一些某些功能的時(shí)候,用這種方式會(huì)快捷,方便很多,這篇文章主要介紹了Python標(biāo)準(zhǔn)庫中隱藏的利器,需要的朋友可以參考下2023-11-11
Python?encode()方法和decode()方法詳解
encode() 方法為字符串類型(str)提供的方法,用于將 str 類型轉(zhuǎn)換成 bytes 類型,這個(gè)過程也稱為“編碼”,這篇文章主要介紹了Python?encode()方法和decode()方法,需要的朋友可以參考下2022-12-12
Python 實(shí)戰(zhàn)開發(fā)校園管理系統(tǒng)詳細(xì)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python開發(fā)一套校園管理系統(tǒng),包含各種人員,如教師、學(xué)生等。學(xué)校的系統(tǒng)通常還包括一些課程的信息,大家可以在過程中查缺補(bǔ)漏,提升水平2021-10-10
Django JSonResponse對(duì)象的實(shí)現(xiàn)
本文主要介紹了Django JSonResponse對(duì)象的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

