python dataframe astype 字段类型转换方法


Posted in Python onApril 11, 2018

使用astype实现dataframe字段类型转换

# -*- coding: UTF-8 -*-
import pandas as pd
df = pd.DataFrame([{'col1':'a', 'col2':'1'}, {'col1':'b', 'col2':'2'}])
print df.dtypes
df['col2'] = df['col2'].astype('int')
print '-----------'
print df.dtypes
df['col2'] = df['col2'].astype('float64')
print '-----------'
print df.dtypes

输出结果:

col1  object
col2  object
dtype: object
-----------
col1  object
col2   int32
dtype: object
-----------
col1   object
col2  float64
dtype: object

注:data type list

Data type  Description
bool_  Boolean (True or False) stored as a byte
int_  Default integer type (same as C long; normally either int64 or int32)
intc  Identical to C int (normally int32 or int64)
intp  Integer used for indexing (same as C ssize_t; normally either int32 or int64)
int8  Byte (-128 to 127)
int16  Integer (-32768 to 32767)
int32  Integer (-2147483648 to 2147483647)
int64  Integer (-9223372036854775808 to 9223372036854775807)
uint8  Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
float_ Shorthand for float64.
float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_  Shorthand for complex128.
complex64  Complex number, represented by two 32-bit floats (real and imaginary components)
complex128 Complex number, represented by two 64-bit floats (real and imaginary components)

以上这篇python dataframe astype 字段类型转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python学习资料
Feb 08 Python
Python字符串匹配算法KMP实例
Jul 18 Python
Python在Windows和在Linux下调用动态链接库的教程
Aug 18 Python
python读取二进制mnist实例详解
May 31 Python
python爬取亚马逊书籍信息代码分享
Dec 09 Python
tensorflow 使用flags定义命令行参数的方法
Apr 23 Python
python读取文件名称生成list的方法
Apr 27 Python
实用自动化运维Python脚本分享
Jun 04 Python
Pyqt5自适应布局实例
Dec 13 Python
Python实现列表中非负数保留,负数转化为指定的数值方式
Jun 04 Python
Python使用tkinter实现小时钟效果
Feb 22 Python
详解Python小数据池和代码块缓存机制
Apr 07 Python
pandas series序列转化为星期几的实例
Apr 11 #Python
pandas的object对象转时间对象的方法
Apr 11 #Python
Python实现判断给定列表是否有重复元素的方法
Apr 11 #Python
python的dataframe转换为多维矩阵的方法
Apr 11 #Python
python的dataframe和matrix的互换方法
Apr 11 #Python
pandas DataFrame数据转为list的方法
Apr 11 #Python
Python paramiko模块的使用示例
Apr 11 #Python
You might like
PHP系统流量分析的程序
2006/10/09 PHP
关于Zend Studio 配色方案插件的介绍
2013/06/24 PHP
PHP设置images目录不充许http访问的方法
2016/11/01 PHP
laravel-admin 实现给grid的列添加行数序号的方法
2019/10/08 PHP
javascript 火狐(firefox)不显示本地图片问题解决
2008/07/05 Javascript
javascript 的Document属性和方法集合
2010/01/25 Javascript
锋利的jQuery 要点归纳(一) jQuery选择器
2010/03/21 Javascript
jQuery对html元素取值与赋值的方法
2013/11/20 Javascript
快速解决jQuery与其他库冲突的方法介绍
2014/01/02 Javascript
js 本地预览的简单实现方法
2014/02/18 Javascript
多种方法实现360浏览器下禁止自动填写用户名密码
2014/06/16 Javascript
如何用jQuery实现ASP.NET GridView折叠伸展效果
2015/09/26 Javascript
Websocket协议详解及简单实例代码
2016/12/12 Javascript
基于easyui checkbox 的一些操作处理方法
2017/07/10 Javascript
深入研究jQuery图片懒加载 lazyload.js使用方法
2017/08/16 jQuery
Mac 安装 nodejs方法(图文详细步骤)
2017/10/30 NodeJs
Vue CLI项目 axios模块前后端交互的使用(类似ajax提交)
2019/09/01 Javascript
Vue父组件向子组件传值以及data和props的区别详解
2020/03/02 Javascript
python版大富翁源代码分享
2018/11/19 Python
Python Pandas 转换unix时间戳方式
2019/12/07 Python
python之MSE、MAE、RMSE的使用
2020/02/24 Python
Python响应对象text属性乱码解决方案
2020/03/31 Python
python中pandas库中DataFrame对行和列的操作使用方法示例
2020/06/14 Python
Python requests模块安装及使用教程图解
2020/06/30 Python
解决c++调用python中文乱码问题
2020/07/29 Python
俄罗斯一家时尚女装商店:Charuel
2019/12/04 全球购物
迪卡侬中国官网:Decathlon中国
2020/08/10 全球购物
swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上?
2013/03/30 面试题
应届生财务会计求职信
2013/11/05 职场文书
工商企业管理专业自荐信范文
2014/04/12 职场文书
小学生成绩单评语
2014/12/31 职场文书
逃课检讨书
2015/01/26 职场文书
七个Python必备的GUI库
2021/04/27 Python
使用GO语言实现Mysql数据库CURD的简单示例
2021/08/07 Golang
Python matplotlib多个子图绘制整合
2022/04/13 Python
vue项目打包后路由错误的解决方法
2022/04/13 Vue.js