J. Doe
J. Doe

Reputation: 11

Stripping 0's from the middle of a dataframe

Basically data is coming into my program in this format 0xxxx000xxxx where the x is unique to the data that I have in another system. I'm trying to remove those 0's as they're always in the same place. I tried

df['item'] = df['item'].str.replace('0','')

but sometimes the x can be a 0 and will get rid of it. I'm not sure how to get rid of just the 0's in those specific positions.

EX: Input: 099890000890

Output (Desired): 99890890

Upvotes: 1

Views: 44

Answers (1)

mozway
mozway

Reputation: 262484

Use the str accessor for indexing:

df['item'] = df['item'].str[1:5] + df['item'].str[8:]

Or str.replace:

df['item'] = df['item'].str.replace(r'0(.{4})000(.{4})', r'\1\2', regex=True)

Output (as new column. Item2):

           item     item2
0  099890000890  99890890

Upvotes: 1

Related Questions