比较两个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解释
在上面的输出中,差异输出中有两列。一个是“self”,旁边是“other”。self指的是s1系列,而“other”指的是s2系列。
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短