我有这个专栏:
C-042-00000017276
F-099-00000201997
F-98-204009
我希望此栏是:
C-42-17276
F-99-201997
F-98-204009
我知道如何通过正则表达式提取数据,并且可以通过迭代行来解决它,但我想做更多的 pandas 风格:
我正在尝试提取“-”符号之间的字符串数字对。
df['column'] = df['column'].str.replace(r'-.*',df['column'].str.extract(r'(-.*-)',expand=False).str.replace('-','').str.lstrip('0'))
但我得到:
TypeError("repl must be a string or callable")
有什么建议吗?
最佳答案
我们可以使用Series.str.replace
为此 positive lookbehind
.
基本上我们想要的是替换一个或多个零( 0+
),如果它前面有破折号 (?<=-)
。 :
df['column'] = df['column'].str.replace('(?<=-)0+', '')
column
0 C-42-17276
1 F-99-201997
2 F-98-204009
关于python - pandas 提取列的子字符串并放入同一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60609457/