比较两个Pandas序列并打印差异

比较两个Pandas序列并打印差异

在这个程序中,我们将比较两个Pandas序列,并打印出序列中的差异。所说的差异是指元素不匹配的索引位置。

算法

步骤1:定义两个Pandas序列s1和s2。
步骤2:使用Pandas序列的compare()函数比较这两个序列。
步骤3:打印它们的差异。

示例程序

import pandas as pd

s1 = pd.Series([10,20,30,40,50,60])
s2 = pd.Series([10,30,30,40,55,60])

print("S1:\n", s1)
print("\nS2:\n", s2)

difference = s1.compare(s2)
print("\n序列之间的差异:\n",difference)

输出结果

S1:
0    10
1    20
2    30
3    40
4    50
5    60
dtype: int64

S2:
0    10
1    30
2    30
3    40
4    55
5    60
dtype: int64

序列之间的差异:
    self  other
1  20.0   30.0
4  50.0   55.0

解释

在上面的输出中,差异输出中有两列。一个是“self” (自己),相邻的是“other” (其他)。self指代s1序列,而“other”指代s2序列。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程