Python 数据分析之逐块读取文本的实现
《利用Python进行数据分析》,第 6 章的数据加载操作 read_xxx,有 chunksize 参数可以进行逐块加载。
经测试,它的本质就是将文本分成若干块,每次处理 chunksize 行的数据,最终返回一个TextParser 对象,对该对象进行迭代遍历,可以完成逐块统计的合并处理。
示例代码文中的示例代码分析如下:
from pandas import DataFrame,Seriesimport pandas as pd path=’D:/AStudy2018/pydata-book-2nd-edition/examples/ex6.csv’# chunksize return TextParserchunker=pd.read_csv(path,chunksize=1000) # an array of Seriestot=Series([])chunkercount=0for 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 columnprint 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 counttot=tot.add(piece[’key’].value_counts(),fill_value=0)chunkercount+=1 #last order the seriestot=tot.order(ascending=False)print chunkercountprint ’--------------’流程分析
首先,例子数据 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 逐块读取文本内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章:
1. IE6/IE7/IE8/IE9中tbody的innerHTML不能赋值的完美解决方案2. WMLScript的语法基础3. xpath简介_动力节点Java学院整理4. asp中response.write("中文")或者js中文乱码问题5. ASP中格式化时间短日期补0变两位长日期的方法6. jsp实现textarea中的文字保存换行空格存到数据库的方法7. 读大数据量的XML文件的读取问题8. 将properties文件的配置设置为整个Web应用的全局变量实现方法9. 一款功能强大的markdown编辑器tui.editor使用示例详解10. HTML5 Canvas绘制图形从入门到精通
