Python|熊猫 Series.combine()
Pandas 系列是带有轴标签的一维 ndarray。标签不必是唯一的,但必须是可散列的类型。该对象支持基于整数和基于标签的索引,并提供了许多用于执行涉及索引的操作的方法。
Pandas Series.combine()
函数根据 func 将 Series 与 Series 或标量组合。它结合了 Series 和其他 using func 来为组合的 Series 执行元素选择。当组合的两个对象之一的某个索引处缺少值时,假定填充值。
Syntax: Series.combine(other, func, fill_value=None)
Parameter :
other : Series or scalar
func : Function that takes two scalars as inputs and returns an element.
fill_value : The value to assume when an index is missing from one Series or the other.
Returns : Series
示例 #1:使用Series.combine()
函数查找两个系列对象中每个索引标签的最大值。
# importing pandas as pd
import pandas as pd
# Creating the first Series
sr1 = pd.Series([80, 25, 3, 25, 24, 6])
# Creating the second Series
sr2 = pd.Series([34, 5, 13, 32, 4, 15])
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
# set the first index
sr1.index = index_
# set the second index
sr2.index = index_
# Print the first series
print(sr1)
# Print the second series
print(sr2)
输出 :
现在我们将使用Series.combine()
函数来查找两个给定系列对象中每个索引标签的最大值。
# find the maximum element-wise
# among sr1 and sr2
result = sr1.combine(other = sr2, func = max)
# Print the result
print(result)
输出 :
正如我们在输出中看到的, Series.combine()
函数成功地返回了两个系列对象中每个索引标签的最大值。示例 #2:使用Series.combine()
函数查找两个系列对象中每个索引标签的最小值。
# importing pandas as pd
import pandas as pd
# Creating the first Series
sr1 = pd.Series([51, 10, 24, 18, None, 84, 12, 10, 5, 24, 2])
# Creating the second Series
sr2 = pd.Series([11, 21, 8, 18, 65, 18, 32, 10, 5, 32, None])
# Create the Index
index_ = pd.date_range('2010-10-09', periods = 11, freq ='M')
# set the first index
sr1.index = index_
# set the second index
sr2.index = index_
# Print the first series
print(sr1)
# Print the second series
print(sr2)
输出 :
现在我们将使用Series.combine()
函数来查找两个给定系列对象中每个索引标签的最小值。
# find the minimum element-wise
# among sr1 and sr2
result = sr1.combine(other = sr2, func = min)
# Print the result
print(result)
输出 :
正如我们在输出中看到的, Series.combine()
函数成功地返回了两个系列对象中每个索引标签的最小值。