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中所有数据元素的数据类型。
备注:能用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中astype用法_浅谈python 中的 type(), dtype(), astype()的区别相关推荐
- python中rstrip用法_浅谈Python3中strip()、lstrip()、rstrip()用法详解
简单来说,三种方法是为了删除字符串中不同位置的指定字符.其中,strip()用于去除字符串的首尾字符,同理,lstrip()用于去除左边的字符,rstrip()用于去除右边的字符 Python中有三个 ...
- python读取图像数据流_浅谈TensorFlow中读取图像数据的三种方式
本文面对三种常常遇到的情况,总结三种读取数据的方式,分别用于处理单张图片.大量图片,和TFRecorder读取方式.并且还补充了功能相近的tf函数. 1.处理单张图片 我们训练完模型之后,常常要用图片 ...
- python sys模块作用_浅谈Python中的模块
模块 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很多编程语言都采用这种组织代码的方式.在Python中,一个.py文件就称之为一个模块(Mod ...
- python 中arange函数_浅谈Python中range与Numpy中arange的比较
本文先比较range与arange的异同点,再详细介绍各自的用法,然后列举了几个简单的示例,最后对xrange进行了简单的说明. 1. range与arange的比较 (1)相同点:A.参数的可选性. ...
- python中怎么计数_浅谈python中统计计数的几种方法和Counter详解
1) 使用字典dict() 循环遍历出一个可迭代对象中的元素,如果字典没有该元素,那么就让该元素作为字典的键,并将该键赋值为1,如果存在就将该元素对应的值加1. lists = ['a','a','b ...
- python方法解析顺序_浅谈Python的方法解析顺序(MRO)
方法解析顺序, Method Resolution Order 从一段代码开始 考虑下面的情况: class A(object): def foo(self): print('A.foo()') cl ...
- 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 ...
最新文章
- Go中协程间通信的方式Sync.Cond
- Alamofire 的使用
- ruby打印_Ruby程序打印一个数字的乘法表
- 微软首款 Office 应用程序登陆 Linux
- De 30: Decoupling Linear Systems with Constant Coefficients
- 生成XML文件的步骤 解析XML文件
- 区块链宠物移动端交互原型模板、免费领取、宠物交易、宠物领养、宠物购买、宠物集市、用户中心、注册登录、订单管理、常用元件、通用元件、设计框架、规则说明、功能流程、界面流程、规则模板、Axure原型、rp
- X509证书基本概念
- 使用YYLabel小结,供参考
- 生活记录:给枯燥的心灵浇浇水
- 明翰英语教学系列之名词篇
- 数据分析(商业数据分析师理论认知之二商业分析师入门指南)
- ISO 8583协议-银行交易的标准
- 文字生成图片html,根据文字生成图片
- 用python写爬虫(一)初识爬虫
- 推荐一个开源库(Calligraphy)将ttf字体库加入到项目中,轻松实现各种字体
- 洛谷P3853 [TJOI2007]路标设置
- 人脸清晰化神器codeFormer图形界面包GUI
- Fluent 时间步长【转载】
- 拼多多开启“国际直通车”计划,要帮用户提升生活品质
热门文章
- 陕西师范大学计算机学院课表,陕西师范大学数学与信息科学学院课程表一.DOC...
- 批量替换任意文件扩展名(格式后缀),如ppt变pptx,docx变doc
- 【C语言小题】用C语言实现sign符号函数
- ORIN刷机教程——保姆版(只有一句废话)
- dumpsys 概述
- Android 滚动的公告栏
- 三维空间中三角面片碰撞检测TTI
- tars框架 php,tars框架安装
- php 扩展函数openssl_decrypt 使用注意事项
- vulkan_日式卡通渲染(NPR)实现