我有一个熊猫系列。我想检查这个系列的dtype是否在一个dtype列表中。类似于:
series.dtype not in [pd.dtype('float64'), pd.dtype('float32')]这给了我以下错误:
AttributeError: 'module' object has no attribute 'dtype'我该怎么办?
发布于 2016-03-29 12:56:57
您可以使用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发布于 2016-03-29 12:56:40
没有像pd.dtype()这样的方法/东西--有np.dtype()或pd.np.dtype()
In [86]: pd.dtype
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-86-dbe1c1048375> in <module>()
----> 1 pd.dtype
AttributeError: module 'pandas' has no attribute 'dtype'
In [87]: np.dtype
Out[87]: numpy.dtype
In [88]: pd.np.dtype
Out[88]: numpy.dtype发布于 2016-03-29 12:56:44
使用select_dtypes和pass include=['floating']
In [11]:
df = pd.DataFrame({'float':[0.0, 1.2, 5.5], 'int':[0,1,2], 'str':list('abc')})
df
Out[11]:
float int str
0 0.0 0 a
1 1.2 1 b
2 5.5 2 c
In [12]:
df.select_dtypes(include=['floating'])
Out[12]:
float
0 0.0
1 1.2
2 5.5https://stackoverflow.com/questions/36284770
复制相似问题