比较两个pandas Series会发生什么?

30 投票
1 回答
14951 浏览
提问于 2025-04-18 18:07

我在使用pandas比较两个序列时遇到了意想不到的情况。我想知道这是正常现象还是一个错误。

假设我:

import pandas as pd
x = pd.Series([1, 1, 1, 0, 0, 0], index=['a', 'b', 'c', 'd', 'e', 'f'], name='Value')
y = pd.Series([0, 2, 0, 2, 0, 2], index=['c', 'f', 'a', 'e', 'b', 'd'], name='Value')

x > y

结果是:

a     True
b    False
c     True
d    False
e    False
f    False
Name: Value, dtype: bool

这不是我想要的。很明显,我希望索引能够对齐。但我必须明确地对齐它们才能得到我想要的结果。

x > y.reindex_like(x)

结果是:

a     True
b     True
c     True
d    False
e    False
f    False
Name: Value, dtype: bool

这才是我期待的结果。

更糟糕的是,如果我:

x + y

我得到:

a    1
b    1
c    1
d    2
e    2
f    2
Name: Value, dtype: int64

所以在进行操作时,索引是对齐的。但在比较时,它们却不对齐。我的观察准确吗?这是出于某种目的吗?

谢谢,

-PiR

1 个回答

10

不管是不是bug,我建议你可以创建一个数据框,然后在这个数据框里比较一下各个系列的数据。

import pandas as pd
x = pd.Series([1, 1, 1, 0, 0, 0], index=['a', 'b', 'c', 'd', 'e', 'f'], name='Value_x')
y = pd.Series([0, 2, 0, 2, 0, 2], index=['c', 'f', 'a', 'e', 'b', 'd'], name='Value_y')

df = pd.DataFrame({"Value_x":x, "Value_y":y})
df['Value_x'] > df['Value_y']

Out[3]:

a     True
b     True
c     True
d    False
e    False
f    False
dtype: bool

撰写回答