1

i am trying to eliminate my ".0" on a specified column. The dtype for the column is an object. I export my dataframe as a .txt file and it keeps showing with decimals. I do not want to round.

I've tried several methods,

df['foDealId'] = df['foDealId'].astype(str).replace('\.0','',regex=True) . I get no change in my data.

and

df['foDealId'] = df['foDealId'].str.replace('.0','') . I get my number truncated in the middle of it where there was a 0. ie 2451669285571 14

and

df['foDealId'].loc[mask] = df['foDealId'].loc[mask].astype(np.int64)
df['foDealId'] = pd.to_numeric(df.foDealId, errors='coerce').fillna(0).astype(np.int64)

any ideas on how to fix?

excelguy
  • 1,574
  • 6
  • 33
  • 67
  • Does this answer your question? [round pandas column with precision but no trailing 0](https://stackoverflow.com/questions/47542657/round-pandas-column-with-precision-but-no-trailing-0) – abdoulsn Dec 13 '19 at 16:21
  • Hello, like it's duplicated (ps it's not me the -1 :D) look this [post](https://stackoverflow.com/questions/42403907/how-to-round-remove-traling-0-zeros-in-pandas-column#42403995) – abdoulsn Dec 13 '19 at 16:23
  • 1
    I've tried that post as well. Also i dont want to round. Let me update with that try. – excelguy Dec 13 '19 at 16:32
  • Add an r before ur regex `replace(r'\.0',`.. Should do the trick – Umar.H Dec 13 '19 at 16:36
  • If the column is an integer column, (numbers where you don't care about leading 0s) then you may want to consider using `.astype('Int64')` (note the capital I). Normal `int` dtypes cannot hold NaN, but `Int64` can. If those are ID numbers where there can be leading 0s, you should probably specify `dtype={'foDealId': 'str'}` for the column when you import the data, and likely you wont have any upcasting to float which leads to this problem. – ALollz Dec 13 '19 at 16:42
  • @Datanovice , tried that, still not working. sigh. – excelguy Dec 13 '19 at 16:59
  • @ALollz , tried your first suggestion, getting `TypeError: object cannot be converted to an IntegerDtype` , How do i specify dtype? Im not reading csv for my data. – excelguy Dec 13 '19 at 17:13
  • 1
    Can you add 2-3 rows of sample data along with your expected output please. – cs95 Dec 13 '19 at 19:11

0 Answers0