比较两个 Pandas 序列并打印出差异
在这个程序中,我们将比较两个 Pandas 序列,并打印出序列中的差异。这里所说的差异是指元素不匹配的索引位置。
算法
Step 1: Define two Pandas series, s1 and s2. Step 2: Compare the series using compare() function in the Pandas series. Step 3: Print their difference.
示例代码
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("\nDifference between the series: \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 Difference between the series: self other 1 20.0 30.0 4 50.0 55.0
说明
在上方的输出中,差异输出中有两列。一列是“自我”,另一列是“其他”。“自我”指 s1 序列,而“其他”指 s2 序列。
广告