leo
leo

Reputation: 441

Remove 2 trailing zero's in a pandas dataframe

I have a dataframe like the below and want to remove trailing zeros in pairs of 2.

col1
99990000
11100000
22220000
data = {'col1': ['99990000', '11100000', '22220000']}
df = pd.DataFrame(data=data)

desired result

col1
9999
1110
2222

The below removes all trailing zeros not and not keeping 1110?

df['col1'].str.replace('00+$','')
df['col1'].str.rstrip('00')

Upvotes: 1

Views: 399

Answers (3)

Sudhakar
Sudhakar

Reputation: 15

Try this, it should work

df['col1'].str[:4]

Upvotes: 0

David542
David542

Reputation: 110572

Just to conceptualize the regex, here is an example. The solution from @mozway is much better but this focuses on the substitution only, ignoring pandas:

import re
col1 = [re.sub(r'(00)+$', '', num) for num in
              ['99990000', '11100000', '22220000']]
# ['9999', '1110', '2222']

enter image description here

Upvotes: 1

mozway
mozway

Reputation: 262494

Your 00+ applies the repeater + only to the last 0, you need to use a group:

df['col1'].str.replace('(00)+$','')

Output:

0    9999
1    1110
2    2222
Name: col1, dtype: object

Upvotes: 3

Related Questions