python - Comparing values in columns and returning boolean values -
hi have 4 columns column names(ma5,ma10,ma20,ma60). see if in each row, satisfies: ma60>ma20>ma10>ma5(numbers in these columns in specific row), return 1 if true , 0 if false. tried following:
(eachstockdf['ma5']>eachstockdf['ma10'] ,  eachstockdf['ma10']>eachstockdf['ma20'] ,  eachstockdf['ma20']>eachstockdf['ma60']) *1 i want return series 0 , 1 indication of whether condition met. gives following error:
valueerror: truth value of series ambiguous. use a.empty, a.bool(),  a.item(), a.any() or a.all(). i know works when compare 2 rows. example:
((eachstockdf['ma5']>eachstockdf['ma10'])) *1 but how can compare 4 columns? in advance!!
you need use bitwise & operators, pandas overloads those. 
also, each condition must wrapped in set of enclosing parens, because of precedence of bitwise operators.
( (eachstockdf['ma5']  > eachstockdf['ma10']) &    (eachstockdf['ma10'] > eachstockdf['ma20']) &    (eachstockdf['ma20'] > eachstockdf['ma60'])  ) * 1 as note, convert bool int 
(....).astype(int) 
Comments
Post a Comment