如何解决如何向下转换 Pandas 中的数字列?
如何优化数据帧内存占用并为数值列找到最佳(最小)数据类型 dtypes
。例如:
A B C D
0 1 1000000 1.1 1.111111
1 2 -1000000 2.1 2.111111
>>> df.dtypes
A int64
B int64
C float64
D float64
预期结果:
>>> df.dtypes
A int8
B int32
C float32
D float32
dtype: object
解决方法
您可以在 to_numeric
中使用参数 dowcast
和 DataFrame.select_dtypes
选择的整数和浮点数列,它可以像提到的@anurag 一样从大熊猫 0.19+
中工作,谢谢:>
fcols = df.select_dtypes('float').columns
icols = df.select_dtypes('integer').columns
df[fcols] = df[fcols].apply(pd.to_numeric,downcast='float')
df[icols] = df[icols].apply(pd.to_numeric,downcast='integer')
print (df.dtypes)
A int8
B int32
C float32
D float32
dtype: object
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。