浅谈python 中的 type(), dtype(), astype()的区别
作者:wzg2016 发布时间:2022-09-13 22:40:39
标签:python,type,dtype,astype
如下所示:
函数 | 说明 |
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)后无错误
来源:https://blog.csdn.net/Strive_For_Future/article/details/92811293
0
投稿
猜你喜欢
- 我们在微博上可以对图片进行向左转向右转等旋转操作,让用户可以从不同的视角欣赏图片效果。本文将结合实例为您讲解如何使用Javascript结合
- 本文实例为大家分享了JS实现canvas简单小画板的具体代码,供大家参考,具体内容如下Html部分:<!DOCTYPE html>
- 兼容当前HTML/XHTML文档是否有DTD声明:以下为程序代码:var xtop = document.documentElement.s
- 在许多人看来,HTML应该是WEB制作所有语言中最简单的语言,因为它不需要编译、封闭等,甚至只需要一个记事本就可以让其在浏览器中呈现出来。所
- 从BbsXp提出来的生肖函数Zodiac(birthday)。使用方法:birthday为把要判断的出生时间,如2008-3-24 20:0
- 如何制作一个文本文件编辑器?我们也来做一个:newdoc.asp<%@ Language=VBScript %&g
- assert断言声明,遇到错误则立即返回在使用python语言开发深度学习模型时,经常会遇到模型运行结束时才会发现的崩溃状态,或者得到的结果
- 阅读上一章:Chapter 4 引用互动性一直是互联网的重点,让使用者与网站能够交换信息,彼此沟通.表单使我们能够有组织的,使用同一方式的从
- 前言每种编程语言为了表现出色,并且实现卓越的性能,都需要有大量编译器级与解释器级的优化。由于字符串是任何编程语言中不可或缺的一个部分,因此,
- Memoization 是一种将函数返回值缓存起来的方法,在 Lisp, Ruby, Perl, Python 等语言中使用非常广泛。随着
- 阅读:Mootools常用方法扩展(三) 继续Mootools常用方法扩展,这次是Window类上的扩展,也就是全局函数。方法:$param
- 环境准备好了!我们怎么使用这些东东?IIS用组件初始化是用这个过程Public Sub OnStartPage给个使用asp组件的例子:数字
- 方法一: 代码如下:id=request.form("checkbox") id=Split(id,"
- 一、目录权限设置很重要:可以有效防范黑客上传木马文件. 如果通过 chmod 644 * -R 的话,php文件就没有权限访问了。 如果通过
- 我就废话不多说了,直接上代码吧:package mainimport ("flag""fmt"&qu
- 原理 采集程序实际上是通过了XML中的XMLHTTP组件调用其它网站上的网页。比如新闻采集程序,很多都是调用了sina的新闻网页,并且对其中
- 好久没有更新博客了,今天看到论坛上有位朋友问起全屏布局,有点像vc的界面。来了兴趣,就写了一个。运用IE6的怪异模式,通过绝对定位来实现的。
- 分页程序sub show_page参数说明:total_records 总记录数everypage_records 每页显示条数
- 对大家推荐很好使用的MySql节点系统,像让大家对MySql节点系统有所了解,然后对MySql节点系统全面讲解介绍,希望对大家有用在向大家详
- 例如:文本abcaBcabCaBCabcaBCa,关键字bc,在不区分大小写的情况,一共有6个匹配项。 则在网页中显示的是abcaBcabC