How do I remove the case sensitivity while doing query

I have below Dataframe as df:

Subject_Name Marks
Science 40
Maths 45
English 38

Now when i do query as below:
d2=df.query(‘Subject_Name == Science’)[‘Marks’]
print(d2)
Output: 40

But if i do query as below:
d2=df.query(‘Subject_Name == science’)[‘Marks’]
print(d2)
Output: Series(, Name: Marks, dtype: object)

so How do I query with case sensitivity? Because I am working on large data and not sure weather name will in which case and want match exact string.
Checked with isin, in also. Contains can be used but if two rows contains similar string then it becomes difficult to program which is exact match(for e.g , toast, toaster).

Why search with a string?

Assuming columns are 0 – 1 and rows are 0 – 3: if you want to return the marks for ‘science’, you simply reference column 0 row 1 for the name and column 1 row 1 for the marks.

1 Like

By Roja Pathan via Discussions on Python.org at 12May2022 04:54:

I have below Dataframe as df:

Subject_Name Marks
Science 40
Maths 45
English 38

Now when i do query as below:
d2=df.query(‘Subject_Name == Science’)[‘Marks’]
print(d2)
Output: 40

But if i do query as below:
d2=df.query(‘Subject_Name == science’)[‘Marks’]
print(d2)
Output: Series(, Name: Marks, dtype: object)

so How do I query with case sensitivity? Because I am working on large data and not sure weather name will in which case and want match exact string.

You might consider that if you aren’t sure about the names, you
shouldn’t be trying to do work with them. Not always the case though.

I would be inclined to normalise the names in your DataFrame. There’s
a .rename() method which can be used to rename your columns, eg:

df.rename(columns={'Science': 'science', 'Maths': 'maths'}, inplace=true)

Then having normalised your column names you can query:

d2=df.query('Subject_Name == science')['Marks']

So rather than trying to write some flexible (and possibly less
efficient) query, you can make your data all the same, and keep using a
simple query. Simple things are easier to debug and reuse.

Cheers,
Cameron Simpson cs@cskk.id.au