pandas extract substring of column and put in the same column

David López :

I have this column:

C-042-00000017276
F-099-00000201997
F-98-204009

I want this column to be:

C-42-17276
F-99-201997
F-98-204009

I know how to extract the data through regex expression and I can solve it iterating over rows, But I want to do it more pandas style:

I am trying this for extract the pair of digits of the string between the '- symbols.

df['column'] = df['column'].str.replace(r'-.*',df['column'].str.extract(r'(-.*-)',expand=False).str.replace('-','').str.lstrip('0'))

but I get:

TypeError("repl must be a string or callable")

any suggestion with that?

Erfan :

We can use Series.str.replace for this with positive lookbehind.
Basically what we want is to replace one or more zeros (0+) if it's preceeded by a dash (?<=-):

df['column'] = df['column'].str.replace('(?<=-)0+', '')

        column
0   C-42-17276
1  F-99-201997
2  F-98-204009

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169347&siteId=1