1

i have a dataframe from a csv file that loads with pandas.read_csv() method, looks like:

                   id  col
0 1151377158377549824 row0
1 1151346166619103232 row1
2 1151737502769827840 row2

types of columns is:

df.dtypes
out:
id        float64
col       object

i want to chang type of id to string but using astype(str) or apply(str), after convert it changes to scientific notation:

                      id  col
0 1.1513771583775498e+18 row0
1 1.1513461666191032e+18 row1
2 1.1517375027698278e+18 row2

what should i do to avoid scientific notation after converting?

  • 1
    The `id` column is not a floating point number, and you should prevent pandas from reading it as such. If you want to have as as a string, use the `dtype` argument to `read_csv` to read it as a an object. – cel Apr 11 '20 at 08:59
  • Your id column dtype is being wrongly inferred as float, as cel says. Use explicit dtype – smci Apr 11 '20 at 09:00
  • maybe i need this columns as float like string, then what? – mehdi nemati Apr 11 '20 at 09:03
  • python automatically interpret the id column as float so you are getting scientific notation. So while reading the csv specify the data type of column pd.read_csv("data.csv", dtype={'id': 'Int64'}) Above line will solve the problem while reading the df – saravanan saminathan Jan 19 '22 at 17:45

1 Answers1

1

you can convert to Int64 and then to string:

df['id'] = df['id'].astype("Int64").astype(str)
df

output:

enter image description here

kederrac
  • 16,819
  • 6
  • 32
  • 55
  • Be careful when having nans as they are replaced by "". Other solutions are mentioned here: https://stackoverflow.com/questions/41157981/pandas-convert-float-in-scientific-notation-to-string – Guido Jun 08 '22 at 14:21