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)
上一篇 2022年5月10日 下午4:40
下一篇 2022年5月10日 下午5:00


相关推荐

  • klayout 源码分析系列4 ruler

    klayout 源码分析系列4 ruler1 首先 程序启动时建立一系列 ruler 的模板

    2025年8月28日
    6
  • ghost备份还原详细步骤_ghost一键备份还原

    ghost备份还原详细步骤_ghost一键备份还原注意点1:整个过程中不可动鼠标,使用键盘和触摸板操作。开始备份或还原后中不要动键盘备份从大白菜系统盘等方法进入GHOST依次进入Local→Partition(分区)→ToImage(到镜像文件)选择备份分区所在磁盘选择分区选择储存分区,写文件名字注意点2:移动备份后的文件极易造成文件的损坏,所以这里的位置一定要选好,之后不要移动位置选择压缩率(一般…

    2025年9月19日
    6
  • 远程代码托管平台–GitHub、Gitee的使用

    远程代码托管平台–GitHub、Gitee的使用本文章需要阅读者有Git基础,如果不知道Git是什么或者不知道Git的基本操作的小伙伴可以先看一看我上一篇文章:Git的介绍、安装及其基本操作在上一节中我们学习了目前全球最流行的分布式版本控制工具–Git的产生、安装以及基本使用,了解了如何通过Git进行版本控制,但是我们可以发现,在上一节中我们所有的操作都是在本地进行的(由工作区添加到暂存区,由暂存区提交到本地库),但是我们知道,在公司内部,一个项目的开发是由一个团队协作完成的,这种协作包括团队内协作和跨团队协作,那么如何实现团队协作呢?事实上,实

    2025年5月30日
    2
  • OpenClaw新手进阶全攻略:四步构建你的专属AI工作流

    OpenClaw新手进阶全攻略:四步构建你的专属AI工作流

    2026年3月14日
    3
  • springboot里面的porm配置问题

    springboot里面的porm配置问题本地jar包直接通过自建目录的方式加入MAVEN是行不通的,install项目的时候会出现该jar包missing问题。解决方式:通过控制台用命令行的方式加入。比如:mvninstall:install-file-Dfile=D:\workSpaceGD\DcloudServices\WebRoot\WEB-INF\lib\json-lib-2.4-jdk15.jar-Dgrou…

    2022年6月18日
    47
  • 几种更新(Update语句)查询的方法

    几种更新(Update语句)查询的方法

    2021年12月4日
    50

发表回复

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

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