通过Pandas读取大文件的实例
当数据文件过大时,由于计算机内存有限,需要对大文件进行分块读取:
import pandas as pd f = open('E:/学习相关/Python/数据样例/用户侧数据/test数据.csv') reader = pd.read_csv(f, sep=',', 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.") df = pd.concat(chunks, ignore_index=True) print(df)
read_csv()函数的iterator参数等于True时,表示返回一个TextParser以便逐块读取文件;
chunkSize表示文件块的大小,用于迭代;
TextParser类的get_chunk方法用于读取任意大小的文件块;
StopIteration的异常表示在循环对象穷尽所有元素时报错;
concat()函数用于将数据做轴向连接:
pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, keys=None, levels=None, names=None, Verify_integrity=False)
常用参数:
objs:Series,DataFrame或者是Panel构成的序列list;
axis:需要合并连接的轴,0是行,1是列;
join:连接的参数,inner或outer;
ignore=True表示重建索引。
相关推荐
三石 2020-10-30
roamer 2020-10-29
三石 2020-10-29
wangquannuaa 2020-10-15
wangquannuaa 2020-09-29
jzlixiao 2020-09-15
wangquannuaa 2020-08-30
三石 2020-08-23
逍遥友 2020-08-21
jzlixiao 2020-08-18
wangquannuaa 2020-08-17
QianYanDai 2020-08-16
cjsyrwt 2020-08-14
jzlixiao 2020-07-29
xirongxudlut 2020-07-20
mmmjyjy 2020-07-16
QianYanDai 2020-07-05
QianYanDai 2020-07-05
june0 2020-07-04