使用python pandas在合并的数据框中整数变为小数 [英] Integer becomes decimal in merged dataframe using python pandas

查看:913
本文介绍了使用python pandas在合并的数据框中整数变为小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个csv文件,所有数字字段均为int,无小数. 当我使用pandas merge函数合并两个数据框时,我发现一个数据框中的int字段都变成了十进制,为什么会这样?

I have two csv file and all the numeric fields are int, no decimal. When I use pandas merge function to join two dataframe, I found the int fields in one dataframe all became decimal, why that happens?

推荐答案

DataFrame的每个列都有一个dtype. dtype控制该列中可以包含哪些类型的值.毫无疑问,具有整数dtypes的列只能包含整数.具有浮点dtypes的列仅包含浮点数-并且NaN是浮点数:

Each column of a DataFrame has a dtype. The dtype controls what kinds of values can be contained in that column. Columns with integer dtypes, unsurprisingly, can contain only integers. Columns with floating point dtypes contain only floats -- and NaN is a float:

In [191]: isinstance(np.nan, float)
Out[191]: True

因此,即使agescore是整数列,由于合并的age_yscore_y列包含NaN,因此dtype必须升级为浮点dtype以容纳.

So even though age and score are integer-valued columns, since the merged age_y and score_y columns contain NaN, the dtype must be upgraded to a floating point dtype to accommodate the NaN.

这篇关于使用python pandas在合并的数据框中整数变为小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆