Search code examples
pythonpandasdata-cleaning

Search whole dataframe for empty cells except specific columns - how to write condensed code to do this?


I'm writing code that data cleans my original dataframe and then spits out a dataframe of all the rows with errors. This line of code currently finds empty cells in the column 'RaceId'. I have 40 other columns and I would like to find empty cells in all of them apart from 'InRun' and 'Flucs'. How do I create a line of code that does this so I don't have to write out 40 lines of code?

My code:

df2[(df2['RaceId'] == '']

Solution

  • If you want to remove rows that have empty string in all the specified columns, pass all to agg

    df[your_col_list].apply(lambda x: x == '').agg(all, axis=1)
    

    Otherwise, pass any to agg.