How to make conditions based on multiple columns of a row in pandas

Question:

I have the following dataframe:

       score firstyearrisk allfuture                                            Pharmas               Nutras
Gene                                                                                                        
ANK2     0.0             -         -                                     Risperidone[9]                    Risperidone[9]
CCL4     1.0           1.0       1.0                                     Risperidone[9]                    -

And I would like to remove the pharmas or nutras from every row where the score, firstyearris, allfuture add up to 0. There are dashes within these columns so those should be counted as 0, but not alter the table.

Something like this would be good:

       score firstyearrisk allfuture                                            Pharmas               Nutras
Gene                                                                                                        
ANK2     0.0             -         -                                                  -                    -
CCL4     1.0           1.0       1.0                                      Risperidone[9]                    -
Asked By: Michael Schmitz

||

Answers:

Before fill NA with -, you can try

cols1 = ['score', 'firstyearrisk', 'allfuture']
cols2 = ['Pharmas', 'Nutras']

df.loc[df[cols1].sum(axis=1).eq(0), cols2] = 0
Answered By: Ynjxsjmh
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.