两个参数:chunksize,iterator
1、chunksize
read_csv
和 read_table
有一个chunksize参数,用以指定一个块大小(每次读取多少行),返回一个可迭代的 TextFileReader
对象。
import pandas as pd reader = pd.read_csv("pff_GEN_NUCHANGE.csv",chunksize=10000) for df in reader : 对df处理 #如df.drop(columns=['GEN_id'],axis=1,inplace=True) #print(type(df),df.shape)打印看一下信息
to_csv也同样有chunksize参数
2、iterator=True
import pandas as pd reader = pd.read_csv("pff_GEN_NUCHANGE.csv", iterator=True) loop = True chunkSize = 100000 chunks = [] while loop: try: chunk = reader.get_chunk(chunkSize) chunks.append(chunk) except StopIteration: loop = False print ("Iteration is stopped.") pff_AA_df = pd.concat(chunks, ignore_index=True)