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中的JSON模块
Apr 08 Python
以911新闻为例演示Python实现数据可视化的教程
Apr 23 Python
使用Python的PIL模块来进行图片对比
Feb 18 Python
pandas中Timestamp类用法详解
Dec 11 Python
python 3.6 +pyMysql 操作mysql数据库(实例讲解)
Dec 20 Python
python通过微信发送邮件实现电脑关机
Jun 20 Python
python实现自动获取IP并发送到邮箱
Dec 26 Python
Python中最大递归深度值的探讨
Mar 05 Python
浅谈Python批处理文件夹中的txt文件
Mar 11 Python
python列表,字典,元组简单用法示例
Jul 11 Python
解决Django Haystack全文检索为空的问题
May 19 Python
Python实现学生管理系统并生成exe可执行文件详解流程
Jan 22 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版(3)
2006/10/09 PHP
php实现singleton()单例模式实例
2014/11/06 PHP
php正则表达式获取内容所有链接
2015/07/24 PHP
jquery实现多级下拉菜单的实例代码
2013/10/02 Javascript
JS的document.all函数使用示例
2013/12/30 Javascript
jQuery Easyui实现左右布局
2016/01/26 Javascript
Javascript数组Array基础介绍
2016/03/13 Javascript
Angular 2 利用Router事件和Title实现动态页面标题的方法
2017/08/23 Javascript
基于Vue过渡状态实例讲解
2017/09/14 Javascript
[01:40]2014DOTA2国际邀请赛 三冰SOLO赛后采访恶搞
2014/07/09 DOTA
Python3 入门教程 简单但比较不错
2009/11/29 Python
详解Python如何获取列表(List)的中位数
2016/08/12 Python
利用python写个下载teahour音频的小脚本
2017/05/08 Python
python PyTorch预训练示例
2018/02/11 Python
python2.7安装图文教程
2018/03/13 Python
python 将字符串完成特定的向右移动方法
2019/06/11 Python
使用python实现哈希表、字典、集合操作
2019/12/22 Python
Python图像处理库PIL的ImageDraw模块介绍详解
2020/02/26 Python
python实现ssh及sftp功能(实例代码)
2020/03/16 Python
浅谈Python中文件夹和python package包的区别
2020/06/01 Python
Banana Republic欧盟:美国都市简约风格的代表品牌
2018/05/09 全球购物
英国复古服装和球衣购买网站:3Retro Football
2018/07/09 全球购物
Pretty Little Thing美国:时尚女性服饰
2018/08/27 全球购物
优质美利奴羊毛袜,不只是徒步旅行:Darn Tough Vermont
2018/11/05 全球购物
Wiggle澳大利亚:自行车、跑步、游泳商店
2020/11/07 全球购物
测试时代收集的软件测试面试题
2013/09/25 面试题
秘书专业自荐信范文
2013/12/26 职场文书
集体婚礼策划方案
2014/02/22 职场文书
超市开店计划书
2014/04/26 职场文书
英语系毕业生求职信
2014/07/13 职场文书
ktv好的活动方案
2014/08/17 职场文书
2014年财政所工作总结
2014/11/22 职场文书
2014年信访维稳工作总结
2014/12/08 职场文书
《我们的民族小学》教学反思
2016/02/19 职场文书
全国劳模先进事迹材料(2016精选版)
2016/02/25 职场文书
Java图书管理系统,课程设计必用(源码+文档)
2021/06/30 Java/Android