Python:TypeError:“float”对象不是iterab

2024-10-01 17:32:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从Pandas dataframe中删除stopwords。这是我的代码:

import nltk
from nltk.corpus import stopwords
nltk.download('stopwords')

stop_words = stopwords.words('english')
print(stop_words)

data['description'] = data['description'].apply(lambda x: [item for item in x if item not in stop_words])

输出:

^{pr2}$

我认为错误来自这一部分:

[item for item in x if item not in stop_words]

但很明显,停止语是一个列表。那么呢?在

编辑1:

我对代码进行了以下更改:

data['description'] = data['description'].str.split()
print(data.description[679])
data['description'] = data['description'].apply(lambda x: [item for item in x if item not in stop_words])

split()可以完美地工作。这是数据描述[679]:

['ame', 'jalsa', 'event', 'presents', 'navratri', 'jhankaar', 'premium', 'navratri', 'and', 'lifestyle', 'exhibition', 'september', 'seema', 'hall', 'anand', 'nagar', 'road', 'near', 'sachin', 'tower', 'ahmedabad', 'visit', 'meet', 'over', 'designers', 'from', 'all', 'over', 'india', 'perfect', 'navratri', 'stuff', 'shopping', 'created', 'ame', 'jalsa', 'event', 'dont', 'miss', 'this', 'grand', 'exhibition', 'navratri', 'exhibition', 'premium', 'exhibition', 'lifestyle', 'fashion', 'accessories', 'jewellery', 'jalsa', 'jalsa', 'jalsa', 'exhibition', 'jalsaaholics', 'jalsa', 'ahmedabad', 'shopping']

但错误依然存在。在


Tags: 代码infordataifnotdescriptionitem
1条回答
网友
1楼 · 发布于 2024-10-01 17:32:59

我认为有丢失的值,所以可能的解决方案是通过^{}-在再次创建assign to column back之后删除它们:

data['description'] = (data['description'].dropna()
                         .apply(lambda x: [item for item in x if item not in stop_words]))

如果需要,请删除列description中缺少值NaNs的所有行:

^{pr2}$

或者,如果需要空列表来查找缺失值:

data = pd.DataFrame({
    'description': ['i love','a me you',None,'ahoj', np.nan],
    'B': list(range(5))
})

data['description'] = data['description'].str.split()
print (data)
    description  B
0     [i, love]  0
1  [a, me, you]  1
2          None  2
3        [ahoj]  3
4           NaN  4

stop_words = ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]

f = lambda x: [item for item in x if item not in stop_words] if isinstance(x, list) else []
data['description'] = data['description'].apply(f)

print (data)
  description  B
0      [love]  0
1          []  1
2          []  2
3      [ahoj]  3
4          []  4

相关问题 更多 >

    热门问题