python dataframe astype 字段类型转换方法

作者:伴生伴熟 时间:2022-02-19 07:58:50 

使用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)

来源:https://blog.csdn.net/chinacmt/article/details/52230339

标签:python,dataframe,astype
0
投稿

猜你喜欢

  • 用户体验杂谈

    2011-10-21 21:09:08
  • WEB2.0网页制作标准教程(12)XHTML校验及常见错误

    2008-02-19 19:59:00
  • 简单好用的PHP分页类

    2023-11-22 09:32:39
  • Python 2/3下处理cjk编码的zip文件的方法

    2022-08-05 17:53:40
  • 整理Python中常用的conda命令操作

    2022-07-17 16:53:33
  • Fibonacci数,Θ(log n)

    2010-03-28 13:28:00
  • IE8的一些CSS hack

    2008-03-17 13:08:00
  • python面试题之列表声明实例分析

    2022-01-10 12:46:51
  • CSS Sprites

    2007-10-10 13:21:00
  • Asp用XMLHTTP实现QQ状态在线查询

    2007-10-22 17:46:00
  • asp+ajax版四级联动菜单(数据库)

    2009-07-03 15:40:00
  • 原生JS实现左右箭头选择日期实例代码

    2023-08-06 04:55:27
  • Python绘制3d螺旋曲线图实例代码

    2022-12-22 01:30:23
  • Python 如何写入Excel格式和颜色

    2023-03-10 20:49:55
  • 网站设计输入了些什么?

    2008-04-01 09:30:00
  • python wav模块获取采样率 采样点声道量化位数(实例代码)

    2023-04-26 23:25:28
  • 跟老齐学Python之集合(set)

    2023-02-11 00:51:31
  • 数据库Oracle数据的异地的自动备份

    2010-07-27 13:28:00
  • 垂直栅格与渐进式行距(下)

    2009-07-09 16:52:00
  • asp任何取得多个表单的值

    2008-04-15 15:31:00
  • asp之家 网络编程 m.aspxhome.com