Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

filter pandas dataframe by several column values

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

mini_df = df[(df['col1']==0) & (df['col2']==1)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading