【1.2】Pandas的dtypes

一、Pandas所支持的数据类型:

  1. float
  2. int
  3. bool
  4. datetime64[ns]
  5. datetime64[ns, tz]
  6. timedelta[ns]
  7. category
  8. object

默认的数据类型是int64,float64.

二、查看数据类型

df.dtypes

df['column_name'].dtypes

series.dtype
get_dtype_counts() 

如果一列中含有多个类型,则该列的类型会是object,同样字符串类型的列也会被当成object类型. 不同的数据类型也会被当成object,比如int32,float32

三、通过列类型选取列

select_dtypes()

DataFrame.select_dtypes(include=None, exclude=None)

参数

include, exclude : list-like(传入想要查找的类型)

返回

subset : DataFrame

Raises

ValueError
TypeError

四、转换列类型

DataFrame.astype$Series.astype

Series.astype(dtype, copy=True, errors=’raise’, **kwargs) 
DataFrame.astype(dtype, copy=True, errors=’raise’, **kwargs)

参数

  1. dtype : data type, or dict of column name -> data type(传入列名和类型的字典)
  2. errors : {‘raise’, ‘ignore’}, default ‘raise’.(ignore,强制转换,这样不会报错,可以识别不同类型的数据)
  3. kwargs : keyword arguments to pass on to the constructor

返回

casted : type of caller
Index.astype

Index.astype(dtype, copy=True)

参数

dtype : numpy dtype or pandas type
copy : bool, default True

其他转换方法

to_numeric() (conversion to numeric dtypes) 
to_datetime() (conversion to datetime objects) 
to_timedelta() (conversion to timedelta objects)

参考资料

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn