我正在开发一个python模块,它允许用户从parquet文件中读取略多于1M行x 372列到内存中,以便人们像这样执行分析:
data = pandas.read_parquet(url1 + str(year) + url2, columns=columns, engine='fastparquet')
我试图通过转换一些数据类型来主动减少数据大小,例如。对象到类别,int64到int32等,通过执行以下操作:
for column in test.select_dtypes(include=['object']):
if len(test[column].unique()) < 100:
test[column] = test[column].astype('category')
for column in test.select_dtypes(include=['int64']):
test[column] = test[column].astype('int32')
for column in test.select_dtypes(include=['float64']):
test[column] = test[column].astype('float32')
这样可以将数据大小减少约50%,但速度很慢(完全转换需要约3分钟,而初始数据导入只需1分钟)。有没有其他方法可以让它跑得更快?蒂娅
2条答案
按热度按时间5kgi1eie1#
不要使用pandas方法进行转换,尝试使用numpy数组,这要快得多。举例来说:
检查numpy文档中的不同数据类型:https://numpy.org/doc/stable/reference/arrays.dtypes.html
dojqjjoe2#
对我来说,在设置之前删除列,它显著加快了时间,例如: