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


相关推荐

  • vs2010下载官方下载_怎样下载vs2010

    vs2010下载官方下载_怎样下载vs2010http://www.microsoft.com/downloads/en/results.aspx?freetext=Visual+Studio+2010&displaylang=en&am

    2022年8月4日
    3
  • systick的使用方法_cleardevice函数

    systick的使用方法_cleardevice函数systick的原理前一篇博文有介绍,简而言之就是SysTick定时器是一个24位的倒计数,当倒计数为0时,将从RELOAD寄存器中取值作为定时器的初始值,同时可以选择在这个时候产生中断(异常号:15

    2022年8月6日
    3
  • pip如何卸载库_查看python版本的命令是

    pip如何卸载库_查看python版本的命令是pipfreeze>allpackages.txtpipuninstall-rallpackages.txt-y

    2022年10月10日
    1
  • C / C++ 读取文件出现乱码解决方法 | 输出到文件出现乱码

    C / C++ 读取文件出现乱码解决方法 | 输出到文件出现乱码  昨天用C语言写了一下文件读取,发现读出来的全是乱码。这肯定是文字编码不同导致的。    据我查证,C语言的汉字编码方式是由你电脑决定的,所以需要看一下你电脑是什么编码,来确定你需要把文本文件改成什么编码。1.win+R,打开运行框之后输入cmd打开,然后在cmd最上边右键→属性,点开就可以查看当前编码方式,我的电脑是GBK。2.然后修改对应的文本文件编码方式。…

    2022年7月26日
    58
  • 语音信号处理分类

    语音信号处理分类人与人之间的通信:语音压缩与编码压缩语音信号的传输带宽或者降低电话信道的传输码率解释:比特率=位深X采样率X信道个数,比特率越高,还原度越高,速度越慢。所以降低编码比特率,可以节省频率资源。第一类人与机器之间的通信:语音合成机器讲话,人接听文语转换(TTS)系统,主要关注可理解度,自然度,可懂度,逼真度(情感语音合成)第二类人与机器之间的通信:语音识别人讲话,机器接…

    2022年5月26日
    39
  • import上一级目录的模块(Python)

    import上一级目录的模块(Python)

    2020年11月8日
    238

发表回复

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

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