Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading