浅谈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


猜你喜欢
- <html><head><style>body{font-family:宋体;font-size:16p
- 参考: Smashing magzine翻译+整理: Demix当完成一项前端的工作之后,许多人都会忘记该项目的结构与细节。然而代码并不是马
- python 3.x版本print输出不换行的格式如下:print(x, end="")其中,end=&quo
- 从接触Python以来,一直都是采用 virtualenv 和 virtualenvwrapper 来管理不同项目的依赖环境,通过 work
- Golang开发环境搭建Go 语言开发包国外:https://golang.org/dl/国内(推荐): https://golang.go
- 字符串打印打印函数echo: 打印值,用于单值print_r(): 人类可读方式打印,用于数组var_dump():打印结构和类型,一般用于
- # django manage.py扩展自定义命令环境: mac django1.10.3在实际的项目开发过程中,我们可能要执行某脚本初始化
- 1、说明xlwt模块是非追加写入.xls模块,所以要一次性写入for循环和列表,这样就没有追加和非追加的说法。并且将Excel表合并,将每一
- 下面是一段产生log-normal分布的代码,以此进行说明。clear all;clc;for t=1:100 Traffic(t) =cu
- 1. Series相当于数组numpy.array类似s1=pd.Series([1,2,4,6,7,2])s2=pd.Series([4,
- 1.使用测量工具,量化性能才能改进性能,常用的timeit和memory_profiler,此外还有profile、cProfile、hot
- 遍历字典: keys() 、values() 、items()1. xxx.keys() : 返回字典的所有的key 返回一个序列,序列中保
- 如下:re.split(pattern, string, [maxsplit], [flags])pattern:表示模式字符串,由要匹配的
- 最初开发的 Matplotlib,仅支持绘制 2d 图形,后来随着版本的不断更新, Matplotlib 在二维绘图的基础上,构建了一部分较
- pyinstaller打包问题简单介绍一下pyinstaller常用的参数:可选参数示例说明-Fpyinstaller -F demo.py
- 本文实例讲解了javascript实现无缝上下滚动的代码,分享给大家供大家参考,具体内容如下js实现上下无缝滚动的原理是这样的:1、首先给容
- 1、pyecharts绘制饼图(显示百分比)# 导入模块from pyecharts import options as optsfrom
- 【OpenCV】⚠️高手勿入! 半小时学会基本操作⚠️边界填充概述OpenCV 是一个跨平台的计算机视觉库, 支持多语言, 功能强大. 今天
- 在ASP中,除了ADODB、Scripting 等一些常用组件外,我们还可以用微软的ActiveX方法来轻松捕获哟: <%u
- 如果我们的语句为:IF NOT EXISTS(SELECT 1 FROM sys.views WHERE name='Report_