亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將文本處理步驟應用于熊貓數據框

將文本處理步驟應用于熊貓數據框

慕斯709654 2021-11-09 19:30:35
我有文本字符串,我正在使用以下字符串函數來清理它?,F在我想縮放它并將其應用于數據幀。我面臨的挑戰是它不適用于數據框。我嘗試申請 numpy 數組,但結果是空字符串。數據框是單列,具有與給定的行變量相似的字符串:                               00   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 ...`` 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()結果:['Mozilla'、'兼容'、'MSIE'、'Windows'、'NT'、'NET'、'CLR'、'handyCafeCln']我嘗試在函數中打包相同的步驟,但無法將其應用于 datframe 并出現以下錯誤 Series' object has no attribute 'translatedef 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)
查看完整描述

1 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

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))


查看完整回答
反對 回復 2021-11-09
  • 1 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號