python - 比较 pandas 和 python 中的整数和浮点数是不一致的。为什么?
问题描述
熊猫在这里做了一些奇怪的事情dtypes
,我试图找出原因......
这是一个例子pd.DataFrame
。
df = pd.DataFrame([[1, 2, 3], [1.1, 2.2, 3.3]]).T.convert_dtypes()
它df.dtypes
是:
0 Int64
1 float64
dtype: object
我需要验证列是否是正确的数据类型,所以我执行以下操作:
df[1].dtype == float
我明白了True
。当我为0
( int
) 列执行此操作时:
df[0].dtype == int
我明白了False
如果我这样做,“验证”int
类型的唯一方法似乎是:df[0].dtype == pd.core.arrays.integer.Int64Dtype()
问题:为什么不一致?
解决方案
Pandasis_integer_dtype
就是为了这个:
df.dtypes
0 Int64
1 float64
dtype: object
# Both of these work. You can either pass a column or dtype
pd.api.types.is_integer_dtype(df[0])
pd.api.types.is_integer_dtype(df.dtypes[0])
# True
检查浮点数将使用is_float_dtype
. 测试其他 dtype 也有类似的功能,请仔细阅读文档以获取更多信息。
推荐阅读
- c# - MS Bot 框架中的自动填充选项
- assembly - 如何在 nasm 中为 BIN 文件创建 ELF 子系统?
- android - 适用于 API 10 的 AppCompatV7 最新版本
- node.js - Angular 4 - 使用 http 请求检测连接问题
- charts - 组合图表 - 使用多个 THEN/OVER 函数进行计算
- delphi - 使用 extCreatePen 的虚线不在 Delphi 中打印
- django - 从 Listview 中的其他模型获取字段的总和
- ios - 当没有为位置服务授予权限时,MKUserTrackingButton 会变成一个微调器
- php - 这个 php 操作符是做什么的?
- spring - 如何使用 Spring MVC 动态绑定表单元素