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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL深分页问题解决的实战记录
2024-01-18 21:32:36
![](https://img.aspxhome.com/file/2023/6/81166_0s.png)
垂直无缝滚动图片(兼容性好)实例教程源码下载
2010-04-06 12:16:00
![](https://img.aspxhome.com/file/UploadPic/20104/6/scrollnews-65s.jpg)
浅谈MySQL在cmd和python下的常用操作
2024-01-16 13:11:17
python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用
2023-09-10 17:20:38
python实现五子棋程序
2022-05-26 23:24:31
![](https://img.aspxhome.com/file/2023/3/65973_0s.gif)
Python+unittest+requests+excel实现接口自动化测试框架
2021-09-08 07:04:49
![](https://img.aspxhome.com/file/2023/2/64732_0s.jpg)
python实现猜数字小游戏
2021-09-26 02:23:11
Python3爬虫里关于识别微博宫格验证码的知识点详解
2022-01-05 11:44:06
![](https://img.aspxhome.com/file/2023/8/133488_0s.png)
超详细注释之OpenCV制作图像Mask
2021-10-20 14:15:34
![](https://img.aspxhome.com/file/2023/3/98793_0s.jpg)
MySQL中union和order by同时使用的实现方法
2024-01-26 22:48:45
![](https://img.aspxhome.com/file/2023/9/124549_0s.png)
python绘制直线的方法
2022-10-08 20:01:52
![](https://img.aspxhome.com/file/2023/5/93565_0s.jpg)
[教程]用代码让你了解网页与键盘
2008-06-10 12:18:00
Python 实现Serial 与STM32J进行串口通讯
2021-03-05 16:01:33
python 实现汉诺塔游戏
2021-06-28 10:47:51
![](https://img.aspxhome.com/file/2023/0/95770_0s.png)
动态导航设计
2008-09-21 13:40:00
![](https://img.aspxhome.com/file/UploadPic/20089/21/2008921134132724s.jpg)
asp如何让我的网页自动适应客户端的屏幕分辨率?
2010-05-13 16:38:00
python:socket传输大文件示例
2022-04-18 19:44:26
import的本质解析
2023-07-12 07:09:24
Python如何调用JS文件中的函数
2022-11-21 01:23:11
![](https://img.aspxhome.com/file/2023/0/67850_0s.png)
javascript设置和获取cookie的方法实例详解
2024-04-22 13:05:08