python类型转换astype时间_python dataframe astype 字段类型转换方法

python类型转换astype时间_python dataframe astype 字段类型转换方法使用astype实现dataframe字段类型转换#-*-coding:UTF-8-*-importpandasaspddf=pd.DataFrame([{‘col1′:’a’,’col2′:’1′},{‘col1′:’b’,’col2′:’2′}])printdf.dtypesdf[‘col2’]=df[‘col2’].astype(‘int’)print’–…

大家好,又见面了,我是你们的朋友全栈君。

使用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.net/135361.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java 二维数组排序

    java 二维数组排序①使用Comparator生成一个比较器对象初始化数组:int[][]arr=newint[m][n];排序规则:先按数组的第一个元素进行升序排序,若第一个元素相等,则按照第二个元素进行升序排序。使用API:Arrays.sort()(T[]a,Comparator<?superT>c),该API根据给定的比较器(设定排序方式)对指定的数组进行排序。代码实现:Arrays.sort(arr,newComparator<int[]>(){

    2022年6月5日
    34
  • 微信小程序 之 40029

    微信小程序 之 40029开发版正常使用,但是生成体验版之后报40029错误。错误原因:1:openid和当前小程序不对应,或者AppSecret秘钥和当前小程序不对应的。2:当前小程序能正确获取到token,但是在code还没有失效时,另一个小程序也用这个小程序的openid或者AppSecret秘钥去请求token。这个时候也会报40029,说到底还是第一个错误,只是触发的方式不同。解决办法也很简单,检查一下,当前的openid和AppSecret是否和当前要部署使用的小程序的openid和Ap..

    2022年5月6日
    48
  • 补码的运算方法「建议收藏」

    补码的运算方法「建议收藏」负数在计算机中是用补码的形式存储的,正数在计算机中是用原码的形式存储的。正数求原码直接将十进制转二进制即可,负数的补码是在原码的基础上除符号位外其余位取反后+1。但是用这种方式求负数补码用编程实现不太方便,下面介绍一种用编程实现起来较简便的求负数补码的方法:1.求出负数绝对值的原码2.从原码的最后一位数码位往前数,当遇到第一个1时停在此位置3.将第一个1前面的数码全部求反…

    2022年9月23日
    0
  • sparksql 概述

    sparksql 概述

    2021年11月27日
    44
  • [LeetCode] First Bad Version

    [LeetCode] First Bad Version

    2021年9月8日
    43
  • Lucene源码解析–TokenStream和AttributeSource

    Lucene源码解析–TokenStream和AttributeSource转 http://blog.itpub.net/28624388/viewspace-765691/一:Lucene的概况&lt;style./*Style.Definitions*/table.MsoNormalTable{mso-style-name:普通表格;mso-tstyle-rowband-size:0;mso-tstyle-colband-size:0;…

    2022年7月22日
    9

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号