python dataframe astype 字段类型转换

简介:

使用astype实现dataframe字段类型转换

# -*- coding: UTF-8 -*-
import pandas as pd
df = pd.DataFrame([{'col1':'a', 'col2':'1'}, {'col1':'b', 'col2':'2'}])

print df.dtypes

df['col2'] = df['col2'].astype('int')
print '-----------'
print df.dtypes

df['col2'] = df['col2'].astype('float64')
print '-----------'
print df.dtypes
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

输出结果:

col1    object
col2    object
dtype: object
-----------
col1    object
col2     int32
dtype: object
-----------
col1     object
col2    float64
dtype: object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

注:data type list

Data type   Description
bool_   Boolean (True or False) stored as a byte
int_    Default integer type (same as C long; normally either int64 or int32)
intc    Identical to C int (normally int32 or int64)
intp    Integer used for indexing (same as C ssize_t; normally either int32 or int64)
int8    Byte (-128 to 127)
int16   Integer (-32768 to 32767)
int32   Integer (-2147483648 to 2147483647)
int64   Integer (-9223372036854775808 to 9223372036854775807)
uint8   Unsigned integer (0 to 255)
uint16  Unsigned integer (0 to 65535)
uint32  Unsigned integer (0 to 4294967295)
uint64  Unsigned integer (0 to 18446744073709551615)
float_  Shorthand for float64.
float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa
float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa
complex_    Shorthand for complex128.
complex64   Complex number, represented by two 32-bit floats (real and imaginary components)
complex128  Complex number, represented by two 64-bit floats (real and imaginary components)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
转自:http://blog.csdn.net/chinacmt/article/details/52230339
目录
相关文章
|
5月前
|
Python
Python 字典转 dataframe
使用pandas将Python字典转换为DataFrame,需先确保安装pandas(`pip install pandas`)。
96 1
|
5月前
|
BI 数据处理 索引
Pandas基本操作:Series和DataFrame(Python)
Pandas基本操作:Series和DataFrame(Python)
321 1
|
5月前
|
数据采集 数据挖掘 Python
【Python DataFrame专栏】讲解DataFrame中缺失值的处理方法,包括填充、删除和插值技术。
【5月更文挑战第20天】在Python的Pandas库中处理DataFrame缺失值,包括查看缺失值(`isnull().sum()`)、填充(`fillna()`:固定值、前向填充、后向填充)、删除(`dropna()`:按行或列)和插值(`interpolate()`:线性、多项式、分段常数)。示例代码展示了这些方法的使用。
413 3
【Python DataFrame专栏】讲解DataFrame中缺失值的处理方法,包括填充、删除和插值技术。
|
2月前
【Pandas+Python】初始化一个全零的Dataframe
初始化一个100*3的0矩阵,变为Dataframe类型,并为每列赋值一个属性。
25 2
|
3月前
|
数据采集 机器学习/深度学习 数据可视化
了解数据科学面试中的Python数据分析重点,包括Pandas(DataFrame)、NumPy(ndarray)和Matplotlib(图表绘制)。
【7月更文挑战第5天】了解数据科学面试中的Python数据分析重点,包括Pandas(DataFrame)、NumPy(ndarray)和Matplotlib(图表绘制)。数据预处理涉及缺失值(dropna(), fillna())和异常值处理。使用describe()进行统计分析,通过Matplotlib和Seaborn绘图。回归和分类分析用到Scikit-learn,如LinearRegression和RandomForestClassifier。
70 3
|
4月前
|
Python
在Python的pandas库中,向DataFrame添加新列简单易行
【6月更文挑战第15天】在Python的pandas库中,向DataFrame添加新列简单易行。可通过直接赋值、使用Series或apply方法实现。例如,直接赋值可将列表或Series对象分配给新列;使用Series可基于现有列计算生成新列;apply方法则允许应用自定义函数到每一行或列来创建新列。
360 8
|
3月前
|
存储 数据可视化 数据处理
`geopandas`是一个开源项目,它为Python提供了地理空间数据处理的能力。它基于`pandas`库,并扩展了其对地理空间数据(如点、线、多边形等)的支持。`GeoDataFrame`是`geopandas`中的核心数据结构,它类似于`pandas`的`DataFrame`,但包含了一个额外的地理列(通常是`geometry`列),用于存储地理空间数据。
`geopandas`是一个开源项目,它为Python提供了地理空间数据处理的能力。它基于`pandas`库,并扩展了其对地理空间数据(如点、线、多边形等)的支持。`GeoDataFrame`是`geopandas`中的核心数据结构,它类似于`pandas`的`DataFrame`,但包含了一个额外的地理列(通常是`geometry`列),用于存储地理空间数据。
|
5月前
|
大数据 Python
【Python DataFrame专栏】DataFrame内存管理与优化:大型数据集处理技巧
【5月更文挑战第20天】本文介绍了使用Python的pandas库优化DataFrame内存管理的六个技巧:1) 查看DataFrame内存占用;2) 使用高效数据类型,如`category`和`int32`;3) 仅读取需要的列;4) 分块处理大数据集;5) 利用`inplace`参数节省内存;6) 使用`eval()`和`query()`进行快速筛选。这些方法有助于处理大型数据集时提高效率。
169 3
【Python DataFrame专栏】DataFrame内存管理与优化:大型数据集处理技巧
|
5月前
|
存储 数据挖掘 数据处理
【Python DataFrame 专栏】优化 DataFrame 性能:提升数据处理效率的秘诀
【5月更文挑战第19天】优化 Python DataFrame 性能的关键点包括:选择合适的数据类型以节省内存,避免重复计算,利用向量化操作,考虑使用 `iterrows` 或 `itertuples` 迭代,优化索引以及借助 `Cython` 或 `Numba` 加速代码执行。通过这些策略,能提升数据处理效率,应对大规模数据挑战。
213 2
【Python DataFrame 专栏】优化 DataFrame 性能:提升数据处理效率的秘诀
|
5月前
|
数据挖掘 数据处理 Python
【Python DataFrame 专栏】Python DataFrame 入门指南:从零开始构建数据表格
【5月更文挑战第19天】本文介绍了Python数据分析中的核心概念——DataFrame,通过导入`pandas`库创建并操作DataFrame。示例展示了如何构建数据字典并转换为DataFrame,以及进行数据选择、添加修改列、计算统计量、筛选和排序等操作。DataFrame适用于处理各种规模的表格数据,是数据分析的得力工具。掌握其基础和应用是数据分析之旅的重要起点。
53 2
【Python DataFrame 专栏】Python DataFrame 入门指南:从零开始构建数据表格
下一篇
无影云桌面