2017-07-25 106 views
0

我正在使用財務DataFrame進行此工作。我想創建一個df ['LB4']列,如果所有LB1,LB2和LB3都爲真,則返回true。Python DataFrames從其他3個「True/False」列創建'Create/False'列

Date  Open High Low  Close Volume  LB1  LB2  LB3        
2005-01-03 4.63 4.65 4.47 4.52 173354034 False False False 
2005-01-04 4.56 4.68 4.50 4.57 274515332 False False False 
2005-01-05 4.60 4.66 4.58 4.61 170210264 False False True 
2005-01-06 4.62 4.64 4.52 4.61 176469496 False True True 
2005-01-07 4.64 4.97 4.62 4.95 558932752 True True False 

任何想法?

我是Python新手,很感激任何幫助。

謝謝

回答

1

與此開始(修改你的例子有點):

In [1095]: df 
Out[1095]: 
    LB1 LB2 LB3 
0 False False False 
1 False False False 
2 False False True 
3 True True True 
4 True True True 

你可以使用按位&

In [1096]: df.LB1 & df.LB2 & df.LB3 
Out[1096]: 
0 False 
1 False 
2 False 
3  True 
4  True 
dtype: bool 

或者, df.all

In [1097]: df[['LB%d' %i for i in range(1, 4)]].all(axis=1) 
Out[1097]: 
0 False 
1 False 
2 False 
3  True 
4  True 
dtype: bool 

可以縮短列表解析df.select_dtypes([bool]).all(axis=1),如果你只知道這些列是布爾而已。

相關問題