加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何使用正则表达式从字符串中提取前两个字符

发布时间:2020-12-14 06:08:11 所属栏目:百科 来源:网络整理
导读:参考: Pandas DataFrame: remove unwanted parts from strings in a column 参考上面链接中提供的答案.我研究了一些正则表达式,我打算深入研究,但与此同时我可以使用一些帮助. 我的数据框是这样的: DF: c_contofficeID0 01091 01092 34343 123434 4 1255N
参考: 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

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,to_replace=r'[1][2]',value=r'')

尝试2)

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

尝试3)

df['contofficeID'].replace(regex=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()

enter image description here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读