Home > database >  filter pandas dataframe by several column values
filter pandas dataframe by several column values

Time:12-15

I would like to filter a df by several columns, is it possible to do it in one line?

so far I did it over many lines:

mini_df = df[df['col1']==0] mini_df = mini_df[mini_df['col2']==1]

but if I do mini_df = df[df['col1']==0 and df['col2']==1] it does not work. since I want to have many such filters would be good to be able to do them in one line.

CodePudding user response:

you were close, try to put every filter in brackets () and "&" instead of "and":

mini_df = df[(df['col1']==0) & (df['col2']==1)]
  • Related