0

I am new to Python. I got trouble in creating a pandas data frame.

dataDict = {}
dataDict['grant_id'] = grant_ids
dataDict['patent_title'] = patent_title
dataDict['kind'] = kinds

df=pd.DataFrame(dataDict)

The code above works in python2, but when I change to python3, I got error message:

TypeError Traceback (most recent call last)
<ipython-input-6-3a9900bc5bca> in <module>()
      9 #dataDict['abstract'] = abstractResult
     10 
---> 11 df=pd.DataFrame(dataDict)
     12 
     13 df.head()

3 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/internals/construction.py in extract_index(data)
    303             elif is_list_like(val) and getattr(val, 'ndim', 1) == 1:
    304                 have_raw_arrays = True
--> 305                 raw_lengths.append(len(val))
    306 
    307         if not indexes and not raw_lengths:

TypeError: object of type 'map' has no len()

grant_ids is a list of integers.

Is there a way to fix it?

shyamzzp
  • 115
  • 7
kevin
  • 309
  • 2
  • 12
  • 1
    `grant_ids` seems like a result of `map` operator. In python 2.x, `map` returns `list` which therefore works fine with `pandas.DataFrame`. In python 3.x, `map` returns an iterator. You need to explicitly construct a list such as `grant_ids = list(grant_ids)` – Chris Aug 13 '19 at 05:39

1 Answers1

1

The issue is with Python 2 and 3 map function returns differences. In Python 2, the map returns a list while in 3, it returns a generator. Generators have no length(as they yield result on evaluating, namely do not store all values on memory). You can turn generator to list with list(generator_object) or list comprehension.

dataDict = {}
dataDict['grant_id'] = [id_ for id_ in grant_ids]
dataDict['patent_title'] = [title for title in patent_title]
dataDict['kind'] = [kind for kind in kinds]

df=pd.DataFrame(dataDict)

This should work.

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57