我正在嘗試根據 first_name 列減少 csv 檔案內容,當我說減少時,我試圖僅過濾掉其中包含拉丁字符的那些行。
我的資料看起來像這樣,
A_ID ID_NUMBER DT25 DT45
abcd 0001 Condé and Geoff Shallard
abcd 555248817 R?ndi & John Fay
abcd 54786 Randy john
abcd 006299 László and Virginia Csernohorszky-Hope
abcd 000323 Kim Jonh
abcd 01012 Larry will
我只是想在 DT25 中創建一個包含 SPL/拉丁字符的所有行的 DF,
預期的輸出類似于:
A_ID ID_NUMBER DT25 DT45
abcd 0001 Condé and Geoff Shallard
abcd 555248817 R?ndi & John Fay
abcd 006299 László and Virginia Csernohorszky-Hope
我試過這個,
import string
df = pd.read_csv(filename)
pattern = "^[a-zA-Z-'&.]*$"
alphabet = string.ascii_letters string.punctuation
#first_name_df = df[~df['DT25'].str.contains(alphabet, na = False)]
first_name_df = df[~df['DT25'].str.contains(pattern, na = False)]
print(first_name_df)
這又給了我原始的 DF。熊貓專家可以幫我解決這個問題嗎?
uj5u.com熱心網友回復:
您可以使用正則運算式[^\t-\r -~]:
filtered = df[df['DT25'].str.contains('[^\t-\r -~]')]
輸出:
>>> filtered
A_ID ID_NUMBER DT25
0 abcd 1 Condé and Geoff Shallard
1 abcd 555248817 R?ndi & John Fay
3 abcd 6299 László and Virginia Csernohorszky-Hope
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/449888.html
上一篇:如何將每2行合并為1行
