简体   繁体   中英

TypeError: argument of type 'int' is not iterable

I was trying to create a unique identifier variable from the index for the cluster training data to merge with the cluster assignment variable

clus_train.reset_index(level=0, inplace=True)

It is throwing an error saying that

TypeError                                 Traceback (most recent call last)
<ipython-input-41-d0d098349dbc> in <module>()
  1 # create a unique identifier variable from the index for the
  2 # cluster training data to merge with the cluster assignment variable
----> 3 clus_train.reset_index(level=0, inplace=True)

C:\Users\3016205\AppData\Local\Continuum\Anaconda3\lib\site-
packages\pandas\core\frame.py in reset_index(self, level, drop, inplace, 
col_level, col_fill)
3053                 # to ndarray and maybe infer different dtype
3054                 level_values = _maybe_casted_values(lev, lab)
-> 3055                 if level is None or i in level:
3056                     new_obj.insert(0, name, level_values)
3057 

TypeError: argument of type 'int' is not iterable

Can you please help with this. I am using Python 3.x

You have hit a bug in Pandas; you are using reset_index() on a dataframe with a single-set index with a Pandas version somewhere between 0.19.2 and 0.20.1. This is issue #16263 :

between v0.19.2 and v0.20.1, the behavior of DataFrame.reset_index changed. With a single set index:

  • It does not attempt to keep the column (essentially making drop=True always on)
  • level=int no longer works (iterables work)

Upgrade Pandas to 0.20.2 or newer.

As the bug report notes, a temporary work-around would be to use level=[0] .

You're passing in level as a variable with value of 0 . Then you're using the in keyword to see if i is in the level variable. The in keyword basically means "look in this list/set/string/what-have-you and see if this other variable is contained within it". Python tries to look inside your level variable and finds that it's an int and doesn't know what to do, because you can't look inside an int . It's just its own thing, not a container for other things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM