我基本上想要单词列表中每个列表中的单词数量。这是我的代码
for ind,word in enumerate(brand6['words']):
brand6['len']=len(brand6[ind][word])
但是,我遇到这样的错误:
Traceback (most recent call last):
File "C:\Users\darshini.nagaraj\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1618, in
pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1626, in
pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\darshini.nagaraj\Desktop\example.py", line 28, in <module>
print((brand6[ind][word]))
File "C:\Users\darshini.nagaraj\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\darshini.nagaraj\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1618, in
pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1626, in
pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
我已经为此工作了3天。任何帮助都感激不尽!! 预先感谢!
答案 0 :(得分:0)
尝试
word_count=[]
for i in range(brand6.shape[0]):
word_count.append(len(brand6['words'][i]))
brand6['word_count']=word_count
最后一行将在您的数据集中创建一个新列作为'word_count'。