在此程序中,我们将比较两个熊猫系列,并打印该系列中的差异。通过差异,我们的意思是元素不匹配的索引位置。
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
在上面的输出中,差异输出中有两列。一个是“self”,旁边是“other”。self指的是s1系列,而“other”指的是s2系列。