DataFrame的列中元素的混合类型 [英] Mixed types of elements in DataFrame's column

查看:149
本文介绍了DataFrame的列中元素的混合类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下三个DataFrame:

df1 = pd.DataFrame([[1,2],[4,3]])
df2 = pd.DataFrame([[1,.2],[4,3]])
df3 = pd.DataFrame([[1,'a'],[4,3]])

以下是DataFrame的第二列的类型:

Here are the types of the second column of the DataFrame's:

In [56]: map(type,df1[1])
Out[56]: [numpy.int64, numpy.int64]

In [57]: map(type,df2[1])
Out[57]: [numpy.float64, numpy.float64]

In [58]: map(type,df3[1])
Out[58]: [str, int]

在第一种情况下,所有int都强制转换为numpy.int64.美好的.在第三种情况下,基本上没有铸造.但是,在第二种情况下,整数(3)被强制转换为numpy.float64;可能是因为另一个数字是浮点数.

In the first case, all int's are casted to numpy.int64. Fine. In the third case, there is basically no casting. However, in the second case, the integer (3) is casted to numpy.float64; probably since the other number is a float.

如何控制投射?在第二种情况下,我想将[float64, int64][float, int]作为类型.

How can I control the casting? In the second case, I want to have either [float64, int64] or [float, int] as types.

使用可调用的打印功能,可以在此处中找到解决方法.... >

Using a callable printing function there can be a workaround as showed here.

def printFloat(x):
    if np.modf(x)[0] == 0:
        return str(int(x))
    else:
        return str(x)
pd.options.display.float_format = printFloat

推荐答案

pandas DataFrame(或Series)的列是同类的.您可以使用dtype(或DataFrame.dtypes)进行检查:

The columns of a pandas DataFrame (or a Series) are homogeneously of type. You can inspect this with dtype (or DataFrame.dtypes):

In [14]: df1[1].dtype
Out[14]: dtype('int64')

In [15]: df2[1].dtype
Out[15]: dtype('float64')

In [16]: df3[1].dtype
Out[16]: dtype('O')

仅通用'object' dtype可以容纳任何python对象,并且这种方式还可以包含混合类型:

Only the generic 'object' dtype can hold any python object, and in this way can also contain mixed types:

In [18]: df2 = pd.DataFrame([[1,.2],[4,3]], dtype='object')

In [19]: df2[1].dtype
Out[19]: dtype('O')

In [20]: map(type,df2[1])
Out[20]: [float, int]

但实际上不建议这样做,因为这样做会破坏熊猫的目的(或至少是表现).

But this is really not recommended, as this defeats the purpose (or at least the performance) of pandas.

您是否有一个原因想要在同一列中同时使用整数和浮点数?

Is there a reason you specifically want both ints and floats in the same column?

这篇关于DataFrame的列中元素的混合类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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