本篇內(nèi)容介紹了“怎么使用Python的pandas庫創(chuàng)建多層次索引”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)服務(wù)商,為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站服務(wù),網(wǎng)站設(shè)計(jì),網(wǎng)站托管運(yùn)營等一站式綜合服務(wù)型公司,專業(yè)打造企業(yè)形象網(wǎng)站,讓您在眾多競爭對(duì)手中脫穎而出成都創(chuàng)新互聯(lián)公司。
pd.MultiIndex,即具有多個(gè)層次的索引。通過多層次索引,我們就可以操作整個(gè)索引組的數(shù)據(jù)。本文主要介紹在Pandas中創(chuàng)建多層索引的6種方式:
pd.MultiIndex.from_arrays():多維數(shù)組作為參數(shù),高維指定高層索引,低維指定低層索引。
pd.MultiIndex.from_tuples():元組的列表作為參數(shù),每個(gè)元組指定每個(gè)索引(高維和低維索引)。
pd.MultiIndex.from_product():一個(gè)可迭代對(duì)象的列表作為參數(shù),根據(jù)多個(gè)可迭代對(duì)象元素的笛卡爾積(元素間的兩兩組合)進(jìn)行創(chuàng)建索引。
pd.MultiIndex.from_frame:根據(jù)現(xiàn)有的數(shù)據(jù)框來直接生成
groupby():通過數(shù)據(jù)分組統(tǒng)計(jì)得到
pivot_table():生成透視表的方式來得到
In [1]:
import pandas as pd import numpy as np
通過數(shù)組的方式來生成,通常指定的是列表中的元素:
In [2]:
# 列表元素是字符串和數(shù)字 array1 = [["xiaoming","guanyu","zhangfei"], [22,25,27] ] m1 = pd.MultiIndex.from_arrays(array1) m1
Out[2]:
MultiIndex([('xiaoming', 22), ( 'guanyu', 25), ('zhangfei', 27)], )
In [3]:
type(m1) # 查看數(shù)據(jù)類型
通過type函數(shù)來查看數(shù)據(jù)類型,發(fā)現(xiàn)的確是:MultiIndex
Out[3]:
pandas.core.indexes.multi.MultiIndex
在創(chuàng)建的同時(shí)可以指定每個(gè)層級(jí)的名字:
In [4]:
# 列表元素全是字符串 array2 = [["xiaoming","guanyu","zhangfei"], ["male","male","female"] ] m2 = pd.MultiIndex.from_arrays( array2, # 指定姓名和性別 names=["name","sex"]) m2
Out[4]:
MultiIndex([('xiaoming', 'male'), ( 'guanyu', 'male'), ('zhangfei', 'female')], names=['name', 'sex'])
下面的例子是生成3個(gè)層次的索引且指定名字:
In [5]:
array3 = [["xiaoming","guanyu","zhangfei"], ["male","male","female"], [22,25,27] ] m3 = pd.MultiIndex.from_arrays( array3, names=["姓名","性別","年齡"]) m3
Out[5]:
MultiIndex([('xiaoming', 'male', 22), ( 'guanyu', 'male', 25), ('zhangfei', 'female', 27)], names=['姓名', '性別', '年齡'])
通過元組的形式來生成多層索引:
In [6]:
# 元組的形式 array4 = (("xiaoming","guanyu","zhangfei"), (22,25,27) ) m4 = pd.MultiIndex.from_arrays(array4) m4
Out[6]:
MultiIndex([('xiaoming', 22), ( 'guanyu', 25), ('zhangfei', 27)], )
In [7]:
# 元組構(gòu)成的3層索引 array5 = (("xiaoming","guanyu","zhangfei"), ("male","male","female"), (22,25,27)) m5 = pd.MultiIndex.from_arrays(array5) m5
Out[7]:
MultiIndex([('xiaoming', 'male', 22), ( 'guanyu', 'male', 25), ('zhangfei', 'female', 27)], )
最外層是列表
里面全部是元組
In [8]:
array6 = [("xiaoming","guanyu","zhangfei"), ("male","male","female"), (18,35,27) ] # 指定名字 m6 = pd.MultiIndex.from_arrays(array6,names=["姓名","性別","年齡"]) m6
Out[8]:
MultiIndex([('xiaoming', 'male', 18), ( 'guanyu', 'male', 35), ('zhangfei', 'female', 27)], names=['姓名', '性別', '年齡'] # 指定名字 )
使用可迭代對(duì)象的列表作為參數(shù),根據(jù)多個(gè)可迭代對(duì)象元素的笛卡爾積(元素間的兩兩組合)進(jìn)行創(chuàng)建索引。
在Python中,我們使用 isinstance()
函數(shù) 判斷python對(duì)象是否可迭代:
# 導(dǎo)入 collections 模塊的 Iterable 對(duì)比對(duì)象 from collections import Iterable
通過上面的例子我們總結(jié):常見的字符串、列表、集合、元組、字典都是可迭代對(duì)象
下面舉例子來說明:
In [18]:
names = ["xiaoming","guanyu","zhangfei"] numbers = [22,25] m7 = pd.MultiIndex.from_product( [names, numbers], names=["name","number"]) # 指定名字 m7
Out[18]:
MultiIndex([('xiaoming', 22), ('xiaoming', 25), ( 'guanyu', 22), ( 'guanyu', 25), ('zhangfei', 22), ('zhangfei', 25)], names=['name', 'number'])
In [19]:
# 需要展開成列表形式 strings = list("abc") lists = [1,2] m8 = pd.MultiIndex.from_product( [strings, lists], names=["alpha","number"]) m8
Out[19]:
MultiIndex([('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)], names=['alpha', 'number'])
In [20]:
# 使用元組形式 strings = ("a","b","c") lists = [1,2] m9 = pd.MultiIndex.from_product( [strings, lists], names=["alpha","number"]) m9
Out[20]:
MultiIndex([('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)], names=['alpha', 'number'])
In [21]:
# 使用range函數(shù) strings = ("a","b","c") # 3個(gè)元素 lists = range(3) # 0,1,2 3個(gè)元素 m10 = pd.MultiIndex.from_product( [strings, lists], names=["alpha","number"]) m10
Out[21]:
MultiIndex([('a', 0), ('a', 1), ('a', 2), ('b', 0), ('b', 1), ('b', 2), ('c', 0), ('c', 1), ('c', 2)], names=['alpha', 'number'])
In [22]:
# 使用range函數(shù) strings = ("a","b","c") list1 = range(3) # 0,1,2 list2 = ["x","y"] m11 = pd.MultiIndex.from_product( [strings, list1, list2], names=["name","l1","l2"] ) m11 # 總個(gè)數(shù) 3*3*2=18
總個(gè)數(shù)是``332=18`個(gè):
Out[22]:
MultiIndex([('a', 0, 'x'), ('a', 0, 'y'), ('a', 1, 'x'), ('a', 1, 'y'), ('a', 2, 'x'), ('a', 2, 'y'), ('b', 0, 'x'), ('b', 0, 'y'), ('b', 1, 'x'), ('b', 1, 'y'), ('b', 2, 'x'), ('b', 2, 'y'), ('c', 0, 'x'), ('c', 0, 'y'), ('c', 1, 'x'), ('c', 1, 'y'), ('c', 2, 'x'), ('c', 2, 'y')], names=['name', 'l1', 'l2'])
通過現(xiàn)有的DataFrame直接來生成多層索引:
df = pd.DataFrame({"name":["xiaoming","guanyu","zhaoyun"], "age":[23,39,34], "sex":["male","male","female"]}) df
直接生成了多層索引,名字就是現(xiàn)有數(shù)據(jù)框的列字段:
In [24]:
pd.MultiIndex.from_frame(df)
Out[24]:
MultiIndex([('xiaoming', 23, 'male'), ( 'guanyu', 39, 'male'), ( 'zhaoyun', 34, 'female')], names=['name', 'age', 'sex'])
通過names參數(shù)來指定名字:
In [25]:
# 可以自定義名字 pd.MultiIndex.from_frame(df,names=["col1","col2","col3"])
Out[25]:
MultiIndex([('xiaoming', 23, 'male'), ( 'guanyu', 39, 'male'), ( 'zhaoyun', 34, 'female')], names=['col1', 'col2', 'col3'])
通過groupby函數(shù)的分組功能計(jì)算得到:
In [26]:
df1 = pd.DataFrame({"col1":list("ababbc"), "col2":list("xxyyzz"), "number1":range(90,96), "number2":range(100,106)}) df1
Out[26]:
df2 = df1.groupby(["col1","col2"]).agg({"number1":sum, "number2":np.mean}) df2
查看數(shù)據(jù)的索引:
In [28]:
df2.index
Out[28]:
MultiIndex([('a', 'x'), ('a', 'y'), ('b', 'x'), ('b', 'y'), ('b', 'z'), ('c', 'z')], names=['col1', 'col2'])
通過數(shù)據(jù)透視功能得到:
In [29]:
df3 = df1.pivot_table(values=["col1","col2"],index=["col1","col2"]) df3
In [30]:
df3.index
Out[30]:
MultiIndex([('a', 'x'), ('a', 'y'), ('b', 'x'), ('b', 'y'), ('b', 'z'), ('c', 'z')], names=['col1', 'col2'])
“怎么使用Python的pandas庫創(chuàng)建多層次索引”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
當(dāng)前題目:怎么使用Python的pandas庫創(chuàng)建多層次索引
鏈接分享:http://jinyejixie.com/article24/jopije.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站制作、商城網(wǎng)站、品牌網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)