2016-01-10 25 views
1

在比較數據框中的列時,我對理解熊貓行爲有些困難。我想要做的是將邏輯操作應用於不同的列,並根據邏輯結果生成結果列,其結果爲True或False。 (邏輯也可能適用於用.shift()產生的滯後列值,但我認爲這對於這個問題是不必要的。)大熊貓向量化列比較問題

問題是我理解比較df.A < df.B是矢量化(所以速度非常快),並且應該按元素結果生成一個元素。這適用於我分配給系列時,但是當我嘗試將它分配給新列時出錯。下面是一個例子:

df = pd.DataFrame(np.random.randn(10,2),index=(np.arange(10)),columns=['A','B']) 
df['C'] = False # must add column with [] notation rather than .C 
a = df.A < df.B 
df.C = A 
df 

這產生了預期的輸出:

  A   B  C 
0 1.222631 0.568988 False 
1 -0.719666 0.733197 True 
2 -2.434720 -0.131745 True 
3 0.653228 0.428794 False 
4 0.862103 0.402158 False 
5 -0.256027 -0.819937 False 
6 -1.728418 1.463709 True 
7 -1.110928 -2.173016 False 
8 0.656576 -1.218179 False 
9 0.014519 -0.854039 False 

所以,繼續並嘗試不通過分配到一系列的中間步驟去:

df['C'] = False # not necessary but a reset 
if df.A < df.B: df.C = True 

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 

所以這裏的問題是爲什麼當我第一次瀏覽這個系列時,它會工作,但不能直接分配給列?我懷疑有更多這種行爲我不明白。

讓我繼續一個相關的例子。據我所知,np.where()可能是比if語句更容易執行的操作,但我仍然遇到一個指向缺乏理解的問題。下面是我想應該是等價的三條線:

df['C'] = np.where((df.A < 0 & df.B > df.A), True, False) #1 Errors 
df['C'] = np.where((df.A < 0) and (df.B > df.A), True, False) #2 Errors 
df['C'] = np.where((df.A < 0) & (df.B > df.A), True, False) #3 Works 

之間#2,#3的區別在於和VS & ......我懷疑有東西逐位回事幕後我不完全瞭解。但爲什麼Ex 1錯誤?額外的括號不需要,對嗎? 條件1 &條件2 (條件1)&(條件2)

爲什麼那些產生不同的結果?更重要的是,這是記錄在哪裏?只是想擴大自己的學習和理解,並學習如何自己處理這類問題。

謝謝!

回答

2

要回答你的問題,

  • 爲什麼如果df.A < df.B:df.C =真不成?

答:我認爲這是由「if」造成的。 您可以將「if」看作一個函數,它只接受True/False或已知的True/False評估(即如果爲1,如果爲-1,如果爲0,如果爲None)。你可以參考https://docs.python.org/2/library/stdtypes.html。基本上python中的所有東西都是擴展對象類。如果該課程實施了非零len,「if」應該起作用。你可以嘗試以下方法:

>>> x = 1 
>>> x.__nonzero__() 
True 
>>> x = df.C 
>>> x.__nonzero__() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/Library/Python/2.7/site-packages/pandas/core/generic.py", line 731, in __nonzero__ 
    .format(self.__class__.__name__)) 
ValueError: The truth value of a Series is ambiguous. Use a.empty,  a.bool(), a.item(), a.any() or a.all(). 

如果你還是想知道有更多的細節,我會向您推薦大熊貓源代碼https://github.com/pydata/pandas/blob/master/pandas/core/generic.py

  • 爲什麼並不起作用與&比較?

我認爲這是回答Difference between 'and' (boolean) vs. '&' (bitwise) in python. Why difference in behavior with lists vs numpy arrays?

  • 爲什麼我們需要有支架?

如果您檢查錯誤消息,

>>> df.B>df.A & df.A <0 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/Library/Python/2.7/site-packages/pandas/core/ops.py", line 786, in wrapper 
    return filler(self._constructor(na_op(self.values, other.values), 
    File "/Library/Python/2.7/site-packages/pandas/core/ops.py", line 758, in na_op 
    result = lib.vec_binop(x, y, op) 
    File "pandas/lib.pyx", line 914, in pandas.lib.vec_binop (pandas/lib.c:16248) 
    File "pandas/lib.pyx", line 907, in pandas.lib.vec_binop (pandas/lib.c:16122) 
TypeError: unsupported operand type(s) for &: 'float' and 'bool' 

這意味着實際上&運營商正試圖浮子和布爾之間使用。浮子在哪裏,布爾在哪裏? bool是df.B> df.A,浮點數是df.A.這意味着什麼?這意味着&沒有超過(<,>),檢查該https://www.ibiblio.org/swaroopch/byteofpython/read/operator-precedence.html

在另一方面運算符優先級「和」有運營商優先(<,>),從而和工作。

要深入挖掘它,我相信檢查源代碼將是一個好方法。 希望它回答你的問題。

+0

好的,謝謝。這很有幫助。我想我假定C = A user5747140