Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 过滤器将数据类型重置为默认值的位置?_Python_Python 3.x_Pandas - Fatal编程技术网

Python 过滤器将数据类型重置为默认值的位置?

Python 过滤器将数据类型重置为默认值的位置?,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个列数据类型显式设置为“int32”的dataframe。当我使用括号操作符进行过滤时,数据类型不会改变 scripts[scripts['Security Id'] == 'ABB']['Security Code'].head() 0 500002名称:安全代码,数据类型:int32 但是,当我使用where筛选时,数据类型会重置回默认值,即float64 (scripts.where(scripts['Security Id'] == 'ABB') .dropna()

我有一个列数据类型显式设置为“int32”的dataframe。当我使用括号操作符进行过滤时,数据类型不会改变

scripts[scripts['Security Id'] == 'ABB']['Security Code'].head()
0 500002名称:安全代码,数据类型:int32

但是,当我使用where筛选时,数据类型会重置回默认值,即float64

(scripts.where(scripts['Security Id'] == 'ABB')
       .dropna())['Security Code'].head()
数据类型更改回“float64”

0 500002.0名称:安全代码,数据类型:float64


所以我只是想知道为什么会出现这种情况,特别是因为操作符链接是熊猫的惯用方式。

第二种情况下
dtype
的变化是
numpy
没有
NaN
的整数表示的结果。因此,如果数值列中有
NaN
,则
dtype
将转换为
float

在第一种情况下:

scripts[scripts['Security Id'] == 'ABB']['Security Code'].head()
您只是根据条件返回
脚本的子集。由于底层的
DataFrame
是type
int32
,因此子集将具有相同的
dtype

但是,在第二种情况下,
DataFrame.where
返回一个对象,它在其中传递条件为
True
的行中的值,但在其他情况下用
np.NaN
替换该值。因此,您正在修改
DataFrame
并引入
NaN
值,这将强制
pandas
将列强制转换为
float64

例如:

import pandas as pd
scripts = pd.DataFrame({'Security Id': ['ABB', 'ABB', 'ABC', 'ABB'],
                        'Security Code': [1, 2, 3, 4]})
scripts['Security Code'] = scripts['Security Code'].astype('int32')

scripts.where(scripts['Security Id'] == 'ABB')

   Security Code Security Id
0            1.0         ABB
1            2.0         ABB
2            NaN         NaN
3            4.0         ABB
Security Code    float64
Security Id       object
dtype: object