[英]How to remove English Words from a column in a CSV file using Python
[英]Remove specific words from column using Python
數據最初來源於PDF 對數據做進一步分析,有一個[identity]列,其中一些值拼寫錯誤,即包含拼寫錯誤或特殊字符。
尋找從列中刪除不需要的字符。
輸入數據:
identity
UK25463AC
ID:- UN67342OM
#ID!?
USA5673OP
預期 Output:
identity
UK25463AC
UN67342OM
NAN
USA5673OP
到目前為止我嘗試過的腳本:
stop_word = ['#ID!?','ID:-']
pat = '|'.join(r"\b{}\b".format(x) for x in stop_words)
df['identity'] = df['identity'].str.replace(pat, '')
所以我不知道如何處理這個問題
從預期的 output 是必要的刪除單詞邊界\b\b
並且因為特殊的正則表達式 chcarecer 添加了re.escape
,然后將Series.replace
用於空字符串,如果只有空字符串到缺失值:
import re
stop_words = ['#ID!?','ID:-']
pat = '|'.join(r"{}".format(re.escape(x)) for x in stop_words)
df['identity'] = df['identity'].replace(pat, '', regex=True).replace('', np.nan)
print (df)
identity
0 UK25463AC
1 UN67342OM
2 NaN
3 USA5673OP
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.