Home > Mobile >  Check if multiple rows are filled and if not blank all values
Check if multiple rows are filled and if not blank all values

Time:08-28

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?

CodePudding user response:

You can use enter image description here

You can use this code to get the expected output :

df.loc[df[["Date", "Code", "Manager"]].isna().any(axis=1), ["Date", "Code", "Manager"]] = ''

>>> print(df)

enter image description here

  • Related