gpt4 book ai didi

python - 检查系列的 dtype 是否属于 pandas 中的 dtypes 列表

转载 作者:行者123 更新时间:2023-12-01 04:02:12 27 4
gpt4 key购买 nike

我有一个 Pandas 系列。我想检查该系列的数据类型是否在数据类型列表中。像这样的东西:

series.dtype not in [pd.dtype('float64'), pd.dtype('float32')]

这给了我以下错误:

AttributeError: 'module' object has no attribute 'dtype'

我应该做什么?

最佳答案

IIUC您可以使用dtypes来检查Series是否不是float:

print df
a b c
0 -1.828219 True 1.0
1 0.681694 False 2.0
2 -2.360949 True 1.0
3 1.034397 False 2.0
4 1.073993 True 1.0
5 1.306872 False 2.0

print df.a.dtype
float32
print df.b.dtype
bool

print df.a.dtype not in [pd.np.dtype('float64'), pd.np.dtype('float32')]
False
print df.b.dtype not in [pd.np.dtype('float64'), pd.np.dtype('float32')]
True

也可以使用np.dtype,如上所述MaxU :

print df.a.dtype not in [np.dtype('float64'), np.dtype('float32')] 
False
print df.b.dtype not in [np.dtype('float64'), np.dtype('float32')]
True

关于python - 检查系列的 dtype 是否属于 pandas 中的 dtypes 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36284770/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com