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

import pandas as pd
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 = data.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与数据分析)