介绍
小编这次要给大家分享的是python中()的类型,dtype (), astype()有什么区别,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。
<>强如下所示:强>
函数说明类型()返回数据结构类型(列表、dict numpy。ndarray等)dtype ()返回数据元素的数据类型(整数、浮点数等)
备注:1)由于列表,dict等可以包含不同的数据类型,因此不可调用dtype()函数
2) np。数组中要求所有元素属于同一数据类型,因此可调用dtype()函数
astype ()改变np.array中所有数据元素的数据类型。
备注:能用dtype()才能用astype ()
<强>测试代码:强>
进口numpy np 类Myclass (): 通过=[[1,2,3],[4、5、6]] b={& # 39;一个# 39;:1 & # 39;b # 39;: 2 & # 39; c # 39;: 3} c=np.array ([1, 2, 3]) d=Myclass () e=np.linspace (1、5、10) c_=c.astype (np.float) f=10 print(“类型(a)=?类型(a)) print(“类型(b)=?类型(b)) print(“类型(c)=?类型(c)) print(“类型(d)=?类型(d)) print(“类型(e)=?类型(e)) print(“类型(f)=?类型(f)) print (“(c_)=袄嘈?类型(c_)) #打印(a.dtype) # # AttributeError: & # 39;列表# 39;对象没有属性& # 39;dtype& # 39; #打印(b.dtype) # # AttributeError: & # 39; dict # 39;对象没有属性& # 39;dtype& # 39; 打印(c.dtype) #打印(d.dtype) # # AttributeError: & # 39; myclass # 39;对象没有属性& # 39;dtype& # 39; 打印(e.dtype) 打印(c_.dtype) #打印(f.dtype) # # AttributeError: & # 39; int # 39;对象没有属性& # 39;dtype& # 39; #打印(a.astype (np.int)) # # AttributeError: & # 39;列表# 39;对象没有属性& # 39;astype& # 39; #打印(b.astype (np.int)) # # AttributeError: & # 39; dict # 39;对象没有属性& # 39;astype& # 39; print (c.astype (np.int)) #打印(d.astype (np.int)) # # AttributeError: & # 39; myclass # 39;对象没有属性& # 39;astype& # 39; print (e.astype (np.int)) #打印(f.astype (np.int)) # # AttributeError: & # 39; int # 39;对象没有属性& # 39;astype& # 39;
<强>熊猫astype()错误强>
由于数据出现错误
DataError:没有数值类型总
引用>改正以后才认识到astype的重要性。
Top15[& # 39;人口# 39;]=Top15[& # 39;能源供应# 39;].div (Top15[& # 39;每人均能源供应# 39;]).astype(浮动)
引用>
df_mean=((df.set_index(& # 39;大陆# 39;).groupby(要求等级=0)(& # 39;人口# 39;].agg({& # 39;意味着# 39;:np.mean})))
#加了astype(浮动)后无错误python中()的类型,dtype (), astype()有什么区别