2016-08-16 50 views
3

我有一個相當大的從csv讀取的大熊貓DataFrame(約300萬行& 72列),並且我收到警告說某些列包含混合數據類型:警告:在非常大的數據幀的列中有多個數據類型

DtypeWarning: Columns (1,2,3,15,16,17,18,19,20,21,22,23,31,32,33,35,37,38,39,40,41,42,43,44,45,46,47,48,50,51,52,55,57,58,60,71) have mixed types. Specify dtype option on import or set low_memory=False. 
    interactivity=interactivity, compiler=compiler, result=result) 

如果我不能只注視csv,那麼處理這個問題的最佳方法是什麼?特別是,有沒有辦法獲得給定列中發生的所有數據類型的列表以及它們對應的行數?

+0

如果你知道前手的dtypes,你可以使用一個轉換器功能將數據丟在正確的類型,而閱讀... – Kartik

+0

@Kartik我不知道這個數據類型 – helloB

回答

2

考慮以下df

df = pd.DataFrame(dict(col1=[1, '1', False, np.nan, ['hello']], 
         col2=[2, 3.14, 'hello', (1, 2, 3), True])) 
df = pd.concat([df for _ in range(2)], ignore_index=True) 

df 

enter image description here

你可以研究不同類型和多少人有與

df.col1.apply(type).value_counts() 

<type 'float'> 2 
<type 'int'>  2 
<type 'list'>  2 
<type 'bool'>  2 
<type 'str'>  2 
Name: col1, dtype: int64 

你可以調查其中col1行是浮動像這樣

df[df.col1.apply(type) == float] 

enter image description here

相關問題