Код: Выделить всё
df['Text'].fillna('').apply(str)
df['Text'].str.replace(r"[^a-zA-Z]", " ", regex=True)
df.dropna()
< /code>
Тогда я определил < /p>
def preprocess_text(text):
text = re.sub(r'\d+', '', text) # Remove numbers
text = text.lower() # Convert to lowercase
text = text.translate(str.maketrans('', '', string.punctuation)) # Remove punctuation
words = word_tokenize(text) # Tokenize text
words = [word for word in words if word not in stopwords.words('english')] # Remove stopwords
return words # Return list of words
< /code>
Но когда я называю это на своем раме, я получаю < /p>
df['cleaned_text'] = df['Text'].apply(preprocess_text)
AttributeError: 'float' object has no attribute 'lower'
< /code>
Я вернулся и изменил функцию < /p>
def preprocess_text(text):
try:
text = re.sub(r'\d+', '', text) # Remove numbers
except TypeError:
print(text)
except AttributeError:
print(text)
text = text.lower() # Convert to lowercase
text = text.translate(str.maketrans('', '', string.punctuation)) # Remove punctuation
words = word_tokenize(text) # Tokenize text
words = [word for word in words if word not in stopwords.words('english')] # Remove stopwords
return words # Return list of words
Любые указатели о том, как изолировать, где в этой массе Текст ошибка возникает? Или еще лучше, предварительный шаг, который я могу устранить это?
Подробнее здесь: https://stackoverflow.com/questions/794 ... ith-python