python dataframe astype 字段类型转换方法
使用astype实现dataframe字段类型转换
#-*-coding:UTF-8-*- importpandasaspd df=pd.DataFrame([{'col1':'a','col2':'1'},{'col1':'b','col2':'2'}]) printdf.dtypes df['col2']=df['col2'].astype('int') print'-----------' printdf.dtypes df['col2']=df['col2'].astype('float64') print'-----------' printdf.dtypes
输出结果:
col1object col2object dtype:object ----------- col1object col2int32 dtype:object ----------- col1object col2float64 dtype:object
注:datatypelist
DatatypeDescription bool_Boolean(TrueorFalse)storedasabyte int_Defaultintegertype(sameasClong;normallyeitherint64orint32) intcIdenticaltoCint(normallyint32orint64) intpIntegerusedforindexing(sameasCssize_t;normallyeitherint32orint64) int8Byte(-128to127) int16Integer(-32768to32767) int32Integer(-2147483648to2147483647) int64Integer(-9223372036854775808to9223372036854775807) uint8Unsignedinteger(0to255) uint16Unsignedinteger(0to65535) uint32Unsignedinteger(0to4294967295) uint64Unsignedinteger(0to18446744073709551615) float_Shorthandforfloat64. float16Halfprecisionfloat:signbit,5bitsexponent,10bitsmantissa float32Singleprecisionfloat:signbit,8bitsexponent,23bitsmantissa float64Doubleprecisionfloat:signbit,11bitsexponent,52bitsmantissa complex_Shorthandforcomplex128. complex64Complexnumber,representedbytwo32-bitfloats(realandimaginarycomponents) complex128Complexnumber,representedbytwo64-bitfloats(realandimaginarycomponents)
以上这篇pythondataframeastype字段类型转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。