📜  如何比较两个熊猫系列的元素?

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

如何比较两个熊猫系列的元素?

有时我们需要比较pandas系列来进行一些比较分析。可以在关系运算符的帮助下比较两个熊猫系列,我们可以轻松地一次比较两个系列的相应元素。结果将以 True 或 False 的形式显示。我们也可以使用像Pandas Series.equals()这样的函数 比较两个熊猫系列。

方法 1:使用关系运算符

示例 1:检查两个 Series 元素是否相等

Python3
# Importing pandas library
import pandas as pd
  
# Creating 2 pandas Series
ps1 = pd.Series([2.5, 4, 6, 8, 10, 1.75, 40])
ps2 = pd.Series([1.5, 3, 5, 7, 10, 1.75, 20])
  
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
  
# Compare the series using '==' and '!=' 
# Relational operators
print("\nCompare the elements of the two given Series:")
print("\nEqual:")
print(ps1 == ps2)
print("\nNot Equal:")
print(ps1 != ps2)


Python3
# Importing pandas library
import pandas as pd
  
# Creating 2 pandas Series
ps1 = pd.Series([2.5, 4, 6, 8, 10, 1.75, 40])
ps2 = pd.Series([1.5, 3, 5, 7, 10, 1.75, 20])
  
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
  
# Compare the series using '>' Relational operators
print("\nCompare the elements of the two given Series:")
print("\nGreater than:")
print(ps1 > ps2)


Python3
# Importing pandas library
import pandas as pd
  
# Creating 2 pandas Series 
ps1 = pd.Series([2.5, 4, 6, 8, 10, 1.75, 40])
ps2 = pd.Series([1.5, 3, 5, 7, 10, 1.75, 20])
  
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
  
# Compare the series using '<' Relational operators
print("\nCompare the elements of the two given Series:")
print("\nLess than:")
print(ps1 < ps2)


Python3
# Importing pandas library
import pandas as pd
  
# Creating 2 pandas Series
ps1 = pd.Series([2.5, 4, 6, 8, 10, 1.75, 40])
ps2 = pd.Series([1.5, 3, 5, 7, 10, 1.75, 20])
  
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
  
# Comparing two series using Series.equals()
# function
print("\nResult of comparing Two Series:")
result = ps1.equals(other=ps2)
print(result)


Python3
# Importing pandas library
import pandas as pd
  
# Creating 2 pandas Series
ps1 = pd.Series([80, 25, 3, 25, 24, 6])
ps2 = pd.Series([80, 25, 3, 25, 24, 6])
  
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
  
# Comparing two series using Series.equals()
# function
print("\nResult of comparing Two Series:")
result = ps1.equals(other=ps2)
print(result)


输出:

在上面的例子中,我们比较了两个系列 ' ps1 ' 和 ' ps2 ' 的元素,以检查它们是否相等。

示例 2:检查系列 1 元素是否大于系列 2

蟒蛇3

# Importing pandas library
import pandas as pd
  
# Creating 2 pandas Series
ps1 = pd.Series([2.5, 4, 6, 8, 10, 1.75, 40])
ps2 = pd.Series([1.5, 3, 5, 7, 10, 1.75, 20])
  
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
  
# Compare the series using '>' Relational operators
print("\nCompare the elements of the two given Series:")
print("\nGreater than:")
print(ps1 > ps2)

输出:

在上面的例子中,我们比较了两个系列 ' ps1 ' 和 ' ps2 ' 的元素,以检查 ps1 的元素是否大于 ps2 的元素。

示例 3:检查系列 1 元素是否小于系列 2

蟒蛇3

# Importing pandas library
import pandas as pd
  
# Creating 2 pandas Series 
ps1 = pd.Series([2.5, 4, 6, 8, 10, 1.75, 40])
ps2 = pd.Series([1.5, 3, 5, 7, 10, 1.75, 20])
  
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
  
# Compare the series using '<' Relational operators
print("\nCompare the elements of the two given Series:")
print("\nLess than:")
print(ps1 < ps2)

输出:

 在上面的例子中,我们比较了两个系列 ' ps1 ' 和 ' ps2 ' 的元素,以检查 ps1 的元素是否小于 ps2 的元素。

方法二:使用Pandas Series.equals()函数

Pandas Series.equals()函数测试两个对象是否包含相同的元素。此函数允许将两个 Series 或 DataFrame 相互比较,以查看它们是否具有相同的形状和元素。

句法:

Series.equals(other)

例子:

蟒蛇3

# Importing pandas library
import pandas as pd
  
# Creating 2 pandas Series
ps1 = pd.Series([2.5, 4, 6, 8, 10, 1.75, 40])
ps2 = pd.Series([1.5, 3, 5, 7, 10, 1.75, 20])
  
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
  
# Comparing two series using Series.equals()
# function
print("\nResult of comparing Two Series:")
result = ps1.equals(other=ps2)
print(result)

输出:

在上面的例子中,我们使用函数Series.equals() 比较给定的两个熊猫系列“ ps1”“ps2”

示例 2:

蟒蛇3

# Importing pandas library
import pandas as pd
  
# Creating 2 pandas Series
ps1 = pd.Series([80, 25, 3, 25, 24, 6])
ps2 = pd.Series([80, 25, 3, 25, 24, 6])
  
print("Series1:")
print(ps1)
print("\nSeries2:")
print(ps2)
  
# Comparing two series using Series.equals()
# function
print("\nResult of comparing Two Series:")
result = ps1.equals(other=ps2)
print(result)

输出: