簡體   English   中英

從數據框中刪除單詞列表

[英]removing a list of words from a dataframe

我有一個由包含字符串的數據系列組成的數據框。 我有一個希望從每一行中刪除的字符串列表。

tcl_list = ["tab", "cr", "lf", "doublequote", "singlequote", "eof"]
df[['Summary', 'Description']] = re.sub("|".join(tcl_list), ' ', df[['Summary', 'Description']])

例如:

由此:

the tab dog is acting sneaky like a doublequote cat doublequote

對此:

the dog is acting sneaky like a cat

但是,我收到此錯誤:

TypeError: expected string or bytes-like object

我嘗試使用apply()和lambda函數,但未成功。 有什么建議么?

我認為正則表達式需要應用於列的單個字符串

df['val'] = ['the tab dog is acting sneaky like a doublequote cat doublequote']

df.val.apply(lambda x: re.sub("|".join(tcl_list),'',x))

要么

df.val.str.replace("|".join(tcl_list),'')

出:

0    the  dog is acting sneaky like a  cat 
Name: val, dtype: object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM