pandas層次化索引

一 前言

本篇的層次化索引是一篇讀者必須要會的知識,特別對數據的分類起到很好的效果,知識追尋者文章的數據構造一向都很隨意,所以體現不出什麼直觀感受,有心的讀者可以構造有層級的數據(比如部門的層級,學科的分數層級等等)進行學習本篇文章肯定感覺大有收穫,師傅領進門,修行看個人;

二層級化索引

2.1 層級化索引

將原始的索引1至6分爲3個層級,分別是 a,b,c,如下示例

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b', 'c', 'c'], [1, 2, 3, 4, 5, 6]]
ser = pd.Series(np.random.randn(6),index)
print(ser)

輸出

a  1   -0.286724
   2   -0.619187
b  3    0.480865
   4   -0.597817
c  5   -0.165860
   6    2.628038

2.1 獲取指定層級數據

獲取指定層級數據,比如b級數據;

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b', 'c', 'c'], [1, 2, 3, 4, 5, 6]]
ser = pd.Series(np.random.randn(6),index)
level_b = ser['b']
print(level_b)

輸出

3    0.208537
4   -0.903878
dtype: float64

2.2 獲取指定值

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b', 'c', 'c'], [1, 2, 3, 4, 5, 6]]
ser = pd.Series(np.random.randn(6),index)
level_b1 = ser['b',3]
print(level_b1)

輸出

-2.278494077763927

2.3 層級切片

也可以類似字符串,列表一樣進行對索引進行切片獲取;比如想獲取b和c兩個層級的數據;

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b', 'c', 'c'], [1, 2, 3, 4, 5, 6]]
ser = pd.Series(np.random.randn(6),index)
level_bc = ser['b':'c']
print(level_bc)

輸出

b  3   -0.111179
   4   -1.018673
c  5    0.922177
   6   -1.040579
dtype: float64

當然也可以使用loc進行切片,將會出現2層索引;

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b', 'c', 'c'], [1, 2, 3, 4, 5, 6]]
ser = pd.Series(np.random.randn(6),index)
level_ab = ser.loc[['a','b']]
print(level_ab)

輸出

a  1   -0.272074
   2   -0.708729
b  3    1.277346
   4    1.080583
dtype: float64

2.4 多層級中應用unstack

之前文章提到過stack , unstack 的應用,這次使用unstack應用於多層級,實現內層級的列轉爲行

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b', 'c', 'c'], [1, 2, 3, 4, 5, 6]]
ser = pd.Series(np.random.randn(6),index)
unser = ser.unstack()
print(unser)

輸出

          1         2         3         4         5         6
a  0.452994  1.397289       NaN       NaN       NaN       NaN
b       NaN       NaN  2.400214 -0.130237       NaN       NaN
c       NaN       NaN       NaN       NaN  1.329461  1.041663

三 多軸對應多索引

如果想列有2行,索引有2行,就實現了一個數據集可以使用不同的索引列的功能,好強大;

3.1 多軸示例

索引a ,b;和 1,2,3,4 ;列 zszxz1,zszxz2; 和 u1,u2;

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b'], [1, 2, 3, 4]]
columns = [['zszxz1','zszxz2'],['u1', 'u2']]
frame = pd.DataFrame(np.random.randn(8).reshape((4,2)), index=index,columns=columns)
print(frame)

輸出

       zszxz1    zszxz2
           u1        u2
a 1 -1.239692 -0.395482
  2 -0.587833 -0.225688
b 3  1.504247  0.523000
  4 -0.996312 -0.540993

3.2 獲取單層索引值

使用loc獲取單層索引

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b'], [1, 2, 3, 4]]
columns = [['zszxz1','zszxz2'],['u1', 'u2']]
frame = pd.DataFrame(np.random.randn(8).reshape((4,2)), index=index,columns=columns)
print(frame)

print(frame.loc['a'])

輸出

     zszxz1    zszxz2
         u1        u2
1 -0.539454 -0.018574
2 -1.180073 -1.261010

3.2 獲取雙層索引值

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b'], [1, 2, 3, 4]]
columns = [['zszxz1','zszxz2'],['u1', 'u2']]
frame = pd.DataFrame(np.random.randn(8).reshape((4,2)), index=index,columns=columns)
print(frame.loc[['a']])

輸出

       zszxz1    zszxz2
           u1        u2
a 1 -0.539454 -0.018574
  2 -1.180073 -1.261010

3.3 根據外層列獲取

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b'], [1, 2, 3, 4]]
columns = [['zszxz1','zszxz2'],['u1', 'u2']]
frame = pd.DataFrame(np.random.randn(8).reshape((4,2)), index=index,columns=columns)
print(frame['zszxz1'])

輸出

           u1
a 1 -2.062139
  2  0.624969
b 3  1.050788
  4  0.088685

3.4 根據內層列獲取

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b'], [1, 2, 3, 4]]
columns = [['zszxz1','zszxz2'],['u1', 'u2']]
frame = pd.DataFrame(np.random.randn(8).reshape((4,2)), index=index,columns=columns)
print(frame['zszxz1']['u1'])

輸出

a  1    0.104911
   2    0.219530
b  3    0.816740
   4    0.793440
Name: u1, dtype: float64

3.4 根據索引列獲取指定值

想要獲取第一行第一列的值

# -*- coding: utf-8 -*-

import pandas as pd
import numpy as np

index=[['a', 'a', 'b', 'b'], [1, 2, 3, 4]]
columns = [['zszxz1','zszxz2'],['u1', 'u2']]
frame = pd.DataFrame(np.random.randn(8).reshape((4,2)), index=index,columns=columns)
print(frame.loc['a',1]['zszxz1','u1'])

輸出

2.2670422041028484

3.5 取值總結

  • 對於列可以使用中括號[]進行逐級獲取,一箇中括號[]就是一個層級;
  • 想獲取一個層級裏面的多個內容就是 [column1,column2…];

  • 對於行的獲取就是使用 loc 函數,在一個[] 中出現多值表示多個層級 [level1,levle2];
  • 出現多個[],根據不同的數據結構對於不同的行列;

四多層級構造方式說明

除了原有的顯示構造函數進行多層級構造支持如下構造方式

pd.pd.MultiIndex.from_product()

pd.pd.MultiIndex.from_tuples()

pd.MultiIndex.from_arrays()

pd.MultiIndex.from_frame()

index=[['a', 'a', 'b', 'b'], [1, 2, 3, 4]]
columns = [['zszxz1','zszxz2'],['u1', 'u2']]
frame = pd.DataFrame(np.random.randn(8).reshape((4,2)), index=pd.MultiIndex.from_arrays(index),columns=columns)
print(frame)

輸出

       zszxz1    zszxz2
           u1        u2
a 1  0.423330 -1.065528
  2 -0.231434 -0.763397
b 3 -0.185660 -0.713429
  4 -0.134907  1.489376
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章