finance - Python DataFrames Create 'Create/False' Column from 3 other 'True/False' Columns -
i'm working financial dataframe. want create df['lb4'] column, returns true if of lb1, lb2, , lb3 true.
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
any ideas?
i new python , appreciate help.
thank
starting (modified example bit):
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
you can use bitwise &
:
in [1096]: df.lb1 & df.lb2 & df.lb3 out[1096]: 0 false 1 false 2 false 3 true 4 true dtype: bool
alternatively, df.all
:
in [1097]: df[['lb%d' %i in range(1, 4)]].all(axis=1) out[1097]: 0 false 1 false 2 false 3 true 4 true dtype: bool
you can shorten list comprehension df.select_dtypes([bool]).all(axis=1)
if know columns boolean , nothing else.
Comments
Post a Comment