python dtype什么意思_浅谈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)后无错误
以上这篇浅谈python 中的 type(), dtype(), astype()的区别就是小编分享给大家的全部内容了,希望能给大家一个参考
您可能感兴趣的文章:
python dtype什么意思_浅谈python 中的 type(), dtype(), astype()的区别相关推荐
- python中dtype什么意思_浅谈python 中的 type(), dtype(), astype()的区别
如下所示: 函数 说明 type() 返回数据结构类型(list.dict.numpy.ndarray 等) dtype() 返回数据元素的数据类型(int.float等) 备注:1)由于 list. ...
- python读取图像数据流_浅谈TensorFlow中读取图像数据的三种方式
本文面对三种常常遇到的情况,总结三种读取数据的方式,分别用于处理单张图片.大量图片,和TFRecorder读取方式.并且还补充了功能相近的tf函数. 1.处理单张图片 我们训练完模型之后,常常要用图片 ...
- python sys模块作用_浅谈Python中的模块
模块 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很多编程语言都采用这种组织代码的方式.在Python中,一个.py文件就称之为一个模块(Mod ...
- python中文字符串编码_浅谈python下含中文字符串正则表达式的编码问题
前言 Python文件默认的编码格式是ascii ,无法识别汉字,因为ascii码中没有中文. 所以py文件中要写中文字符时,一般在开头加 # -*- coding: utf-8 -*- 或者 #co ...
- python 共享内存变量_浅谈python多进程共享变量Value的使用tips
前言: 在使用tornado的多进程时,需要多个进程共享一个状态变量,于是考虑使用multiprocessing.Value(对于该变量的具体细节请查阅相关资料).在根据网上资料使用Value时,由于 ...
- python数据类型转换原因_浅谈Python数据类型之间的转换
Python数据类型之间的转换 函数 描述 int(x [,base]) 将x转换为一个整数 long(x [,base] ) 将x转换为一个长整数 float(x) 将x转换到一个浮点数 compl ...
- python采用函数编程模式_浅谈Python 函数式编程
匿名函数lambda表达式 什么是匿名函数? 匿名函数,顾名思义就是没有名字的函数,在程序中不用使用 def 进行定义,可以直接使用 lambda 关键字编写简单的代码逻辑.lambda 本质上是一个 ...
- python打开方式错误_浅谈python 调用open()打开文件时路径出错的原因
昨晚搞鼓了一下python的open()打开文件 代码如下 def main(): infile =open("C:\Users\Spirit\Desktop\bc.txt",'r ...
- python方法解析顺序_浅谈Python的方法解析顺序(MRO)
方法解析顺序, Method Resolution Order 从一段代码开始 考虑下面的情况: class A(object): def foo(self): print('A.foo()') cl ...
最新文章
- TensorRT 7.2.1 开发概要(下)
- IPad开发之有帮助的开发工具
- 【Matlab 控制】求左右特征向量
- Python的深copy和浅copy
- C#语法:委托与事件
- Opencv 中cv开头的函数和没有cv的区别,例如cvWaitkey()和waitKey()的区别
- linux下c 调用python,在Linux下使用python ctypes调用io\u submit
- Maven仓库—Nexus环境搭建及使用
- 9个Console命令
- 获取到的数组在webview中成了字符串
- 十大排序算法——计数排序(C语言)
- 方正台式计算机保护卡密码忘记了,方正电脑E系列忘记还原卡密码处理方法
- 产品配件类目税目分类_HS编码知识:汽车零部件怎么归类?
- 使用管理员权限强制删除文件夹
- 春节假期 | 最强抢票攻略
- Kubernetes 学习总结(29)—— 使用 kubeadm 部署 Kubernetes 1.24 详细步骤总结
- Java8增加功能--Effectively final 功能
- 集线器、路由器、网桥(桥接器)、网关、网线、交换机、中继器(转发器)、网卡工作在哪一层
- 任意用户注册任意用户密码修改
- RGB color model