0

I am not using any float object in my code ,yet it is showing 'float' object not iterable as warning. The dataset is Titanic Dataset from Kaggle.

I have tried changing the variable name in case I might have used it somewhere else but to no avail. (ie,from x to the current variable y). I have imported the numpy library too. Also I tried using the snippet of code out of the while loop while giving some value of i ( say 1) and it worked perfectly fine.

i= 0
while i <= 890:
    if (dataset.loc[i , 'Cabin'] != np.NaN):
    y = list(dataset.loc[i , 'Cabin'])
    dataset.loc[i , 'Cabin'] = y[0]
i= i+1

The cabin column of the dataset has alphanumeric values like 'C123'. I just wanted the 'C' or the first letter of the alphanumeric value.Which is why I changed it into a list and took the first array element. But I get..

TypeError                                 Traceback (most recent call
 last)
<ipython-input-70-3eeda9c4954a> in <module>
  2 while i <= 890:
  3     if (dataset.loc[i , 'Cabin'] != np.NaN):
----> 4         y = list(dataset.loc[i , 'Cabin'])
  5         dataset.loc[i , 'Cabin'] = y[0]
  6     i= i+1

TypeError: 'float' object is not iterable

2 Answers 2

1

The problem is dataset.loc[i , 'Cabin'] returns a float, and you're calling list on it. list callable requires its argument to be an iterable, but it's getting a float, hence the error.

The obvious fix is to wrap the float in a list literal:

y = [dataset.loc[i , 'Cabin']]

But why bother with converting it into a list as you're getting the first indexed value in the next line, so you could just set it as y = dataset.loc[i , 'Cabin'].

N.B: There could be other better solutions to your real issue if I know your use case.

Sign up to request clarification or add additional context in comments.

Comments

0

the issue is that dataset.loc[i , 'Cabin']is actually a float object which cannot be turned into a list like you want... check out this example:

>>> a = 1.0
>>> b = list(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'float' object is not iterable

this is essentially what you're doing

try wrapping dataset.loc[i , 'Cabin'] with [] like below:

>>> b = [a]
>>> b
[1.0]

2 Comments

I need a list like ['C', '1', '2', '3'] not like ['C123'].And why does the list function work outside of the while loop using the same code..
it all depends on what dataset.loc[i,'cabin'] returns. because you are iterating i it's possible that it sometimes return an iterable and other times it just returns a float of some kind. i would start by checking what datatypes the return values are, is it throwing the error on the first loop? the last? in the middle? we really need more to go on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.