這次給大家帶來object格式怎樣無損轉換成float64格式,object格式無損轉換成float64格式的注意事項有哪些,下面就是實戰案例,一起來看一下。
在數據處理過程中
比如從CSV文件中導入數據data_df = pd.read_csv("names.csv")
在處理之前一定要查看數據的類型data_df.info()*RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
Name 891 non-null object
Sex 891 non-null object
Age 714 non-null float64
SibSp 891 non-null int64
Parch 891 non-null int64
Ticket 891 non-null object
Fare 891 non-null float64
Cabin 204 non-null object
Embarked 889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.6+ KB*
以上object , int64, 以及 float64 便是數據的類型。
如果我們需要對列數據進行相互之間的運算的吧,必須注意的一點是:
兩列的數據類型是否是相同的!!
如果一個object類型與int64的類型相加,便會發生錯誤
錯誤提示可能如下:TypeError: ufunc 'add' not contain a loop with signature matching types dtype('
此時的object類型可能是‘12.3'這樣str格式的數字,如果要運算必須進行格式轉換:
可采用如下方法(convert_objects):dt_df = dt_df.convert_objects(convert_numeric=True)
親測有效。
再提醒一遍!得到數據一定要先查看數據類型!!!
相信看了本文案例你已經掌握了方法,更多精彩請關注Gxl網其它相關文章!
推薦閱讀:
object怎么轉換成float數據
Vue2父組件與子組件的雙向綁定怎么實現