Python 过滤器将数据类型重置为默认值的位置?
我有一个列数据类型显式设置为“int32”的dataframe。当我使用括号操作符进行过滤时,数据类型不会改变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()
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
是typeint32
,因此子集将具有相同的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