Python 3-Zip是pandas数据框中的迭代器 [英] Python 3 - Zip is an iterator in a pandas dataframe
问题描述
我正在关注熊猫教程
这些教程是使用python 2.7编写的,而我正在python 3.4中进行编写
The tutorials are written using python 2.7 and I am doing them in python 3.4
这是我的版本详细信息.
Here is my version details.
In [11]: print('Python version ' + sys.version)
Python version 3.4.1 |Anaconda 2.0.1 (64-bit)| (default, Jun 11 2014, 17:27:11)
[MSC v.1600 64 bit (AMD64)]
In [12]: print('Pandas version ' + pd.__version__)
Pandas version 0.14.1
我按照教程创建了zip
I create the zip as per the tutorial
In [13]: names = ['Bob','Jessica','Mary','John','Mel']
In [14]: births = [968, 155, 77, 578, 973]
In [15]: zip?
Type: type
String form: <class 'zip'>
Namespace: Python builtin
Init definition: zip(self, *args, **kwargs)
Docstring:
zip(iter1 [,iter2 [...]]) --> zip object
Return a zip object whose .__next__() method returns a tuple where
the i-th element comes from the i-th iterable argument. The .__next__()
method continues until the shortest iterable in the argument sequence
is exhausted and then it raises StopIteration.
In [16]: BabyDataSet = zip(names,births)
但是创建后,第一个错误表明我看不到zip的内容.
But after creation the first error shows that I cannot see the contents of the zip.
In [17]: BabyDataSet
Out[17]: <zip at 0x4f28848>
In [18]: print(BabyDataSet)
<zip object at 0x0000000004F28848>
然后,当我创建数据框时,出现此迭代器错误.
Then when I go to create the dataframe I get this iterator error.
In [21]: df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-636a49c94b6e> in <module>()
----> 1 df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
c:\Users\Sayth\Anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self
, data, index, columns, dtype, copy)
255 copy=copy)
256 elif isinstance(data, collections.Iterator):
--> 257 raise TypeError("data argument can't be an iterator")
258 else:
259 try:
TypeError: data argument can't be an iterator
In [22]:
这是我需要以其他方式执行的python 3陷阱吗?还是其他?
Is this a python 3 gotcha where I need to do it differently? Or other?
推荐答案
您需要更改以下行:
BabyDataSet = zip(names,births)
收件人:
BabyDataSet = list(zip(names,births))
这是因为zip
现在在python 3中返回一个迭代器,因此出现了错误消息.有关更多详细信息,请参见: http://www.diveintopython3.net/porting -code-to-python-3-with-2to3.html#zip 和 https://docs.python.org/3/library/functions.html#zip
This is because zip
now returns an iterator in python 3, hence your error message. For more details see: http://www.diveintopython3.net/porting-code-to-python-3-with-2to3.html#zip and https://docs.python.org/3/library/functions.html#zip
这篇关于Python 3-Zip是pandas数据框中的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!