首页 Python python – Pandas读取sql整数变为浮点数

python – Pandas读取sql整数变为浮点数

我遇到了一个问题,当我使用pandas读取Mysql表时,一些列(参见’to_nlc’)曾经是整数变成了一个浮点数(之后自动添加.0). 任何人都可以搞清楚吗?或者一些猜测?非常感谢! 问题是您的数据包含NaN值,因此int会自动转换为float. 我想你可以查看NA type promotions

我遇到了一个问题,当我使用pandas读取Mysql表时,一些列(参见’to_nlc’)曾经是整数变成了一个浮点数(之后自动添加.0).

任何人都可以搞清楚吗?或者一些猜测?非常感谢!

解决方法

问题是您的数据包含NaN值,因此int会自动转换为float.

我想你可以查看NA type promotions:

When introducing NAs into an existing Series or DataFrame via reindex or some other means,boolean and integer types will be promoted to a different dtype in order to store the NAs. These are summarized by this table:

Typeclass   Promotion dtype for storing NAs
floating    no change
object      no change
integer     cast to float64
boolean     cast to object

While this may seem like a heavy trade-off,in practice I have found very few cases where this is an issue in practice. Some explanation for the motivation here in the next section.

本文来自网络,不代表青岛站长网立场。转载请注明出处: https://www.0532zz.com/html/kaifa/python/20200921/10042.html
上一篇
下一篇

作者: dawei

【声明】:青岛站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

返回顶部