當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Pandas MultiIndex.names用法及代碼示例

Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。

Pandas MultiIndex.names屬性返回MultiIndex中級別的名稱。

用法: MultiIndex.names

範例1:采用MultiIndex.names屬性以在MultiIndex中找到級別的名稱。

# importing pandas as pd 
import pandas as pd 
  
# Creating the array 
array = [[1, 2, 3], ['Sharon', 'Nick', 'Bailey']] 
  
# Print the array 
print(array)

輸出:

現在,我們使用該數組創建MultiIndex

# Creating the MultiIndex 
midx = pd.MultiIndex.from_arrays(array, names =('Number', 'Names')) 
  
# Print the MultiIndex 
print(midx)

輸出:

現在,我們將在MultiIndex中找到所有級別的名稱。

# Print the names of the level in MultiIndex 
midx.names

輸出:

從輸出中可以看到,midx有兩個級別,級別的名稱為“ Number”和“ Names”。

範例2:采用MultiIndex.names屬性以在給定的MultiIndex中查找級別的名稱。

# importing pandas as pd 
import pandas as pd 
  
# Creating the array 
array =[[1, 2, 3], ['Sharon', 'Nick', 'Bailey'],  
           ['Doctor', 'Scientist', 'Physicist']] 
  
# Print the array 
print(array)

輸出:

現在,我們使用該數組創建MultiIndex

# Creating the MultiIndex 
midx = pd.MultiIndex.from_arrays(array,  
   names =('Ranking', 'Names', 'Profession')) 
  
# Print the MultiIndex 
print(midx)

輸出:

現在,我們將在MultiIndex中找到所有級別的名稱。

# Print the names of the level in MultiIndex 
midx.names

輸出:

從輸出中可以看到,midx具有三個級別,級別的名稱為“ Number”,“ Names”和“ Profession”。



相關用法


注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas MultiIndex.names。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。