python分块读取大数据,避免内存不足的方法

yipeiwu_com7年前Python基础

如下所示:

def read_data(file_name):
 '''
 file_name:文件地址
 '''
 inputfile = open(file_name, 'rb') #可打开含有中文的地址
 data = pd.read_csv(inputfile, iterator=True)
 loop = True
 chunkSize = 1000 #一千行一块
 chunks = []
 while loop:
  try:
   chunk = dcs.get_chunk(chunkSize)
   chunks.append(chunk)
  except StopIteration:
   loop = False
   print("Iteration is stopped.")
 data = pd.concat(chunks, ignore_index=True)
 #print(train.head())
 return data

以上这篇python分块读取大数据,避免内存不足的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

pip install urllib2不能安装的解决方法

python35 urllib2 不能用 Could not find a version that satisfies the requirement urllib2 (from...

和孩子一起学习python之变量命名规则

变量命名规则 下面是关于变量名(也称为标识符)的一些规则 必须以一个字母或一个下划线字符开头。后面可以使用一个字母、数字或下划线字符的序列,长度不限。 字母可以是大写或小写,大小写...

numpy.linspace函数具体使用详解

numpy.linspace函数具体使用详解

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) 在指定的间隔内返回均匀间隔的数...

pandas的相关系数与协方差实例

1、输出百分比变化以及前后指定的行数 a = np.arange(1,13).reshape(6,2) data = DataFrame(a) #计算列的百分比变化,如果...

python转换摩斯密码示例

复制代码 代码如下:CODE = {'A': '.-',     'B': '-...',   'C': '-.-.',&nb...