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

Time:08-13

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]                    -

CodePudding user response:

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
  • Related