'2016-12-30' & Date<'2017-05-01'") sample_to_work_on.head() dataframe islike this: Plaza ID Date Ho" /> '2016-12-30' & Date<'2017-05-01'") sample_to_work_on.head() dataframe islike this: Plaza ID Date Ho" /> '2016-12-30' & Date<'2017-05-01'") sample_to_work_on.head() dataframe islike this: Plaza ID Date Ho"/>

Why i'm having this argument type 'int' is not iterable error on dataframe querying?

256 Views Asked by At
sample_to_work_on = df.query("'Plaza ID'==5 & Date>'2016-12-30' & Date<'2017-05-01'")

sample_to_work_on.head()
 


dataframe islike this:

Plaza ID    Date    Hour    Direction   # Vehicles - E-ZPass    # Vehicles - VToll
0   21  2022-08-06  0   I   2820    649
1   21  2022-08-06  1   I   2124    474
2   21  2022-08-06  2   I   1617    391
3   21  2022-08-06  3   I   1228    358
4   21  2022-08-06  4   I   1604    368

**when i try this statement on query() function on my data it gives me a type error : **

264 try:
--> 265     return x.isin(y)
    266 except AttributeError:

AttributeError: 'list' object has no attribute 'isin'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[10], line 1
----> 1 sample_to_work_on = df.query("'Plaza ID'==5 & Date>'2016-12-30' & Date<'2017-05-01'")
      3 sample_to_work_on.head()

File c:\Users\eren\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
    325 if len(args) > num_allow_args:
    326     warnings.warn(
    327         msg.format(arguments=_format_argument_list(allow_args)),
    328         FutureWarning,
    329         stacklevel=find_stack_level(),
    330     )
--> 331 return func(*args, **kwargs)
...
    270     except AttributeError:
    271         pass
--> 272 return x in y

TypeError: argument of type 'int' is not iterable

why is this happening and any idea to prevent this ? my goal is to retrieve data which has plaza id of 5 and that spesific dates in the query

i tried to rewrite statement for 5 times and it gave me the same error there was no problem with date retrieving but plaza id makes it a problem

1

There are 1 best solutions below

1
Timeless On

Since the column name Plaza ID contains a whitespace, I suggest you to use the backtick (`).

From @World Wide DBA answer on the utility of backsticks in SQL :

They are called quoted identifiers and they tell the parser to handle the text between them as a literal string. They are useful for when you have a column or table that contains a keyword or space. For instance the following would not work:

df.query("`Plaza ID`==5 and Date>'2016-12-30' and Date<'2017-05-01'")

Alternatively, use loc :

df.loc[(df["Plaza ID"].eq(5)) & (df["Date"].between("2016-12-30", "2017-05-01"))]