我正在尝试将 CSV 文件加载到 python 中并清理文本。但我不断收到错误消息。我将 CSV 文件保存在一个名为 data_file 的变量中,下面的函数会清理文本并返回干净的 data_file。
import pandas as pd
import numpy as np
import re
import matplotlib.pyplot as plt
df = pd.read_csv("/Users/yoshithKotla/Desktop/janTweet.csv")
data_file = df
print(data_file)
def cleanTxt(text):
text = re.sub(r'@[A-Za-z0-9]+ ', '', text) # removes @ mentions
text = re.sub(r'#[A-Za-z0-9]+', '', text)
text = re.sub(r'RT[\s]+', '', text)
text = re.sub(r'https?:\/\/\S+', '', text)
return text
df['data_file'] = df['data_file'].apply(cleanTxt)
df
我在这里遇到一个关键错误。