《利用Python进行数据分析》,第 6 章的数据加载操作 read_xxx,有 chunksize 参数可以进行逐块加载。
经测试,它的本质就是将文本分成若干块,每次处理 chunksize 行的数据,最终返回一个TextParser 对象,对该对象进行迭代遍历,可以完成逐块统计的合并处理。
文中的示例代码分析如下:
from pandas import DataFrame,Series import pandas as pd path='D:/AStudy2018/pydata-book-2nd-edition/examples/ex6.csv' # chunksize return TextParser chunker=pd.read_csv(path,chunksize=1000) # an array of Series tot=Series([]) chunkercount=0 for piece in chunker: print '------------piece[key] value_counts start-----------' #piece is a DataFrame,lenth is chunksize=1000,and piece[key] is a Series ,key is int ,value is the key column print piece['key'].value_counts() print '------------piece[key] value_counts end-------------' #piece[key] value_counts is a Series ,key is the key column, and value is the key count tot=tot.add(piece['key'].value_counts(),fill_value=0) chunkercount+=1 #last order the series tot=tot.order(ascending=False) print chunkercount print '--------------'
首先,例子数据 ex6.csv 文件总共有 10000 行数据,使用 chunksize=1000 后,read_csv操作返回一个 TextParser 对象,该对象总共有10个元素,遍历过程中打印 chunkercount验证得到。
其次,每个 piece 对象是一个 DataFrame 对象,piece['key'] 得到的是一个 Series 对象,默认是数值索引,值为 csv 文件中的 key 列的值,即各个字符串。
将每个 Series 的 value_counts 作为一个Series,与上一次统计的 tot 结果进行 add 操作,最终得到所有块数据中各个 key 的累加值。
最后,对 tot 进行 order 排序,按降序得到各个 key 的值在 csv 文件中出现的总次数。
这里很巧妙了使用 Series 对象的 add 操作,对两个 Series 执行 add 操作,即合并相同key:key相同的记录的值累加,key不存在的记录设置填充值为0。
输出结果为:
到此这篇关于Python 数据分析之逐块读取文本的实现的文章就介绍到这了,更多相关Python 逐块读取文本内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。