pandas 数据类型 dtypes |
您所在的位置:网站首页 › 简述数据交易平台的类型有哪些 › pandas 数据类型 dtypes |
本内容来自:https://gairuo.com
看过来 《pandas 教程》 持续更新中,提供建议、纠错、催更等加作者微信: gairuo123(备注:pandas教程)和关注公众号「盖若」ID: gairuo。跟作者学习,请进入 Python学习课程。欢迎关注作者出版的书籍:《深入浅出Pandas》 和 《Python之光》。 本内容来自:https://gairuo.compandas 的数据类型是指某一列的里所有的数据的共性,如果全是数字那么就是类型数字型,其中一个不是数据那么就没法是数字型了。我们知道 pandas 里的一列可以由 NumPy 数组组成,事实上大多 NumPy 的数据类型就是 pandas 的类型,pandas 也会有自己特有的数据类型。 数据的类型主要的数据类型有: float int bool datetime64[ns] datetime64[ns, tz] timedelta64[ns] timedelta[ns] category object string 新功能,还在实验阶段默认的数字类型是 int64 和 float64,文字类型是 object。 和 python、NumPy 类型的对应关系: Pandas 类型 Python 类型 NumPy类型 使用场景 object str or mixed string_, unicode_, mixed types 文本或者混合数字 int64 int int_, int8, int16, int32, int64, uint8, uint16, uint32, uint64 整型数字 float64 float float_, float16, float32, float64 浮点数字 bool bool bool_ True/False 布尔型 datetime64[ns] nan datetime64[ns] 日期时间 timedelta[ns] nan nan 两个时间之间的距离,时间差 category nan nan 有限文本值,枚举object 类型是 Python 的对象,它可以是任何数据类类型,比如字符、列表、函数、json 等等,甚至可以是 Series、DataFrame。 关于 Numpy 的数据类型介绍可以查看:NumPy 的数据类型。 类型的查看 df.dtypes # 各字段的数据类型 df.team.dtype # 某个字段的类型 s.dtype # S 的类型 df.dtypes.value_counts() # 各类型有多少个字段也可以实现按数据类型筛选,只看某些类型的数据。 常用 Pandas 特有类型 DatetimeTZDtype带有时区的日期时间格式。 # 时区为北京时间,单位支持纳秒 d = pd.DatetimeTZDtype("ns", tz='Asia/Shanghai') pd.Series(['20200501 22:23:22.3432'], dtype=d) # 0 2020-05-01 22:23:22.343200+08:00 # dtype: datetime64[ns, Asia/Shanghai]也可以用字符串去指定类型,此字符串可用在所有指定数据类型的地方,所有数据类型道理一样。 pd.Series(['20200501 22:23:22.3432'], dtype='datetime64[ns, Asia/Shanghai]') pd.Series(['20200501 22:23:22.3432'], dtype='datetime64[ns]') # 无时区如果需要指定一个时间定值,可以用 pd.Timestamp(): # 用字符形式 pd.Timestamp('2017-01-01T12') # Timestamp('2017-01-01 12:00:00') # Unix epoch 指定时间单和时区 pd.Timestamp(1513393355.5, unit='s') # Timestamp('2017-12-16 03:02:35.500000') pd.Timestamp(1513393355, unit='s', tz='US/Pacific') # Timestamp('2017-12-15 19:02:35-0800', tz='US/Pacific') # 用 datetime.datetime 的方法 pd.Timestamp(2017, 1, 1, 12) # Timestamp('2017-01-01 12:00:00') pd.Timestamp(year=2017, month=1, day=1, hour=12) # Timestamp('2017-01-01 12:00:00')可参考教程关于时间的专门介绍。 CategoricalDtype定义一个有限的字符枚举类型: t = pd.CategoricalDtype(categories=['b', 'a'], ordered=True) pd.Series(['a', 'b', 'a', 'c'], dtype=t) # 'c' 不在列表是值会为 NaN ''' 0 a 1 b 2 a 3 NaN dtype: category Categories (2, object): [b < a] ''' pd.Categorical([1, 2, 3, 1, 2, 3]) # [1, 2, 3, 1, 2, 3] # Categories (3, int64): [1, 2, 3] pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c']) # [a, b, c, a, b, c] # Categories (3, object): [a, b, c] c = pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c'], ordered=True, categories=['c', 'b', 'a']) c # [a, b, c, a, b, c] # Categories (3, object): [c < b < a] c.min() # 'c' PeriodDtype支持时间周期 pd.PeriodDtype(freq='D') # 按天 # period[D] pd.PeriodDtype(freq=pd.offsets.MonthEnd()) # 按月,最后一天 # period[M] StringDtype警告 pd.StringDtype 和 pd.arrays.StringArray 是 pandas 1.0 版本新增加的实验性功能,可能在下个版本不兼容,可以尝试试用,不过从官方的态度来看还是极力推荐的。 支持字符类型数据。 pd.Series(['a','b','c'], dtype=pd.StringDtype()) ''' 0 a 1 b 2 c dtype: string '''可参考教程关于文本数据 处理的专门介绍。 数据类型的检测可以使用类型测试数据的类型: pd.api.types.is_bool_dtype(s) pd.api.types.is_categorical_dtype(s) pd.api.types.is_datetime64_any_dtype(s) pd.api.types.is_datetime64_ns_dtype(s) pd.api.types.is_datetime64_dtype(s) pd.api.types.is_float_dtype(s) pd.api.types.is_int64_dtype(s) pd.api.types.is_numeric_dtype(s) pd.api.types.is_object_dtype(s) pd.api.types.is_string_dtype(s) pd.api.types.is_timedelta64_dtype(s) pd.api.types.is_bool_dtype(s) 类型的转换教程后边会有数据类型转换。 相关内容 pandas 根据列选择非数字类型行 2022-08-25 18:50:54 数据框 DataFrame pandas 教程 pandas 功能介绍 >更新时间:2022-02-12 13:44:33 标签:pandas 数据类型 dtypes |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |