Home > Back-end >  Pandas Query Fails
Pandas Query Fails

Time:10-28

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:

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

CodePudding user response:

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)

CodePudding user response:

Does this solve your problem?

data = pd.read_csv(csvPath)

data=data[(data['Section']=='primary' & data['Title']=='test32')]

  • Related