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

Pandas Query Fails

Given the following CSV:

ID,Title,Created By,Created On,Estimate,Forecast,Priority,References,Section,Section Depth,Section Description,Suite,Suite ID,Type,Updated By,Updated On
C28045,test1,dsa,10/27/2022 1:56 AM,,,Highest,,primary,0,,Master,S378,Automated,test,10/27/2022 4:33 AM
C28060,test32,dsa,10/27/2022 4:34 AM,,,Medium,,primary,0,,Master,S378,Automated,test,10/27/2022 4:34 AM
C28062,test5,dsa,10/27/2022 4:34 AM,,,Medium,,primary,0,,Master,S378,Automated,test,10/27/2022 4:34 AM
C28059,test6,dsa,10/27/2022 4:06 AM,,,Medium,,sec,0,,Master,S378,Automated,test,10/27/2022 4:33 AM
C28061,test643,dsa,10/27/2022 4:34 AM,,,Medium,,sec,0,,Master,S378,Automated,test,10/27/2022 4:34 AM
C28063,tes4352,dsa,10/27/2022 4:34 AM,,,Medium,,sec,0,,Master,S378,Automated,test,10/27/2022 4:34 AM

Using pandas I want to return the ID, for a given "Section" and "Title"

data = pd.read_csv(csvPath)
data.query('Section = primary and Title = test32', inplace=True)
print(data)

Raises:

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

  File "<unknown>", line 1
    Section =primary and Title =test32 
    ^^^^^^^^^^^^^^^^
SyntaxError: Python keyword not valid identifier in numexpr query

>Solution :

It looks like you are missing a few quotes and should be using ‘==’

# using query, filter for Section == 'primary' and Title == 'test32'
data.query("Section == 'primary' and Title == 'test32'", inplace=True)
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