Reputation: 529
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?
Upvotes: 2
Views: 130
Reputation: 42916
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
Upvotes: 3