比較兩個 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 序列。
廣告