erken
erken

Reputation: 207

Remove .0 from dataframe maintaining other numbers

I have a dataframe with only a column that is an object type and contains a list of IDs. Each ID finishes with a .0 like this:

0442.0
0447.0
0461.0
0583.0
0612.0

Now i Would like to remove the final .0 maintaining the zeros to the left, like:

  0442
  0447
  0461
  0583
  0612

I already tried to apply .astype(int), but that causes me the following error:

ValueError: invalid literal for int() with base 10: '0442.0'

Then, I tried to perform the following modification:

df.astype(float).astype(int)

and that works but deletes the zeros to the left. How can I remove only the .0 part? Thank you very much

Upvotes: 2

Views: 770

Answers (3)

ALollz
ALollz

Reputation: 59579

Your column is a string since there are leading 0s. So you need to replace the .0 at the end:

df[0] = df[0].str.replace('\.0$', '')
                            # |
                            # Only changes `.0` at end. 0.01.0 -> 0.01
#      0
#0  0442
#1  0447
#2  0461
#3  0583
#4  0612

Upvotes: 2

harpan
harpan

Reputation: 8641

You need to map it string, split with . and grab the first part.

df['col'] = df['col'].str.split('.').str[0]

Output:

    col
0   0442
1   0447
2   0461
3   0583
4   0612

Upvotes: 2

brentertainer
brentertainer

Reputation: 2200

df[0] = df[0].apply(lambda x: x.split('.')[0])

Upvotes: 0

Related Questions