Python-辨析type/dtype/astype用法
Python,辨析,type,dtype,astype,用法
2025-03-28 09:01:07 时间
大家好,又见面了,我是你们的朋友全栈君。
Python中与数据类型相关函数及属性有如下三个:type/dtype/astype。
名称 | 描述 |
---|---|
type() | 返回参数的数据类型 |
dtype | 返回数组中元素的数据类型 |
astype() | 对数据类型进行转换 |
- type()用于获取数据类型
#type用于获取数据类型
import numpy as np
a=[1,2,3]
print(type(a))
#>>><class 'list'>
b=np.array(a)
print(type(b))
#>>><class 'numpy.ndarray'>
- dtype用于获取数组中元素的类型
#dtype用于获取数组中元素的类型
print(b.dtype)
#>>>int64
x,y,z=1,2,3
c=np.array([x,y,z])
print(c.dtype)
#>>>int64
d=np.array([1+2j,2+3j,3+4j])
print(d.dtype)
#>>>complex128
- astype()修改数据类型 Array中用法
#astype修改数据类型
e=np.linspace(1,5,20)
print(e)
#>>>
''' [1. 1.21052632 1.42105263 1.63157895 1.84210526 2.05263158 2.26315789 2.47368421 2.68421053 2.89473684 3.10526316 3.31578947 3.52631579 3.73684211 3.94736842 4.15789474 4.36842105 4.57894737 4.78947368 5. ] '''
print(e.dtype)
#>>>float64
e=e.astype(int)
print(e)
#>>>[1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 5]
print(e.dtype)
#>>>int64
Dataframe中用法:转换指定字段数据的类型
import pandas as pd
e=np.linspace(1,5,20)
edf=pd.DataFrame(e)
edf[0].head(3)
#>>>
0 1.000000
1 1.210526
2 1.421053
Name: 0, dtype: float64
print(edf[0].dtype)
#>>>float64
edf[0]=edf[0].astype(int)
edf[0].head(3)
#>>>
0 1
1 1
2 1
Name: 0, dtype: int64
print(edf[0].dtype)
#>>>int64
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/139587.html原文链接:https://javaforall.cn
相关文章
- Python基础17-面向对象
- Python-基础04-数据类型
- Python 学生信息管理系统——文章中源码100%真实有效—–如何将类、初始化属性、模块、循环判断、静态方法等一系列知识点结合起来做一个项目「建议收藏」
- 干货 | OpenCV获取不规则区域的最大内切圆(附Python / C++ 源码)
- python break和continue
- python——正则表达式(re模块)详解
- Python 图_系列之纵横对比 Bellman-Ford 和 Dijkstra 最短路径算法
- python识别文字位置_如何利用Python识别图片中的文字
- 使用 setup.py 将 Python 库打包分发到 PyPI 踩坑指南
- python对随机森林分类结果绘制roc曲线
- mt4 python_一个使用Python自动化交易外汇MT4脚本实现「建议收藏」
- 人生苦短,python更短
- 使用Python验证并利用Redis未授权漏洞
- Python 树表查找_千树万树梨花开,忽如一夜春风来(二叉排序树、平衡二叉树)
- aic准则python_Python数据科学:线性回归
- jupyter和python的关系_jupyter notebook和python
- Python字符串与时间相互转换
- pythoncharm注释快捷键_jsp注释快捷键
- 用Python写一个百度POST实时推送工具
- Win10配置Airsim环境并设置Python通信