pandas.Series.compare#

Series.compare(other, align_axis=1, keep_shape=False, keep_equal=False, result_names=('self', 'other'))[源代码]#

与另一个 Series 进行比较并显示差异。

Parameters:
otherSeries

要比较的对象。

align_axis{0 或 ‘index’,1 或 ‘columns’},默认 1

确定对齐比较的轴。

  • 0 或 ‘index’结果差异垂直堆叠

    交替从 self 和 other 中提取行。

  • 1 或 ‘columns’结果差异水平对齐

    交替从 self 和 other 中提取列。

keep_shapebool,默认 False

如果为 true,则保留所有行和列。否则,只保留那些具有不同值的行和列。

keep_equalbool,默认 False

如果为 true,则结果保留相等的值。否则,相等的值显示为 NaN。

result_namestuple, 默认 (‘self’, ‘other’)

设置比较时 DataFrame 的名称。

在 1.5.0 版本加入.

Returns:
Series 或 DataFrame

如果 axis 是 0 或 ‘index’,结果将是一个 Series。结果索引将是一个 MultiIndex,其中 ‘self’ 和 ‘other’ 在内层交替堆叠。

如果 axis 是 1 或 ‘columns’,结果将是一个 DataFrame。它将有两个列,名为 ‘self’ 和 ‘other’。

参见

DataFrame.compare

与另一个 DataFrame 进行比较并显示差异。

Notes

匹配的 NaN 不会显示为差异。

Examples

>>> s1 = pd.Series(["a", "b", "c", "d", "e"])
>>> s2 = pd.Series(["a", "a", "c", "b", "e"])

按列对齐差异。

>>> s1.compare(s2)
  self other
1    b     a
3    d     b

堆叠索引上的差异

>>> s1.compare(s2, align_axis=0)
1  self     b
   other    a
3  self     d
   other    b
dtype: object

保留所有原始行

>>> s1.compare(s2, keep_shape=True)
  self other
0  NaN   NaN
1    b     a
2  NaN   NaN
3    d     b
4  NaN   NaN

保留所有原始行以及所有原始值

>>> s1.compare(s2, keep_shape=True, keep_equal=True)
  self other
0    a     a
1    b     a
2    c     c
3    d     b
4    e     e