2016-03-29 27 views
1

我有一個熊貓系列。我想檢查系列的dtype是否在dtypes列表中。喜歡的東西:檢查系列的dtype是否屬於熊貓中的dtypes列表

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

這給了我下面的錯誤:

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

我該怎麼辦?

+0

對不起正在測試的所有元素或整個homogoneous系列? – EdChum

+0

整個同質系列。熊貓與整個系列相關的dtype - 只要確保我理解正確。 – Swetabh

+0

沒有像'pd.dtype()'這樣的方法/有'''有'np.dtype()'或'pd.np.dtype()' – MaxU

回答

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 

作品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 
+0

接受這個,因爲有一個明顯的例子。 – Swetabh

+0

jezrael的一個小問題:df.a.type不在....或df.a.types中? – Swetabh

+0

我檢查文檔,它是['dtype'](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dtype.html)。你可以像'print type(df.at [0,'a'])'一樣檢查'Series'的' – jezrael

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 
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 
相關問題