0

How to do a pandas query when the datatype is object? I tried:

df.query('Train.Test=="Test"')

And also

df.query('Train.Test==Test')

without success.

SampleID,Age,Ctrl.Case,Train.Test,Subject.ID
2241944,22,Ctrl,Train,1
2119485,22,Ctrl,Test,2
2312486,21,Ctrl,Test,5
2813487,19,Ctrl,Test,6
2812488,21,Ctrl,Test,7

Should I convert it to string first? or can I still query with Train.Test as object type?

Caterina
  • 775
  • 9
  • 26

1 Answers1

3

Any column name that is not a valid Python variable name needs to be quoted with a pair of back ticks:

df.query("`Train.Test` == 'Test'")
Code Different
  • 90,614
  • 16
  • 144
  • 163