pandas学习笔记—dataframe中的数据类型和相互转换

常见的dataframe中的数据类型包括以下:

Data type   Description
datetime64[ns] 日期时间数据类型
str     字符类型
object  一种通用的数据类型,在没有明确指定类型下,所有数据都可认为是object类型
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) astype()方法

改变其中一列数据类型:
举例,改变列名为"col2"列的数据类型

df['col2'] = df['col2'].astype('int') 

df['col2'] = df['col2'].astype('float64') 

改变整个dataframe的数据类型:
举例,改变名为df的dataframe内部所有数据类型

df.astype('int32')

(2) pd.to_XXX()方法

to_XXX()有以下种类:

to_numeric() #转化为数字型,根据情况转化为int或float
to_string() #转化为字符型
to_dict() #转化为字典,不能处理单列数据
to_timestamp() #转化为时间戳,以前还特意写了个函数转换,何必呢
to_datetime() #转化为datetime64[ns]
……            #其它的以后再补充

你可能感兴趣的:(信号处理,机器学习,数据科学,数据分析,大数据,数据挖掘,机器学习,人工智能)