我有一个熊猫系列。我想检查系列的dtype是否在dtypes列表中。类似的东西:
series.dtype not in [pd.dtype('float64'), pd.dtype('float32')]
这给了我以下错误:
AttributeError: 'module' object has no attribute 'dtype'
我该怎么办?
答案 0 :(得分:1)
没有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
答案 1 :(得分:1)
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
与提及的MaxU一样工作np.dtype
:
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
答案 2 :(得分:0)
使用select_dtypes
并传递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.5