Home > Software engineering >  Remove decimal number in dataframe column that is stored as object?
Remove decimal number in dataframe column that is stored as object?

Time:04-21

So I have an issue where the data is stored under a dataframe like an object, which I don't mind at all, however some of the rows that are integers have a trailing .0 and I haven't been able to truncate them.

ID column_object
adssfdg D1esdf
jAEOJDS NaN
SDFJKFAS NaN
DSFADSF 323653.0
SDFASF 43253.0

I would like to remove that trailing 0 in order for the final data to look something like this:

ID column_object
adssfdg D1esdf
jAEOJDS NaN
SDFJKFAS NaN
DSFADSF 323653
SDFASF 43253

Any tips on how to this?

CodePudding user response:

If you don't mind converting the integers to strings, then you could use str.rstrip:

df['column_object'] = df['column_object'].where(lambda x: x.isna(), df['column_object'].astype(str).str.rstrip('.0'))

But if you want to keep the integers as integers, you could use numpy.where. But this will be really weird down the road when you process this data:

import numpy as np
tmp = pd.to_numeric(df['column_object'], errors='coerce')
df['column_object'] = np.where(tmp.notna(), tmp.fillna(0).astype(int), df['column_object'])

Output:

         ID column_object
0   adssfdg        D1esdf
1   jAEOJDS           NaN
2  SDFJKFAS           NaN
3   DSFADSF        323653
4    SDFASF         43253
  • Related