浅谈python 中的 type(), dtype(), astype()的区别


Posted in Python onApril 09, 2020

如下所示:

函数 说明
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 相关文章推荐
Python中的ctime()方法使用教程
May 22 Python
基于tensorflow加载部分层的方法
Jul 26 Python
用pycharm开发django项目示例代码
Jun 13 Python
python每天定时运行某程序代码
Aug 16 Python
基于python的BP神经网络及异或实现过程解析
Sep 30 Python
Python:二维列表下标互换方式(矩阵转置)
Dec 02 Python
Python合并2个字典成1个新字典的方法(9种)
Dec 19 Python
python字符串替换re.sub()实例解析
Feb 09 Python
浅谈优化Django ORM中的性能问题
Jul 09 Python
Python使用pickle进行序列化和反序列化的示例代码
Sep 22 Python
用ldap作为django后端用户登录验证的实现
Dec 07 Python
浅谈Python中对象是如何被调用的
Apr 06 Python
利用python绘制数据曲线图的实现
Apr 09 #Python
利用python生成照片墙的示例代码
Apr 09 #Python
Python 改变数组类型为uint8的实现
Apr 09 #Python
pandas 强制类型转换 df.astype实例
Apr 09 #Python
python 给图像添加透明度(alpha通道)
Apr 09 #Python
Python日志logging模块功能与用法详解
Apr 09 #Python
解决python DataFrame 打印结果不换行问题
Apr 09 #Python
You might like
ThinkPHP3.2.2的插件控制器功能简述
2014/07/09 PHP
php常用hash加密函数
2014/11/22 PHP
学习php设计模式 php实现模板方法模式
2015/12/08 PHP
PHP设计模式之适配器模式定义与用法详解
2018/04/03 PHP
php ajax数据传输和响应方法
2018/08/21 PHP
IE8 chrome中table隔行换色解决办法
2010/07/09 Javascript
jquery获得下拉框值的代码
2011/08/13 Javascript
利用JS来控制键盘的上下左右键(示例代码)
2013/12/14 Javascript
js中文逗号转英文实现
2014/02/11 Javascript
动态载入js提高网页打开速度的方法
2014/07/04 Javascript
jQuery Validate表单验证入门学习
2015/12/18 Javascript
基于jQuery实现点击最后一行实现行自增效果的表格
2016/01/12 Javascript
JS判断输入字符串长度实例代码(汉字算两个字符,字母数字算一个)
2016/08/02 Javascript
bootstrap多种样式进度条展示
2016/12/20 Javascript
通过js控制时间,一秒一秒自己动的实例
2017/10/25 Javascript
关于axios如何全局注册浅析
2018/01/14 Javascript
swiper动态改变滑动内容的实现方法
2018/01/17 Javascript
Vue iview-admin框架二级菜单改为三级菜单的方法
2018/07/03 Javascript
解决angularjs service中依赖注入$scope报错的问题
2018/10/02 Javascript
实例分析javascript中的异步
2020/06/02 Javascript
jQuery带控制按钮轮播图插件
2020/07/31 jQuery
[41:56]Spirit vs Liquid Supermajor小组赛A组 BO3 第一场 6.2
2018/06/03 DOTA
python使用socket远程连接错误处理方法
2015/04/29 Python
python清除字符串里非数字字符的方法
2015/07/02 Python
玩转python selenium鼠标键盘操作(ActionChains)
2020/04/12 Python
Python编程实现从字典中提取子集的方法分析
2018/02/09 Python
对pandas中to_dict的用法详解
2018/06/05 Python
在Python中合并字典模块ChainMap的隐藏坑【推荐】
2019/06/27 Python
python lambda表达式在sort函数中的使用详解
2019/08/28 Python
Virtualenv 搭建 Py项目运行环境的教程详解
2020/06/22 Python
如何利用Python写个坦克大战
2020/11/18 Python
实习求职信
2013/12/01 职场文书
大学生咖啡店创业计划书
2014/01/21 职场文书
荷叶圆圆教学反思
2014/02/01 职场文书
高中课程设置方案
2014/05/28 职场文书
2019入党申请书格式和范文
2019/06/25 职场文书