Python 将文本处理步骤应用于数据帧

Python 将文本处理步骤应用于数据帧,python,pandas,Python,Pandas,我有文本字符串,我正在使用以下字符串函数来清理它。现在我想缩放它并将其应用于数据帧。我面临的挑战是它不适用于dataframe。我尝试在numpy数组上应用,但结果是空字符串 数据框是具有类似字符串的单列,如行变量所示: 0 0 Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US... 1 Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/2..

我有文本字符串,我正在使用以下字符串函数来清理它。现在我想缩放它并将其应用于数据帧。我面临的挑战是它不适用于dataframe。我尝试在numpy数组上应用,但结果是空字符串

数据框是具有类似字符串的单列,如行变量所示:

                               0
0   Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US...
1   Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/2...
2   Mozilla/5.0 (iPod; U; CPU iPhone OS 4_1 like M...
3   Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/201...
4   Mozilla/4.0 (compatible; MSIE 7.0; Windows NT ...
``

结果:

[‘Mozilla’, "兼容",, “MSIE”, “窗口”, ‘新界’, “净”, “CLR”, “handyCafeCln”]

我尝试在函数中打包相同的步骤,但无法将其应用于datframe,错误序列为“object has no attribute”


我们可以看到一些示例输入v/s预期输出作为数据帧吗数据框是包含“line”变量中给定字符串的单列。更新
 line = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; handyCafeCln/3.3.21)"
 re_print = re.compile('[^%s]' % re.escape(string.printable))
 remove_digits = str.maketrans('', '', digits)
 remove_punc =str.maketrans('', '', string.punctuation)
 line = line.translate(remove_digits)
 line = line.translate(remove_punc)
 line = line.split()
def clean_pairs(lines):
    re_print = re.compile('[^%s]' % re.escape(string.printable))
    remove_digits = str.maketrans('', '', digits)
    remove_punc =str.maketrans('', '', string.punctuation)

    lines.translate(remove_digits)
    lines.translate(remove_punc)
    lines.split()

df.apply(clean_pairs)
def clean_pairs(lines):
    re_print = re.compile('[^%s]' % re.escape(string.printable))
    remove_digits = str.maketrans('', '', string.digits)
    remove_punc =str.maketrans('', '', string.punctuation)
    lines = lines.translate(remove_digits)
    lines = lines.translate(remove_punc)
    lines = lines.split()
    return lines

df = pd.DataFrame([line])
print(df[0].apply(clean_pairs))