python类型转换astype时间_python dataframe astype 字段类型转换方法
大家好,又见面了,我是你们的朋友全栈君。
使用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 字段类型转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/135361.html原文链接:https://javaforall.cn
相关文章
- 【Python】Ubuntu修改默认Python版本
- Python 学生信息管理系统——文章中源码100%真实有效—–如何将类、初始化属性、模块、循环判断、静态方法等一系列知识点结合起来做一个项目「建议收藏」
- SPC(Statistical Process Control 统计过程控制)图——Python+JS实现
- Python基础24-MySQL模块pymysql
- 记一次python清洗疫情历史数据
- jupyter和python的关系_jupyter notebook和python
- 建议收藏!Python 读取千万级数据自动写入 MySQL 数据库
- 知乎高赞!有没有适合新手练习 Python 的做题类网站?
- 用 Python 破解 WiFi 密码,太刺激了!
- Python turtle 模块可以编写游戏,是真的吗?
- python提取xml指定内容
- 快过年了,用Python写副春联&福字送给你~
- Python中的取整、取余运算「建议收藏」
- Python基础15-日志模块logging
- python报错invalid syntax_fatal python error
- 用Python分析2000款避孕套,得出这些有趣的结论
- Python语音信号处理
- Python模拟一个用户登录系统
- python 字符串(字符序列)和字节序列
- Python字符串转换为日期时间– strptime()「建议收藏」