python中astype用法_浅谈python 中的 type(), dtype(), astype()的区别[通俗易懂]

python中astype用法_浅谈python 中的 type(), dtype(), astype()的区别[通俗易懂]如下所示:函数说明type()返回数据结构类型(list、dict、numpy.ndarray等)dtype()返回数据元素的数据类型(int、float等)备注:1)由于list、dict等可以包含不同的数据类型,因此不可调用dtype()函数2)np.array中要求所有元素属于同一数据类型,因此可调用dtype()函数astype()改变np.array中所有数据元素的数据类型。备注…

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

如下所示:

函数

说明

type()

返回数据结构类型(list、dict、numpy.ndarray 等)

dtype()

返回数据元素的数据类型(int、float等)

备注:1)由于 list、dict 等可以包含不同的数据类型,因此不可调用dtype()函数

2)np.array 中要求所有元素属于同一数据类型,因此可调用dtype()函数

astype()

改变np.array中所有数据元素的数据类型。

备注:能用dtype() 才能用 astype()

测试代码:

import numpy as np

class Myclass():

pass

a = [[1,2,3],[4,5,6]]

b = {‘a’:1,’b’:2,’c’:3}

c = np.array([1,2,3])

d = Myclass()

e = np.linspace(1,5,10)

c_ = c.astype(np.float)

f = 10

print(“type(a)=”,type(a))

print(“type(b)=”,type(b))

print(“type(c)=”,type(c))

print(“type(d)=”,type(d))

print(“type(e)=”,type(e))

print(“type(f)=”,type(f))

print(“type(c_)=”,type(c_))

# print(a.dtype) ## AttributeError: ‘list’ object has no attribute ‘dtype’

# print(b.dtype) ## AttributeError: ‘dict’ object has no attribute ‘dtype’

print(c.dtype)

# print(d.dtype) ## AttributeError: ‘Myclass’ object has no attribute ‘dtype’

print(e.dtype)

print(c_.dtype)

# print(f.dtype) ## AttributeError: ‘int’ object has no attribute ‘dtype’

# print(a.astype(np.int)) ## AttributeError: ‘list’ object has no attribute ‘astype’

# print(b.astype(np.int)) ## AttributeError: ‘dict’ object has no attribute ‘astype’

print(c.astype(np.int))

# print(d.astype(np.int)) ## AttributeError: ‘Myclass’ object has no attribute ‘astype’

print(e.astype(np.int))

# print(f.astype(np.int)) ## AttributeError: ‘int’ object has no attribute ‘astype’

补充知识:pandas astype()错误

由于数据出现错误

DataError: No numeric types to aggregate

改正以后才认识到astype的重要性。

Top15[‘populations’] = Top15[‘Energy Supply’].div(Top15[‘Energy Supply per Capita’]).astype(float)

df_mean = ((df.set_index(‘Continent’).groupby(level=0)[‘populations’].agg({‘mean’ : np.mean})))

#加了astype(float)后无错误

以上这篇浅谈python 中的 type(), dtype(), astype()的区别就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持python博客。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/142685.html原文链接:https://javaforall.net

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


相关推荐

  • linux中更改用户名_linux修改用户名和主目录

    linux中更改用户名_linux修改用户名和主目录Linux将用户名修改后,还需要修改组名+家目录+UID这只会更改用户名,而其他的东西,比如用户组,家目录,UID等都保持不变。1、修改用户名$usermod-l新用户旧用户这只会更改用户名,而其他的东西,比如用户组、家目录、ID等都保持不变。注意:你需要从要改名的帐号中登出并杀掉该用户的所有进程,要杀掉该用户的所有进程可以执行下面命令$sudopkill-u旧用户名$…

    2022年9月18日
    2
  • vue调用js文件_vue调用其他js文件中的方法

    vue调用js文件_vue调用其他js文件中的方法本文主要介绍了vue引用js文件的多种方式,本文大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 1、vue-cliwebpack全局引入jquery(1)首先npminstalljquery–save(–save的意思是将模块安装到项目目录下,并在package文件的dependencies节点写入依赖。)(2)在webpack.base.conf.js里加入varwebpack=require(“webpack”)(3)在module

    2022年10月8日
    2
  • cstring头文件都有什么函数_C语言头文件正确写法

    cstring头文件都有什么函数_C语言头文件正确写法首先,必须要清楚CString是怎么写的,是头两个字母大写!切记,不然就不能用!其次,CString是string的升级版,有很多好用的功能,使用CString一般需要包含头文件atlstr.h,在MFC程序中,可能不用包含,因为CString是MFC的基础功能。

    2022年9月12日
    2
  • Prophet快速安装方法

    Prophet快速安装方法安装VC++14,编译器https://download.csdn.net/download/amoscn/10399046安装Fbprophetcondainstallpystancondainstall-cconda-forgefbprophet安装绘图接口condainstallplotly

    2022年6月29日
    26
  • vue 图片上传 图片展示 bootstrap

    vue 图片上传 图片展示 bootstrap效果图html…………<--key=idPicUrl-->

    2022年6月22日
    39
  • LeetCode解题汇总目录

    此篇为学习完《数据结构与算法之美》后,在LeetCode刷题的汇总目录,方便大家查找(Ctrl+Find),一起刷题,一起PK交流!另有解题:《剑指Offer》、《程序员面试金典》、LintCode代码能力测试CAT。如果本文对你有帮助,可以给我点赞加油!通过2021,简单618/636,中等1120/1266,困难283/488

    2022年4月7日
    35

发表回复

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

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