簡體   English   中英

如何使用正則表達式從字符串中提取前兩個字符

[英]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'')

新的答案
來自@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()

在此輸入圖像描述

暫無
暫無

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

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