python 数据处理中各种存储方式里数据类型的转换

Posted on 2016-12-16 15:56  三釜山  阅读(20751)  评论(0编辑  收藏  举报

自己记录,仅供参考

在数据处理时经常会遇到数据类型不匹配的事情,为了方便查看各种存储方式中数据类型的改变。我把一些自己常用的整理方式记录下来,希望可以为以后数据类型的处理工作提供便利。

数据常用的基本处理类型

1、字符串 2、布尔类型 3、整数 4、浮点数 5、日期

 

(1)单个变量的数据类型转换及查看

单个变量的类型查看

1 In [82]: %paste
2 a='2'
3 type(a)
4 
5 ## -- End pasted text --
6 Out[82]: str

单个变量的类型转换

数值转字符串

1 In [84]: a='2'
2     ...: type(a)
3     ...:
4 Out[84]: str
5 
6 In [85]: a=float(a)
7     ...: type(a)
8     ...:
9 Out[85]: float

字符串转日期及日期运算结果转为数字

 1 In [96]: from datetime import timedelta
 2     ...: import time,datetime
 3     ...: changetime1='2016-12-11'
 4     ...: changetime2='2016-12-14'
 5     ...: t1= time.strptime(changetime1,"%Y-%m-%d")
 6     ...: y,m,d = t1[0:3]
 7     ...: change1=datetime.date(y,m,d)
 8     ...: t2= time.strptime(changetime2,"%Y-%m-%d")
 9     ...: y,m,d = t2[0:3]
10     ...: change2=datetime.date(y,m,d)#转成日期格式
11     ...: tianshu=(change2-change1).days#转成数值格式
12     ...: print(type(changetime1))
13     ...: print(type(change1))
14     ...: print(type(change2-change1))
15     ...: print(type(tianshu))
16     ...:
17 <class 'str'>
18 <class 'datetime.date'>
19 <class 'datetime.timedelta'>
20 <class 'int'>


(2) numpy内数据类型的转变及查看

numpy类型

类型 类型代码 说明
int8,uint8 i1,u1 有符号和无符号的8位(1个字节)整型
int16,uint16 i2,u2 有符号和无符号的16位(2个字节)整型
int32,uint32 i4,u4 有符号和无符号的32位(4个字节)整型
int64,uint64 i8,u8 有符号和无符号的64位(8个字节)整型
float16 f2 半精度浮点数
float32 f4或f 标准的单精度浮点数。与c的float兼容
float64 f8或d 标准的双精度浮点数。与c的double和python的float对象兼容
float128 f16或g 扩展精度浮点数
complex64,complex128 c8,c16 分别用两个32位,64位或128位浮点数表示的
complex256 c32 复数
bool ? 存储true和false值的布尔类型
obiect O python对象类型
string S 固定长度的字符串类型(每个字符1个字节)
unicode U 固定长度的uincode类型(字数由平台决定)跟字符创的定义方式一样(如U10)

 

查看numpy的数据类型

1 In [99]: import numpy as np
2     ...: arr=np.array([1,2,3,4,5])
3     ...: arr.dtype#查看数据类型
4     ...:
5 Out[99]: dtype('int32')
1 In [100]: arr1=np.array(['1','2','3','4','5'])
2      ...: arr1.dtype
3      ...:
4 Out[100]: dtype('<U1')#unicode类型

通过ndaary的astype方式显示地转换dtype

1 In [101]: arr1=np.array(['1','2','3','4','5'])
2      ...: print(arr1.dtype)
3      ...: arr2=arr1.astype(int)
4      ...: print(arr2.dtype)
5      ...:
6 <U1
7 int32

In [102]: arr2
Out[102]: array([1, 2, 3, 4, 5])#将unicode类型转为int32

#转为字符串
arr3=arr2.astype(np.str)
print(arr3.dtype)

## -- End pasted text --
<U11


(3)dataframe内数据类型的查看及更改

查看dataframe的数据类型

 1 In [110]: %paste
 2 import numpy as np
 3 import pandas as pd
 4 from pandas import Series,DataFrame
 5 data={'state':['ni','hao','a'],
 6 'year':[2111,3232,4546],
 7 'age':['23','24','25']}
 8 frame=DataFrame(data)
 9 frame.dtypes#查看数据类型
10 
11 ## -- End pasted text --
12 Out[110]:
13 age      object
14 state    object
15 year      int64
16 dtype: object

更改dataframe的数据类型

 1 In [111]: %paste
 2 import numpy as np
 3 import pandas as pd
 4 from pandas import Series,DataFrame
 5 data={'state':['ni','hao','a'],
 6 'year':[2111,3232,4546],
 7 'age':['23','24','25']}
 8 frame=DataFrame(data)
 9 frame['age']=frame['age'].astype('int')#将age转为int类型并替换原来的数据
10 frame.dtypes
11 
12 ## -- End pasted text --
13 Out[111]:
14 age       int32
15 state    object
16 year      int64
17 dtype: object

 

Copyright © 2024 三釜山
Powered by .NET 8.0 on Kubernetes