2016-10-26 34 views
5

參照提取字符串前兩個字符:Pandas DataFrame: remove unwanted parts from strings in a column如何使用正則表達式

在參照上面的鏈接提供了答案。我已經研究了一些正則表達式,並且計劃深入探索,但同時我可以使用一些幫助。

我的數據幀是一樣的東西:

DF:

c_contofficeID 
0   0109 
1   0109 
2   3434 
3   123434 
4   1255N9 
5   0109 
6   123434 
7   55N9 
8   5599 
9   0109 

的僞代碼

如果前兩個字符是一個12刪除它們。或者,對前兩個字符中沒有12的字符添加12。

結果會是什麼樣子:

c_contofficeID 
0   0109 
1   0109 
2   3434 
3   3434 
4   55N9 
5   0109 
6   3434 
7   55N9 
8   5599 
9   0109 

我使用的是從鏈接答案以上爲出發點:

df['contofficeID'].replace(regex=True,inplace=True,to_replace=r'\D',value=r'') 

我已經試過如下:

嘗試1)

df['contofficeID'].replace(regex=True,inplace=True,to_replace=r'[1][2]',value=r'') 

嘗試2)

df['contofficeID'].replace(regex=True,inplace=True,to_replace=r'$[1][2]',value=r'') 

嘗試3)

df['contofficeID'].replace(regex=True,inplace=True,to_replace=r'?[1]?[2]',value=r'') 
+0

'^ 12'是正則表達式 –

+1

什麼 「與 '12' 開頭的」 如果你有 「1234」 ?應該保留「12」還是丟棄? –

回答

2

新的答案
根據註釋從@Addison

# '12(?=.{4}$)' makes sure we have a 12 followed by exactly 4 something elses 
df.c_contofficeID.str.replace('^12(?=.{4}$)', '') 

如果ID必須具備四個大字,這是更簡單到

df.c_contofficeID.str[-4:] 

老答案
使用str.replace

df.c_contofficeID.str.replace('^12', '').to_frame() 

enter image description here

+1

這很危險,因爲它不適用於'1234'。請使用'^ 12(?=。{4} $)' – Addison