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

Check if multiple rows are filled and if not blank all values

I have the following dataframe:

ID  Name    Date    Code    Manager
1   Paulo   %   10  Jonh's Peter
2   Pedro   2001-01-20  20  
3   James       30  
4   Sofia   2001-01-20  40  -

I need a way to check if multiple columns (in this case Date, Code and Manager) are filled with any value. In the case there is any blank in any of those 3 columns, blank all the values in all 3 columns, returning this data:

ID  Name    Date    Code    Manager
1   Paulo   %   10  Jonh's Peter
2   Pedro           
3   James           
4   Sofia   2001-01-20  40  -

What is the best solution for this case?

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

>Solution :

You can use pandas.isnull() with pandas.any(axis=1) then use pandas.loc for setting what value that you want.

col_chk = ['Date', 'Code', 'Manager']
m = df[col_chk].isnull().any(axis=1)
df.loc[m , col_chk] = '' # or pd.NA
print(df)

   ID   Name        Date  Code       Manager
0   1  Paulo           %  10.0  Jonh's Peter
1   2  Pedro                                
2   3  James                                
3   4  Sofia  2001-01-20  40.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