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字段类型转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下