2016-12-30 589 views
1

我試圖使用.map()將DataPrame中的列數據類型從type: object更改爲type: int64將Pandas DataFrame中的列類型更改爲int64

df['one'] = df['one'].map(convert_to_int_with_error) 

這裏是我的功能:

def convert_to_int_with_error(x): 
    if not x in ['', None, ' ']: 
     try: 
      return np.int64(x) 
     except ValueError as e: 
      print(e) 
      return None 
    else: 
     return None 

    if not type(x) == np.int64(): 
     print("Not int64") 
     sys.exit() 

這成功完成。然而,當我完成後,檢查數據類型,其恢復爲type: float

print("%s is a %s after converting" % (key, df['one'].dtype)) 
+0

正是你在哪裏把'如果沒有輸入(X)== np.int64():'條件?你是否說'convert_to_int_with_error'永遠不會返回'None'? – unutbu

+1

對於數字容器,'None'將被視爲'NaN',以保持它的'float'(數值)D型。你需要找到一種方法來處理這些缺失的值/空字符串,這樣會導致'np.int64' dtype。 –

回答

1

我認爲,問題是你有問題的值從None轉換爲NaN,所以int被轉換爲float - 看docs

相反map您可以使用to_numeric與轉換問題的數值參數errors='coerce'NaN

df['one'] = pd.to_numeric(df['one'], errors='coerce') 
+0

我包括嘗試,除了考慮爲無法轉換爲正確雖然Int64值? – Scott

+1

不幸的是,不可能用'NaN'或'None'值得到'dtype'' int'。 – jezrael

相關問題