首页 > 解决方案 > TypeError:“float”对象在 Pandas Dataframe Python 中不可迭代

问题描述

我要做的是在名为“名词”的数据框中创建一个新列,并保留我已从中删除停用词的列中的所有名词。

问题是在我正在使用的药物数据集中,药物评论列中的某处必须有浮动对象,因为它不会让我对其进行迭代。我读到我可以使用 range() 但我不确定这将如何转化为我的循环,因为我不确定范围而且我读到 .dropna 可以工作但据我所知没有空值在任何行中。

我的代码:

df['nouns'] = df.apply(lambda row: nltk.word_tokenize(row['noStopwordsReview']), axis=1) 
noun=[]
for  index, row in df.iterrows():
    noun.append([word for word,pos in pos_tag(row[0]) if pos == 'NN'])
df['nouns'] = noun 

完整的追溯:

回溯(最近一次通话最后):

File "/Users/matthewrook/OneDrive - AUT University/Year 3/Data Mining/assignment1.py", line 76, in <module>
    noun.append([word for word,pos in pos_tag(row[0]) if pos == 'NN'])

  File "/opt/anaconda3/lib/python3.8/site-packages/nltk/tag/__init__.py", line 161, in pos_tag
    return _pos_tag(tokens, tagset, tagger, lang)

  File "/opt/anaconda3/lib/python3.8/site-packages/nltk/tag/__init__.py", line 118, in _pos_tag
    tagged_tokens = tagger.tag(tokens)

  File "/opt/anaconda3/lib/python3.8/site-packages/nltk/tag/perceptron.py", line 181, in tag
    context = self.START + [self.normalize(w) for w in tokens] + self.END

TypeError: 'float' object is not iterable

标签: pythonpandasdataframe

解决方案


推荐阅读