python-3.xpandasdataformat

Convert Float with decimal to string without decimal Python 3.7


Struggling to convert a float 12345678.0 to a string 12345678 Gone through a few different solutions I've found in SO, but can't seem to get to the finish line, the decimal point will not drop.

Here is what I've tried

df["variable"]= df["variable"].astype(str)
df["variable"]= df["variable"].astype(int).astype(str)

# old lambda solution 
cols = ['variable']
for col in cols:
   df[col] = df[col].apply(lambda x: int(x) if x == x else "")

# convert int to string 
df["variable"] = df["variable"].astype(str)

Solution

  • You can do it simply like this:

    df['variable'] = df['variable'].astype(int, errors = 'ignore').astype(str)