name 'yr' is not defined error when querying on Pandas

Asked 2 years ago, Updated 2 years ago, 46 views

I want to loop around yr (year) like the code below to extract some data from the query and process it, but the error name 'yr' is not defined does not execute it.

When I use variables in a query, can't I use the following description?
I would appreciate it if you could tell me the correct notation.

for yrin range (2004, 2010):
    TF=TS.query('m>(yr+1)*12+0 and m<(yr+1)*12+10')[['CID', 'm']]
  …
[TS] (test data)
ID m
A201001
C200510
E200601

python pandas

2022-09-30 18:33

1 Answers

I think it was solved in the comments section, but for future viewers' reference...

To see the local variable in the query syntax of the Pandas data frame, use the
You must use @variable name instead of variable name.

For example, this question can be referred to by @yr.

For more information, see
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.query.html
See the .


2022-09-30 18:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.