简体   繁体   English

PANDAS:将 int64 转换为字符串导致 object dtype

[英]PANDAS : converting int64 to string results in object dtype

I have a dataframe:我有一个 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') df1.dtypes 是浮点数,所以首先我将其转换为 int64 以删除.0 数字 df1.GL = df1.GL.astype('int64')

Then I try to convert it to str but instead I receive object dtype.然后我尝试将其转换为 str 但我收到 object dtype。

在此处输入图像描述

Does anyone know what can be the reason?有谁知道可能是什么原因?

You can force it to use the string dtype by using:您可以使用以下命令强制它使用string dtype:

>>> 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.但是, object dtypes 对于大多数字符串操作都很好。 As per the docs :根据 文档

For backwards-compatibility, object dtype remains the default type we infer a list of strings to为了向后兼容,object dtype 仍然是我们推断字符串列表的默认类型

The type object is actually string in pandas dataframe. object类型实际上是 pandas dataframe 中的字符串。

If you would like to retain the data as string, use df.to_excel() instead of df.to_csv .如果您想将数据保留为字符串,请使用df.to_excel()而不是df.to_csv This is because when opening the CSV file, Excel will automatically convert the number data to numbers.这是因为在打开 CSV 文件时,Excel 会自动将数字数据转换为数字。

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在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM