翼度科技»论坛 编程开发 python 查看内容

数据分析神器Pandas快速入门3类型转换

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
序列类型转换


3.1 自动转换

在pandas 1.0 中,引入了一种新的转换方法.convert_dtypes。它会尝试将Series 换为支持 pd.NA 类型。以city_mpg 系列为例,它将把类型从int64转换为Int64:
  1. >>> city_mpg.convert_dtypes()
  2. 0        19
  3. 1         9
  4. 2        23
  5. 3        10
  6. 4        17
  7.          ..
  8. 41139    19
  9. 41140    20
  10. 41141    18
  11. 41142    18
  12. 41143    16
  13. Name: city08, Length: 41144, dtype: Int64
  14. >>> city_mpg.astype('Int16')
  15. 0        19
  16. 1         9
  17. 2        23
  18. 3        10
  19. 4        17
  20.          ..
  21. 41139    19
  22. 41140    20
  23. 41141    18
  24. 41142    18
  25. 41143    16
  26. Name: city08, Length: 41144, dtype: Int16
  27. >>> city_mpg.astype('Int8')
  28. Traceback (most recent call last):
  29. ...
复制代码
要指定系列数据的类型,可以尝试使用.astype 方法。我们的城市里程可以保存为16位整数,但8位整数就不行了,因为该符号类型的最大值是127,而我们有一些汽车的值是150。使用更窄的类型,就能减少内存使用,从而有更多内存处理更多数据。可以使用NumPy来检查整数和浮点类型的限制:
  1. >>> np.iinfo('int64')
  2. iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)
  3. >>> np.iinfo('uint8')
  4. iinfo(min=0, max=255, dtype=uint8)
  5. >>> np.finfo('float16')
  6. finfo(resolution=0.001, min=-6.55040e+04, max=6.55040e+04, dtype=float16)
  7. >>> np.finfo('float64')
  8. finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64)
复制代码
参考资料

3.2 内存使用

要计算系列的内存使用情况,可以使用.nbytes属性或.memory_usage方法。后者在处理object类型时非常有用,因为可以通过deep=True来计算系列中 Python 对象的内存使用量。
  1. >>> city_mpg.nbytes
  2. 329152
  3. >>> city_mpg.astype('Int16').nbytes
  4. 123432
复制代码
autos的make列包含字符串,并作为对象存储。要获得包含字符串的内存量需要使用.memory_usage 方法:
  1. >>> make = df.make
  2. >>> make.memory_usage()
  3. KeyboardInterrupt
  4. >>> make.nbytes
  5. 329152
  6. >>> make.memory_usage()
  7. 329280
  8. >>> make.memory_usage(deep=True)
  9. 2606395
复制代码
.nbytes只是数据正在使用的内存,不含序列的辅助部分。.memory_usage包括索引内存,还可能包括object类型的等。
3.3 字符串和分类

如果向 .astype 方法传递str,它还可以将数字序列转换为字符串
  1. >>> city_mpg.astype(str)
  2. 0        19
  3. 1         9
  4. 2        23
  5. 3        10
  6. 4        17
  7.          ..
  8. 41139    19
  9. 41140    20
  10. 41141    18
  11. 41142    18
  12. 41143    16
  13. Name: city08, Length: 41144, dtype: object
  14. >>> city_mpg.astype(str)
  15. 0        19
  16. 1         9
  17. 2        23
  18. 3        10
  19. 4        17
  20.          ..
  21. 41139    19
  22. 41140    20
  23. 41141    18
  24. 41142    18
  25. 41143    16
  26. Name: city08, Length: 41144, dtype: object
复制代码
分类序列对字符串数据非常有用,可以节省大量内存。这是因为当你有字符串数据时pandas会存储Python字符串。
当你将其转换为分类数据时,pandas不再为每个值使用Python字符串,而是对其进行优化,因此重复值不会重复。您仍然可以使用.str 属性的所有功能,但可能会节省大量内存(如果您有很多重复值)并提高性能,因为您不需要执行那么多字符串操作。
3.4 有序分类

要创建有序分类,需要定义自己的 CategoricalDtype:
  1. >>> values = pd.Series(sorted(set(city_mpg)))
  2. >>> city_type = pd.CategoricalDtype(categories=values,
  3. ...     ordered=True)
  4. >>> city_mpg.astype(city_type)
  5. 0        19
  6. 1         9
  7. 2        23
  8. 3        10
  9. 4        17
  10.          ..
  11. 41139    19
  12. 41140    20
  13. 41141    18
  14. 41142    18
  15. 41143    16
  16. Name: city08, Length: 41144, dtype: category
  17. Categories (105, int64): [6 < 7 < 8 < 9 ... 137 < 138 < 140 < 150]
复制代码
下表列出了可以传入 .astype 的类型。

3.5 其他类型

.to_numpy方法(或.values属性)返回NumPy数组,而.to_list返回Python列表。一般不要使用这些方法。如果直接使用 NumPy,有时会提高速度,但也有缺点。使用Python列表会大大降低代码速度。
如果你只想要单列的数据帧,你可以使用.to_frame 方法:
  1. >>> city_mpg.to_frame()
  2.        city08
  3. 0          19
  4. 1           9
  5. 2          23
  6. 3          10
  7. 4          17
  8. ...       ...
  9. 41139      19
  10. 41140      20
  11. 41141      18
  12. 41142      18
  13. 41143      16
  14. [41144 rows x 1 columns]
复制代码
此外,还有许多将数据导出为其他格式的转换方法,包括 CSV、Excel、HDF5、SQL、JSON 等。这些方法也存在于数据帧中,在序列中应用不多。
要转换为日期时间,请使用pandas中的 to_datetime 函数。如果要添加时区信息,则需要更多步骤。有关日期的章节将对此进行讨论。

来源:https://www.cnblogs.com/testing-/p/18276123
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具