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字段类型转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短