pandas - Logical operation on two columns of a dataframe -


in pandas, i'd create computed column that's boolean operation on 2 other columns.

in pandas, it's easy add 2 numerical columns. i'd similar logical operator and. here's first try:

in [1]: d = pandas.dataframe([{'foo':true, 'bar':true}, {'foo':true, 'bar':false}, {'foo':false, 'bar':false}])  in [2]: d out[2]:       bar    foo 0   true   true 1  false   true 2  false  false  in [3]: d.bar , d.foo   ## can't ... valueerror: truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all(). 

so guess logical operators don't work quite same way numeric operators in pandas. tried doing error message suggests , using bool():

in [258]: d.bar.bool() , d.foo.bool()  ## spoiler: doesn't work either ... valueerror: truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all(). 

i found way works casting boolean columns int, adding them , evaluating boolean.

in [4]: (d.bar.apply(int) + d.foo.apply(int)) > 0  ## logical or out[4]:  0     true 1     true 2    false dtype: bool  in [5]: (d.bar.apply(int) + d.foo.apply(int)) > 1  ## logical , out[5]:  0     true 1    false 2    false dtype: bool 

this convoluted. there better way?

yes there better way! use & element-wise logical , operator:

d.bar & d.foo  0     true 1    false 2    false dtype: bool 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -