Pandas之MultiIndex對(duì)象的示例詳解
約定
import pandas as pd from pandas import DataFrame import numpy as np
MultiIndex
MultiIndex表示多級(jí)索引,它是從Index繼承過(guò)來(lái)的,其中多級(jí)標(biāo)簽用元組對(duì)象來(lái)表示。
一、創(chuàng)建MultiIndex對(duì)象
創(chuàng)建方式一:元組列表
m_index1=pd.Index([("A","x1"),("A","x2"),("B","y1"),("B","y2"),("B","y3")],name=["class1","class2"])
m_index1
代碼結(jié)果:
MultiIndex(levels=[['A', 'B'], ['x1', 'x2', 'y1', 'y2', 'y3']],
labels=[[0, 0, 1, 1, 1], [0, 1, 2, 3, 4]],
names=['class1', 'class2'])
df1=DataFrame(np.random.randint(1,10,(5,3)),index=m_index1) df1
代碼結(jié)果:
| 0 | 1 | 2 | ||
|---|---|---|---|---|
| class1 | class2 | |||
| A | x1 | 7 | 4 | 8 |
| x2 | 4 | 5 | 2 | |
| B | y1 | 6 | 9 | 7 |
| y2 | 2 | 1 | 6 | |
| y3 | 6 | 8 | 6 |
創(chuàng)建方式二:特定結(jié)構(gòu)
例如**from_arrays()
class1=["A","A","B","B"] class2=["x1","x2","y1","y2"] m_index2=pd.MultiIndex.from_arrays([class1,class2],names=["class1","class2"]) m_index2
代碼結(jié)果:
MultiIndex(levels=[['A', 'B'], ['x1', 'x2', 'y1', 'y2']],
labels=[[0, 0, 1, 1], [0, 1, 2, 3]],
names=['class1', 'class2'])
df2=DataFrame(np.random.randint(1,10,(4,3)),index=m_index2) df2
代碼結(jié)果:
| 0 | 1 | 2 | ||
|---|---|---|---|---|
| class1 | class2 | |||
| A | x1 | 2 | 4 | 5 |
| x2 | 3 | 5 | 9 | |
| B | y1 | 7 | 1 | 2 |
| y2 | 3 | 1 | 8 |
創(chuàng)建方式三:笛卡爾積
from_product()從多個(gè)集合的笛卡爾積創(chuàng)建MultiIndex對(duì)象。
m_index3=pd.MultiIndex.from_product([["A","B"],['x1','y1']],names=["class1","class2"]) m_index3
代碼結(jié)果:
MultiIndex(levels=[['A', 'B'], ['x1', 'y1']],
labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
names=['class1', 'class2'])
df3=DataFrame(np.random.randint(1,10,(2,4)),columns=m_index3) df3
代碼結(jié)果:
| class1 | A | B | ||
|---|---|---|---|---|
| class2 | x1 | y1 | x1 | y1 |
| 0 | 2 | 9 | 1 | 8 |
| 1 | 5 | 2 | 5 | 2 |
二、MultiIndex對(duì)象屬性
df1
代碼結(jié)果:
| 0 | 1 | 2 | ||
|---|---|---|---|---|
| class1 | class2 | |||
| A | x1 | 7 | 4 | 8 |
| x2 | 4 | 5 | 2 | |
| B | y1 | 6 | 9 | 7 |
| y2 | 2 | 1 | 6 | |
| y3 | 6 | 8 | 6 |
m_index4=df1.index print(in1[0])
代碼結(jié)果:
('A', 'x1')
調(diào)用.get_loc()和.get_indexer()獲取標(biāo)簽的下標(biāo):
print(m_index4.get_loc(("A","x2")))
print(m_index4.get_indexer([("A","x2"),("B","y1"),"nothing"]))
代碼結(jié)果:
1
[ 1 2 -1]
MultiIndex對(duì)象使用多個(gè)Index對(duì)象保存索引中每一級(jí)的標(biāo)簽:
print(m_index4.levels[0]) print(m_index4.levels[1])
代碼結(jié)果:
Index(['A', 'B'], dtype='object', name='class1') Index(['x1', 'x2', 'y1', 'y2', 'y3'], dtype='object', name='class2')
MultiIndex對(duì)象還有屬性labels保存標(biāo)簽的下標(biāo):
print(m_index4.labels[0]) print(m_index4.labels[1])
代碼結(jié)果:
FrozenNDArray([0, 0, 1, 1, 1], dtype='int8') FrozenNDArray([0, 1, 2, 3, 4], dtype='int8')
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python?flask框架中多種查詢(xún)參數(shù)的獲取方式
這篇文章主要介紹了pythonflask框架的生命周期以及多種查詢(xún)參數(shù)的獲取方式,文章通過(guò)代碼示例和圖文講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-03-03
關(guān)于pandas.date_range()的用法及說(shuō)明
這篇文章主要介紹了關(guān)于pandas.date_range()的用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
解決Matplotlib圖表不能在Pycharm中顯示的問(wèn)題
今天小編就為大家分享一篇解決Matplotlib圖表不能在Pycharm中顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
使用Python進(jìn)行數(shù)據(jù)清洗和預(yù)處理的實(shí)現(xiàn)代碼
Python作為數(shù)據(jù)科學(xué)領(lǐng)域的熱門(mén)編程語(yǔ)言,提供了豐富的庫(kù)和工具來(lái)處理和清洗數(shù)據(jù),本文將介紹如何使用Python進(jìn)行數(shù)據(jù)清洗和預(yù)處理,并提供相應(yīng)的代碼示例,需要的朋友可以參考下2024-05-05
Pytorch中TensorDataset與DataLoader的使用方式
這篇文章主要介紹了Pytorch中TensorDataset與DataLoader的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
PyCharm 無(wú)法 import pandas 程序卡住的解決方式
這篇文章主要介紹了PyCharm 無(wú)法 import pandas 程序卡住的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
這可能是最好玩的python GUI入門(mén)實(shí)例(推薦)
這篇文章主要介紹了這可能是最好玩的python GUI入門(mén)實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

