📜  Python|熊猫系列.get()

📅  最后修改于: 2022-05-13 01:54:36.698000             🧑  作者: Mango

Python|熊猫系列.get()

Pandas 系列是带有轴标签的一维 ndarray。标签不必是唯一的,但必须是可散列的类型。该对象支持基于整数和基于标签的索引,并提供了许多用于执行涉及索引的操作的方法。

Pandas Series.get()函数从给定键(DataFrame 列、面板切片等)的对象中获取项目。如果未找到,则返回默认值。

示例 #1:使用Series.get()函数获取给定系列对象中传递的索引标签的值。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', None, 'Rio'])
  
# Create the Index
index_ = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5'] 
  
# set the index
sr.index = index_
  
# Print the series
print(sr)

输出 :

现在我们将使用Series.get()函数返回给定系列对象中传递的索引标签的值。

# return the value corresponding to
# the passe index label
result = sr.get(key = 'City 3')
  
# Print the result
print(result)

输出 :

正如我们在输出中看到的, Series.get()函数返回了与传递的索引标签对应的值。示例 #2:使用Series.get()函数获取给定系列对象中传递的索引标签的值。

# 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.get()函数返回给定系列对象中传递的索引标签的值。

# return the value corresponding to
# the passe index label
result = sr.get(key = '2011-03-31')
  
# Print the result
print(result)

输出 :

正如我们在输出中看到的, Series.get()函数返回了与传递的索引标签对应的值。