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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • totalcmd工具的主要功能_windows cmd 命令大全

    totalcmd工具的主要功能_windows cmd 命令大全一、Totalcmd配合7-Zip进行压缩、解压缩下载7z文件。可去官网下载安装版文件,再解压,只提取其中的7z.dll文件和7zG.exe文件再将其放在totalcmd\Utilities\7-Zip目录下。(注Utilities是我自己建的一个文件夹,用来存放一些可直接调用的小工具。)说明:虽说7-Zip有个叫7za.exe的命令行工具,但显示效果不佳…

    2025年8月3日
    4
  • Oracle创建表空间、创建用户的完整过程「建议收藏」

    Oracle创建表空间、创建用户的完整过程「建议收藏」我在工作学习过程中经常用到Oracle,由于经验欠缺,熟练度不足,每次都要百度一下。现在总结一下Oracle创建用户的过程,分享给大家。这里我就直接把sql语句贴出来,使用时直接复制粘贴即可。/*步骤:1、创建表空间2、创建用户3、用户授权*//*创建表空间*/createtablespaceQCJ_TABLESPACE…

    2022年7月11日
    29
  • 软件测试外包公司有哪些_工程资料外包的坏处

    软件测试外包公司有哪些_工程资料外包的坏处一、前言:什么是软件测试外包随着最近10年创业风气的发起,已经涌起创业项目外包公司的兴起,已经不仅仅局限为了降低成本,更多的是为了解决自己雇佣技术人员或者无法管理技术人员的难题。那么外包是什么意思呢?似乎大家对外包都闻之色变!这里我们详细的认识一下什么是外包?外包类似中介派遣公司或者叫做劳务输出公司。就是我们公司把你招聘进来,但是又把你派到其他公司(甲方)工作。但是最终你的劳务合同…

    2025年7月28日
    2
  • python中griddata_python – 来自xyz数据的Matplotlib轮廓:griddata无效索引[通俗易懂]

    python中griddata_python – 来自xyz数据的Matplotlib轮廓:griddata无效索引[通俗易懂]我正在尝试使用具有以下格式的文件的matplotlib来绘制等高线图:x1y1z1x2y2z2等等我可以用numpy.loadtxt加载它来获取向量.到目前为止,没有麻烦.我读这个是为了学习如何绘图,并且可以通过复制粘贴来重现它,所以我确定我的安装没有错:我知道我必须输入x和y作为矢量,z作为数组输入,这可以用griddata完成.这也是我在这个网站上找到的.文件说:zi=gridda…

    2022年5月26日
    39
  • CIO时代APP上线了!每天微讲座等让IT学习“兴趣随身”

    CIO时代APP上线了!每天微讲座等让IT学习“兴趣随身”

    2021年5月27日
    173
  • 点滴的积累—J2SE学习小结

    点滴的积累—J2SE学习小结

    2021年12月14日
    44

发表回复

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

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