Home > Mobile >  PANDAS : converting int64 to string results in object dtype
PANDAS : converting int64 to string results in object dtype

Time:08-11

I have a dataframe:

df1 = pd.DataFrame({'GL': [2311000200.0, 2312000600.0, 2330800100.0]})

df1.dtypes is float so first I convert it to int64 to removes .0 digitals df1.GL = df1.GL.astype('int64')

Then I try to convert it to str but instead I receive object dtype.

enter image description here

Does anyone know what can be the reason?

CodePudding user response:

You can force it to use the string dtype by using:

>>> df1.GL.astype("string")

df1.GL

0    2311000200.0
1    2312000600.0
2    2330800100.0
Name: GL, dtype: string

However, object dtypes are fine for most string operations. As per the docs:

For backwards-compatibility, object dtype remains the default type we infer a list of strings to

CodePudding user response:

The type object is actually string in pandas dataframe.

If you would like to retain the data as string, use df.to_excel() instead of df.to_csv. This is because when opening the CSV file, Excel will automatically convert the number data to numbers.

df1 = pd.DataFrame({'GL': [2311000200.0, 2312000600.0, 2330800100.0]})
df1.GL = df1.GL.astype('int64').astype('string')
df1.to_excel('test.xlsx', index=False)

enter image description here

  • Related