pandas 比较 2 个熊猫系列时会发生什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25435229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What happens when you compare 2 pandas Series
提问by piRSquared
I ran up against unexpected behavior in pandas when comparing two series. I wanted to know if this is intended or a bug.
在比较两个系列时,我遇到了Pandas的意外行为。我想知道这是故意的还是错误。
suppose I:
假设我:
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
yields:
产量:
a True
b False
c True
d False
e False
f False
Name: Value, dtype: bool
which isn't what I wanted. Clearly, I expected the indexes to line up. But I have to explicitly line them up to get the desired results.
这不是我想要的。显然,我预计索引会对齐。但是我必须明确地将它们排列起来才能获得所需的结果。
x > y.reindex_like(x)
yields:
产量:
a True
b True
c True
d False
e False
f False
Name: Value, dtype: bool
Which is what I expected.
这正是我所期望的。
What's worse is if I:
更糟糕的是,如果我:
x + y
I get:
我得到:
a 1
b 1
c 1
d 2
e 2
f 2
Name: Value, dtype: int64
So when operating, the indexes line up. When comparing, they do not. Is my observation accurate? Is this intended for some purpose?
所以在操作的时候,指标是对齐的。比较时,他们没有。我的观察准确吗?这是出于某种目的吗?
Thanks,
谢谢,
-PiR
-PiR
回答by firelynx
Bug or not. I would suggest to make a dataframe and compare the series inside the dataframe.
错误与否。我建议制作一个数据框并比较数据框内的系列。
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

