Myk*_*tko 13 python numeric dataframe pandas dtype
如何优化数据帧内存占用并找到数字列的最佳(最小)数据类型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
Run Code Online (Sandbox Code Playgroud)
预期结果:
>>> df.dtypes
A int8
B int32
C float32
D float32
dtype: object
Run Code Online (Sandbox Code Playgroud)
jez*_*ael 23
您可以使用参数downcastinto_numeric来选择整数并按 浮动列DataFrame.select_dtypes,它可以像@anurag 提到的那样在 pandas 中工作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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9254 次 |
| 最近记录: |