![](/img/trans.png)
[英]How to extract the two characters representing country in a domain in a string by using regex?
[英]How to extract first two characters from string using regex
參考: Pandas DataFrame:從列中的字符串中刪除不需要的部分
參考上面鏈接中提供的答案。 我研究了一些正則表達式,我打算深入研究,但與此同時我可以使用一些幫助。
我的數據框是這樣的:
DF:
c_contofficeID
0 0109
1 0109
2 3434
3 123434
4 1255N9
5 0109
6 123434
7 55N9
8 5599
9 0109
Psuedo Code
如果前兩個字符是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'')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.