Getting rows from a pandas dataframe with categoric and numerical variables where numeric columns are all 0

Viewed 27

I have a pandas dataframe that shows a mixture of categorical contextual data for users, along with a bunch of numeric data (roughly 20 numerical columns in total) that is aggregated to give one row for one user. An example with reduced numerical columns:

  userId   country   status   numericColumn1   numericColumn2   numericColumn3  
 -------- --------- -------- ---------------- ---------------- ---------------- 
     322   UK        free                  0                0              1.4  
     229   US        free                  0                0                0  
     346   HK        paying               45                6             27.7  
     935   US        free                  0                0                0  

What I would like to do is only get the rows where all numeric columns have values of 0s. i.e.

  userId   country   status   numericColumn1   numericColumn2   numericColumn3  
 -------- --------- -------- ---------------- ---------------- ---------------- 
     229   US        free                  0                0                0  
     935   US        free                  0                0                0  

The only way I could think of is by doing a pd.query where each of the numeric columns are 0: df.query('numericColumn1 == 0 & numericColumn2 == & numericColumn3 ==0) but this wouldn't work for when you have the multiple numerical columns(20) like I have.

Is there a better way? or can I only do this by chaining multiple query conditions together.

1 Answers

Try this:

cols = list(df.select_dtypes(include=['number']).columns.drop(['userId'])) #generates a list containing columns having numeric datatypes except userId
new_df = df[df[cols].eq(0).all(1)] #selects rows with 0 values in the column list
Related