AttributeError:“float”对象没有“translate”Python属性

2024-06-01 01:04:02 发布

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

我正在做一些NLP与文本数据从医生只是试图做一些基本的预处理文本清理试图删除停止词和标点符号。我已经给了程序一个标点符号和停止词的列表。你知道吗

我的文本数据如下所示:

"Cyclin-dependent kinases (CDKs) regulate a variety of fundamental cellular processes. CDK10 stands out as one of the last orphan CDKs for which no activating cyclin has been identified and no kinase activity revealed. Previous work has shown that CDK10 silencing increases ETS2 (v-ets erythroblastosis virus E26 oncogene homolog 2)-driven activation of the MAPK pathway, which confers tamoxifen resistance to breast cancer cells"

然后我的代码看起来像:

import string

# Create a function to remove punctuations
def remove_punctuation(sentence: str) -> str:
    return sentence.translate(str.maketrans('', '', string.punctuation))

# Create a function to remove stop words
def remove_stop_words(x):
    x = ' '.join([i for i in x.split(' ') if i not in stop])
return x

# Create a function to lowercase the words
def to_lower(x):
    return x.lower()

然后我尝试将函数应用到文本列

train['Text'] = train['Text'].apply(remove_punctuation)
train['Text'] = train['Text'].apply(remove_stop_words)
train['Text'] = train['Text'].apply(lower)

我收到一条错误信息,如:

--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in ----> 1 train['Text'] = train['Text'].apply(remove_punctuation) 2 train['Text'] = train['Text'].apply(remove_stop_words) 3 train['Text'] = train['Text'].apply(lower)

/opt/conda/lib/python3.6/site-packages/pandas/core文件/系列.py在里面 应用(self、func、convert\u dtype、args、**kwds)3192
否则:3193个值=自身类型(对象).值 ->;3194映射=lib.map\u推断(values,f,convert=convert\u dtype)3195 3196如果len(映射)和 isinstance(映射[0],系列):

熊猫/u库/src/推理.pyx在熊猫身上_libs.lib.map\u推断()

删除标点(句子) 3#创建删除标点符号的函数 4.删除标点符号(句子:str)->;str: ---->;5返回句子.翻译(str.maketrans公司('', '', 字符串.标点符号)) 6 7#创建一个函数来删除停止词

AttributeError:“float”对象没有“translate”属性

为什么我会犯这个错误。我猜是因为文本中出现了数字?你知道吗


Tags: ofthetotext文本createtrainlower