2017-07-25 42 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),如果你只知道这些列是布尔而已。

相关问题