[英]What happens when you compare 2 pandas Series
在比較兩個系列時,我遇到了大熊貓的意外行為。 我想知道這是故意還是錯誤。
假設我:
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
錯誤與否。 我建議制作一個數據幀並比較數據幀內的系列。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.