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

How to Filter Excel Data Before Loading to Pandas

I want to load only users who their status are "Disabled" in Status column into Pandas data frame. My initial code is like this

import pandas as pd

df = pd.read_excel('Users.XLSX', sheet_name='WebUsers',  usecols="A,B")
print(df)

Which is bringing all users no matter they are "Disabled" or "Active". Can you please let me know how to add a filter like WHERE 'Status' == 'Disabled' and load only disabled users into frame?

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

>Solution :

You can’t do that with Pandas, you have to filter after Pandas loads the file:

df = (pd.read_excel('Users.XLSX', sheet_name='WebUsers', usecols=['A', 'B', 'Status'])
        .query("Status == 'disabled'").drop(columns='Status'))

Note: parquet is a column-oriented data file format so you can filter your dataframe before.

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