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


Python Pandas Series.keys()用法及代碼示例

Pandas 係列是帶有軸標簽的一維ndarray。標簽不必是唯一的,但必須是可哈希的類型。該對象同時支持基於整數和基於標簽的索引,並提供了許多方法來執行涉及索引的操作。

Pandas Series.keys()函數是索引的別名。它返回給定係列對象的索引標簽。

用法: Series.keys()

參數:沒有

返回:指數

範例1:采用Series.keys()函數返回給定係列對象的索引標簽。

# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series([10, 25, 3, 25, 24, 6]) 
  
# Create the Index 
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp'] 
  
# set the index 
sr.index = index_ 
  
# Print the series 
print(sr)

輸出:

現在我們將使用Series.keys()函數返回給定係列對象的索引標簽。

# return the keys 
result = sr.keys() 
  
# Print the result 
print(result)

輸出:


正如我們在輸出中看到的,Series.keys()函數已返回給定係列對象的所有索引標簽。

範例2:采用Series.keys()函數返回給定係列對象的索引標簽。

# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series([11, 21, 8, 18, 65, 84, 32, 10, 5, 24, 32]) 
  
# Create the Index 
index_ = pd.date_range('2010-10-09', periods = 11, freq ='M') 
  
# set the index 
sr.index = index_ 
  
# Print the series 
print(sr)

輸出:

現在我們將使用Series.keys()函數返回給定係列對象的索引標簽。

# return the keys 
result = sr.keys() 
  
# Print the result 
print(result)

輸出:

正如我們在輸出中看到的,Series.keys()函數已返回給定係列對象的所有索引標簽。



相關用法


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