python - Difficulty Creating column of Pandas Dataframe -


i wanted create new column of dataframe based on existing columns, want conditional on existing column in dataframe. following code not working. know why?

if cv['keyword'] == 0:     cv['left out'] = (cv['prediction numerator'] - (cv['rate'] *10000))/(cv['prediction denominator'] - 10000) else:     cv['left out'] = (cv['prediction numerator'] - (cv['rate'] *10000 * 10))/(cv['prediction denominator'] - (10000 * 10)) 

i'm getting following error:

traceback (most recent call last):   file "<stdin>", line 1, in <module>   file "c:\users\bwei\downloads\winpython-64bit-2.7.9.4\python-2.7.9.amd64\lib\site-packages\pandas\core\generic.py", line 709, in __nonzero__     .format(self.__class__.__name__)) valueerror: truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all(). 

here's snippet of first 4 columns of dataframe.

        zip  keyword  prediction numerator  prediction denominator   0     01001        0        7650546.693200            40002.558782    1     01001        0        7650546.693200            40002.558782    2     01001        0        7650546.693200            40002.558782    3     01001        0        7650546.693200            40002.558782    4     01002        0            157.951741                0.718621    5     01002        0            157.951741                0.718621    6     01005        0        3600150.148240            20000.671431    7     01005        0        3600150.148240            20000.671431    8     01007        0        6932235.816260            30000.936191    9     01007        0        6932235.816260            30000.936191    10    01007        0        6932235.816260            30000.936191    

thanks, ben

this should work:

cv.loc[cv['keyword']==0,'left out']=expression1 cv.loc[cv['keyword']!=0,'left out']=expression2 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -