[英]How to downcast numeric columns in Pandas?
How to optimize the data frame memory footprint and find the most optimal (minimal) data types dtypes
for numeric columns.如何优化数据框 memory 占用空间并找到数字列的最佳(最小)数据类型
dtypes
。 For example:例如:
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
Expected result:预期结果:
>>> df.dtypes
A int8
B int32
C float32
D float32
dtype: object
You can use parameter dowcast
in to_numeric
with selectig integers and floats columns by DataFrame.select_dtypes
, it working from pandas 0.19+
like mentioned @anurag, thank you:您可以通过
dowcast
在to_numeric
中使用带有 selectig 整数和浮点列的DataFrame.select_dtypes
,它从 pandas 0.19+
开始工作,就像提到的@anurag,谢谢:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.