简体   繁体   中英

pandas extract substring of column and put in the same column

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM