I'm working on a DataFrame query using 2 variables. The first variable is the column label and the second is a list of values. What I want to do is select all row where that column has a value contained in that list. The strange thing is that if I write the column label as a string there is no error, while referencing the variable containing the column label gives the following error:
Traceback (most recent call last):
File "C:\Python\Python36\lib\site-packages\pandas\indexes\base.py", line 2134, in get_loc
return self._engine.get_loc(key)
File "pandas\index.pyx", line 132, in pandas.index.IndexEngine.get_loc (pandas\index.c:4433)
File "pandas\index.pyx", line 151, in pandas.index.IndexEngine.get_loc (pandas\index.c:4238)
File "pandas\index.pyx", line 388, in pandas.index.Int64Engine._check_type (pandas\index.c:8171)
KeyError: False
This is the working code:
rhs_values_list = df1["RHS"].tolist()
query = "shoe_size in @rhs_values_list"
result_set = df2.query(query)
while this rises the above error:
rhs_values_list = df1["RHS"].tolist()
col = "shoe_size"
query = "@col in @rhs_values_list"
result_set = df2.query(query)
Is there something wrong with second version of the query?