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程序语言快速上手教程
Jul 18 Python
列举Python中吸引人的一些特性
Apr 09 Python
Sanic框架流式传输操作示例
Jul 18 Python
Python爬虫实战之12306抢票开源
Jan 24 Python
详解Python的数据库操作(pymysql)
Apr 04 Python
使用Fabric自动化部署Django项目的实现
Sep 27 Python
Python socket聊天脚本代码实例
Jan 02 Python
python matplotlib画盒图、子图解决坐标轴标签重叠的问题
Jan 19 Python
Python安装whl文件过程图解
Feb 18 Python
Python实现疫情通定时自动填写功能(附代码)
May 27 Python
Python中zipfile压缩文件模块的基本使用教程
Jun 14 Python
Python日志模块logging用法
Jun 05 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
图书管理程序(三)
2006/10/09 PHP
分享一下贝贝成长进度的php代码
2012/09/14 PHP
PHP冒泡算法详解(递归实现)
2014/11/10 PHP
PHP批量查询WordPress留言者E-mail地址实现方法
2015/02/15 PHP
PHP通过API获取手机号码归属地
2015/05/28 PHP
php生成二维码
2015/08/10 PHP
Ubuntu 16.04下安装PHP 7过程详解
2017/03/28 PHP
CheckBox 如何实现全选?
2006/06/23 Javascript
JavaScript建立一个语法高亮输入框实现思路
2013/02/26 Javascript
javascript中简单的进制转换代码实例
2013/10/26 Javascript
浅析JQuery中的html(),text(),val()区别
2014/09/01 Javascript
简介JavaScript中search()方法的使用
2015/06/06 Javascript
jQuery基于cookie实现的购物车实例分析
2015/12/24 Javascript
jQuery中使用animate自定义动画的方法
2016/05/29 Javascript
nodejs实现发出蜂鸣声音(系统报警声)的方法
2017/01/18 NodeJs
基于JS实现仿京东搜索栏随滑动透明度渐变效果
2017/07/10 Javascript
JS中的BOM应用
2018/02/02 Javascript
小程序云开发初探(小结)
2018/10/24 Javascript
nodejs使用async模块同步执行的方法
2019/03/02 NodeJs
使用webpack编译es6代码的方法步骤
2019/04/28 Javascript
[01:14:12]2018DOTA2亚洲邀请赛4.7 总决赛 LGD vs Mineski 第二场
2018/04/09 DOTA
Python 匹配任意字符(包括换行符)的正则表达式写法
2009/10/29 Python
举例介绍Python中的25个隐藏特性
2015/03/30 Python
Python中的字符串操作和编码Unicode详解
2017/01/18 Python
Python 列表(List) 的三种遍历方法实例 详解
2017/04/15 Python
python3实现windows下同名进程监控
2018/06/21 Python
Python爬虫抓取技术的一些经验
2019/07/12 Python
解决Python Matplotlib绘图数据点位置错乱问题
2020/05/16 Python
测绘工程系学生的自我评价
2013/11/30 职场文书
十佳少先队员演讲稿
2014/09/12 职场文书
保安2014年终工作总结
2014/12/06 职场文书
劳动保障个人工作总结
2015/03/04 职场文书
大学生求职自荐信范文
2015/03/04 职场文书
罗马假日观后感
2015/06/08 职场文书
股东出资协议书
2016/03/21 职场文书