我使用python为两个txt文件插入列#34; amazon"和#34; yelp" 通过以下功能,它可以正常工作。 这是代码:
for i in range(len(amazon)+1):
a[i]='amazon'
amazon.insert(2,'label',a[i])
for i in range(len(yelp)+1):
a[i]='yelp'
yelp.insert(2,'label',a[i])
但是,当我使用用户定义函数来调用它时,会发生一些错误。我可以知道这个功能是否有任何问题,因为我使用了相同的逻辑
def label(doc):
for i in range(len(doc)):
a[i]=doc
return doc.insert(2,'label',a[i])
------------error msg:
ValueError Traceback (most recent call last)
<ipython-input-493-cc2a2cf79348> in <module>()
----> 1 label(imdb)
<ipython-input-492-d9cdd4df272b> in label(doc)
2 for i in range(len(doc)):
3 a[i]=doc
----> 4 return doc.insert(2,'label',a[i])
C:\Users\asus\Anaconda3\lib\site-packages\pandas\core\frame.py in insert(self, loc, column, value, allow_duplicates)
2421 value = self._sanitize_column(column, value, broadcast=False)
2422 self._data.insert(loc, column, value,
-> 2423 allow_duplicates=allow_duplicates)
2424
2425 def assign(self, **kwargs):
C:\Users\asus\Anaconda3\lib\site-packages\pandas\core\internals.py in insert(self, loc, item, value, allow_duplicates)
3817
3818 block = make_block(values=value, ndim=self.ndim,
-> 3819 placement=slice(loc, loc + 1))
3820
3821 for blkno, count in _fast_count_smallints(self._blknos[loc:]):
C:\Users\asus\Anaconda3\lib\site-packages\pandas\core\internals.py in make_block(values, placement, klass, ndim, dtype, fastpath)
2717 placement=placement, dtype=dtype)
2718
-> 2719 return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
2720
2721 # TODO: flexible with index=None and/or items=None
C:\Users\asus\Anaconda3\lib\site-packages\pandas\core\internals.py in __init__(self, values, ndim, fastpath, placement, **kwargs)
1842
1843 super(ObjectBlock, self).__init__(values, ndim=ndim, fastpath=fastpath,
-> 1844 placement=placement, **kwargs)
1845
1846 @property
C:\Users\asus\Anaconda3\lib\site-packages\pandas\core\internals.py in __init__(self, values, placement, ndim, fastpath)
113 raise ValueError('Wrong number of items passed %d, placement '
114 'implies %d' % (len(self.values),
--> 115 len(self.mgr_locs)))
116
117 @property
**ValueError: Wrong number of items passed 2, placement implies 1**