I have a dataframe with 80 columns. Out of those, there are some 45 columns that I need to check for "all-zero" value filter. If for a row, all those 45 columns have zero as their value, then that row is marked as True. Otherwise as False.
Here's a sample dataframe for this problem:
df = pd.DataFrame({'col1': [101,102,103,104,105,106,107,108,109,110,111],
'col2': ['A','B','A','A','A','B','B','A','A','B','B'],
'col3': [12e10,23e10,34e10,0,56e10,67e10,78e10,89e10,0,12e10,23e10],
'col4': ['F','F','F','E','E','E','E','E','F','F','F'],
'col5': [12e10,0,34e10,45e10,0,67e10,78e10,0,0,12e10,23e10],
'col6': [12e10,0,34e10,0,0,67e10,78e10,0,0,12e10,23e10],
'col7': [12e10,0,34e10,45e10,0,67e10,0,0,0,12e10,23e10],
'col8': [12e10,0,34e10,45e10,0,67e10,78e10,0,0,12e10,23e10],
'col9': [12e10,0,34e10,45e10,0,67e10,78e10,0,0,12e10,23e10],
'col10': [12e10,0,0,45e10,0,67e10,78e10,0,0,12e10,23e10],
'col11': [12e10,0,34e10,45e10,0,67e10,78e10,0,0,12e10,23e10],
'col12': [12e10,0,34e10,0,0,67e10,78e10,0,0,12e10,23e10],
'col13': [12e10,0,34e10,45e10,0,67e10,78e10,0,0,12e10,23e10],
'col14': [12e10,0,34e10,45e10,0,67e10,0,0,0,12e10,23e10],
'col15': [12e10,0,34e10,45e10,0,67e10,78e10,0,0,12e10,23e10],
'col16': [12e10,0,34e10,45e10,0,67e10,78e10,0,0,12e10,0],
'col17': [12e10,0,34e10,45e10,0,67e10,78e10,0,0,0,0],
'col18': [12e10,0,34e10,45e10,0,67e10,78e10,0,0,12e10,23e10],
'col19': [12e10,0,34e10,0,0,67e10,78e10,0,0,12e10,23e10],
'col20': [12e10,0,0,45e10,0,67e10,78e10,0,0,12e10,23e10],
})
Now, in this example, I need to check the condition on all columns from col5 to col19. col3 and col20 are excluded. And all other categorical columns are excluded too.
I did this to filter out those rows.
df[(df[col5] == 0) & (df[col6] == 0) & (df[col7] == 0) & ... & (df[col19] == 0)]
This will get me rows (col1: 102,105,108,109), as they have all col5 to col19 as zero. Now I need to put True in a new column df['mark'] for these rows, and False for the rest of the rows.
But I believe there has to be some easy way to do this?