NoName
NoName

Reputation: 10334

Pandas: inconsistent iteration problems

import pandas as pd
d = {"col1":[1,2], "col2":[3,4]}
df = pd.DataFrame(data = d)

print(type(df.col1))
print(type(df["col1"]))

for index, col1 in df.col1.items():
    pass

for index, col1 in df["col1"]:
    pass

This outputs:

<class 'pandas.core.series.Series'>
<class 'pandas.core.series.Series'>
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-a346dc42f5cd> in <module>
      9     pass
     10 
---> 11 for index, col1 in df["col1"]:
     12     pass

TypeError: cannot unpack non-iterable int object

But why can only 1 be iterated? Both object types are the same as indicated by the print-outs.

Upvotes: 0

Views: 66

Answers (2)

Shoonya
Shoonya

Reputation: 128

The items() method returns iterable of tuples containing the (index, value) pairs from the Series https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.items.html

Series inherits its iter() from IndexOpsMixin, which returns an iterator over the values of the series. ( https://github.com/pandas-dev/pandas/blob/master/pandas/core/base.py ). Hence, it does not yield a tuple of the index and value, but only the value.

Upvotes: 0

oppressionslayer
oppressionslayer

Reputation: 7224

They have different data underneath, here's a look at the items list ( the .items includes the rows):

In [177]: list(df.col1.items())                                                                                                                                                                
Out[177]: [(0, 1), (1, 2)]

In [178]: list(df["col1"])                                                                                                                                                                     
Out[178]: [1, 2]

Upvotes: 4

Related Questions