python中object转为float_object格式怎样无损转换成float64格式
這次給大家?guī)韔bject格式怎樣無損轉(zhuǎn)換成float64格式,object格式無損轉(zhuǎn)換成float64格式的注意事項(xiàng)有哪些,下面就是實(shí)戰(zhàn)案例,一起來看一下。
在數(shù)據(jù)處理過程中
比如從CSV文件中導(dǎo)入數(shù)據(jù)data_df = pd.read_csv("names.csv")
在處理之前一定要查看數(shù)據(jù)的類型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 便是數(shù)據(jù)的類型。
如果我們需要對(duì)列數(shù)據(jù)進(jìn)行相互之間的運(yùn)算的吧,必須注意的一點(diǎn)是:
兩列的數(shù)據(jù)類型是否是相同的!!
如果一個(gè)object類型與int64的類型相加,便會(huì)發(fā)生錯(cuò)誤
錯(cuò)誤提示可能如下:TypeError: ufunc 'add' not contain a loop with signature matching types dtype('
此時(shí)的object類型可能是‘12.3'這樣str格式的數(shù)字,如果要運(yùn)算必須進(jìn)行格式轉(zhuǎn)換:
可采用如下方法(convert_objects):dt_df = dt_df.convert_objects(convert_numeric=True)
親測(cè)有效。
再提醒一遍!得到數(shù)據(jù)一定要先查看數(shù)據(jù)類型!!!
相信看了本文案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注Gxl網(wǎng)其它相關(guān)文章!
推薦閱讀:
object怎么轉(zhuǎn)換成float數(shù)據(jù)
Vue2父組件與子組件的雙向綁定怎么實(shí)現(xiàn)
總結(jié)
以上是生活随笔為你收集整理的python中object转为float_object格式怎样无损转换成float64格式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 混合开发 h5+ 沉浸式的适配
- 下一篇: 每天定时打开某个网页_Python科普帖