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

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

使用 Python 加快查找和替換代碼的速度?

使用 Python 加快查找和替換代碼的速度?

楊__羊羊 2022-06-28 11:09:27
我有一個需要從 pandas 數據框列中刪除的 4,000 個字符串的列表。我下面的代碼適用于下面的示例,但是當我在我的 20k+ 行的 pandas 數據幀上使用它時,它需要很長時間。關于加快速度的任何想法?import pandas as pdimport redf = pd.DataFrame(    {        "ID": [1, 2, 3, 4, 5],        "name": [            "Hello Sam how is it going today? oh yeah",            "Hello Jane how is it going today? oh yeah",            "It is an Hello example how are you doing today?",            "how is it going today?n[soldjgf   ",            "how is it going today Hello World",        ],    })my_list = ['how is it going today?n[soldjgf', 'how are you doing today?']# =============================================================================# p = re.compile('|'.join(map(re.escape, my_list)))df['cleaned_text'] = [p.sub(' ', text) for text in df['name']] 
查看完整描述

1 回答

?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

使用 df.str.replace()

p = re.compile('|'.join(map(re.escape, my_list)))


df['cleaned_text'] = df['name'].str.replace(p, ' ')


查看完整回答
反對 回復 2022-06-28
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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