python中astype用法_浅谈python 中的 type(), dtype(), astype()的区别[通俗易懂]

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中所有数据元素的数据类型。备注…

大家好,又见面了,我是你们的朋友全栈君。

如下所示:

函数

说明

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博客。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/142685.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 关系数据模型的三个组成部分中不包括_数据模型常用组成部分

    关系数据模型的三个组成部分中不包括_数据模型常用组成部分关系数据模型的三个组成部分(1)关系数据模型的数据结构;(2)关系数据模型的操作集合;(3)关系数据模型的完整性约束;

    2025年6月28日
    4
  • leetcode-160. 相交链表(链表)

    leetcode-160. 相交链表(链表)给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。示例 1:输入: [2,3,-2,4]输出: 6解释: 子数组 [2,3] 有最大乘积 6。示例 2:输入: [-2,0,-1]输出: 0解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。/** * Definition for singly-linked list. * struct ListNode { * int val; *

    2022年8月11日
    3
  • Java 二维数组初始化「建议收藏」

    Java 二维数组初始化「建议收藏」1.在定义时初始化。double[][]a=newdouble[][]{{1,2,3},{4,2,7}};double[][]b=newdouble[][]{{3,3},{1,1},{2,2}};如图,a中的{1,2,3}即为第一行,{4,2,7}为第二行。2.先定空间,随后赋值。double[][]container=newdouble[3][4];fo…

    2022年5月26日
    36
  • mysql 添加索引 mysql 如何创建索引

    mysql 添加索引 mysql 如何创建索引1.添加PRIMARYKEY(主键索引)mysql>ALTERTABLE`table_name`ADDPRIMARYKEY(`column`)例:altertableyx_marketing_detailsaddindex(id);2.添加UNIQUE(唯一索引)mysql>ALTERTABLE`table_name`ADDUNIQUE(`column`)3.添加INDEX(普通索引)mysql>ALTERTABLE…

    2022年6月2日
    32
  • springboot集成日志

    springboot集成日志一、通过代码引入slf4j因为pom文件中已经通过父类引入了log4j查看中有直接进行引入即可。// 在类中引入,MyController是本类名 private static final Logger log = LoggerFactory.getLogger(MyController.class);下面直接使用即可。显示结果:默认情况下是info…

    2022年6月13日
    29
  • 3.3转1.8V(电平转换)_i2c电平转换芯片国产

    3.3转1.8V(电平转换)_i2c电平转换芯片国产利用TI电平钳制芯片SN74TVC16222A来做电平转换,优点在于速率可以做得快一些。

    2022年8月10日
    9

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号