《Python Cookbook(第3版)中文版》——6.13 数据汇总和统计

6.13 数据汇总和统计

6.13.1 问题

我们需要在大型数据库中查询数据并由此生成汇总或者其他形式的统计数据。

6.13.2 解决方案

对于任何涉及统计、时间序列以及其他相关技术的数据分析问题,都应该使用Pandas库(http://pandas.pydata.org)。

为了小试牛刀,下面这个例子使用Pandas来分析芝加哥的老鼠和啮齿动物数据库(https:// data.cityofchicago.org/Service-Requests/311-Service-Requests-Rodent-Baiting/97t6-zrhs)。在写作本书时,这个CSV文件中有大约74 000条数据:

>>> import pandas
>>> # Read a CSV file, skipping last line
>>> rats = pandas.read_csv('rats.csv', skip_footer=1)
>>> rats
<class 'pandas.core.frame.DataFrame'>
Int64Index: 74055 entries, 0 to 74054
Data columns:
Creation Date                 74055 non-null values
Status                     74055 non-null values
Completion Date                 72154 non-null values
Service Request Number         74055 non-null values
Type of Service Request         74055 non-null values
Number of Premises Baited         65804 non-null values
Number of Premises with Garbage    65600 non-null values
Number of Premises with Rats     65752 non-null values
Current Activity             66041 non-null values
Most Recent Action             66023 non-null values
Street Address                 74055 non-null values
ZIP Code                     73584 non-null values
X Coordinate                 74043 non-null values
Y Coordinate                 74043 non-null values
Ward                         74044 non-null values
Police District                 74044 non-null values
Community Area                 74044 non-null values
Latitude                     74043 non-null values
Longitude                     74043 non-null values
Location                     74043 non-null values
dtypes: float64(11), object(9)
>>> # Investigate range of values for a certain field
>>> rats['Current Activity'].unique()
array([nan, Dispatch Crew, Request Sanitation Inspector], dtype=object)
>>> # Filter the data
>>> crew_dispatched = rats[rats['Current Activity'] == 'Dispatch Crew']
>>> len(crew_dispatched)
65676
>>>
>>> # Find 10 most rat-infested ZIP codes in Chicago
>>> crew_dispatched['ZIP Code'].value_counts()[:10]
60647         3837
60618         3530
60614         3284
60629         3251
60636         2801
60657         2465
60641         2238
60609         2206
60651         2152
60632         2071
>>>
>>> # Group by completion date
>>> dates = crew_dispatched.groupby('Completion Date')
<pandas.core.groupby.DataFrameGroupBy object at 0x10d0a2a10>
>>> len(dates)
472
>>>
>>> # Determine counts on each day
>>> date_counts = dates.size()
>>> date_counts[0:10]
Completion Date
01/03/2011               4
01/03/2012             125
01/04/2011              54
01/04/2012              38
01/05/2011              78
01/05/2012             100
01/06/2011             100
01/06/2012              58
01/07/2011               1
01/09/2012              12
>>>

>>> # Sort the counts
>>> date_counts.sort()
>>> date_counts[-10:]
Completion Date
10/12/2012             313
10/21/2011             314
09/20/2011             316
10/26/2011             319
02/22/2011             325
10/26/2012             333
03/17/2011             336
10/13/2011             378
10/14/2011             391
10/07/2011             457
>>>

你没看错,2011年10月7号对于老鼠来说的确是非常忙碌的一天。

6.13.3 讨论

Pandas是一个庞大的库,它还有更多的功能,但我们无法在此一一描述。但是,如果需要分析大型的数据集、将数据归组、执行统计分析或者其他类似的任务,那么Pandas绝对值得一试。

时间: 2024-08-02 07:42:37

《Python Cookbook(第3版)中文版》——6.13 数据汇总和统计的相关文章

《Python Cookbook(第3版)中文版》——导读

前 言 自2008年以来,我们已经目睹了整个Python世界正缓慢向着Python 3进化的事实.众所周知,完全接纳Python 3要花很长的时间.事实上,就在写作本书时(2013年),大多数Python程序员仍然坚持在生产环境中使用Python 2.关于Python 3不能向后兼容的事实也已经做了许多努力来补救.的确,向后兼容性对于任何已经存在的代码库来说是个问题.但是,如果你着眼于未来,你会发现Python 3带来的好处绝非那么简单. 正因为Python 3是着眼于未来的,本书在之前的版本上

拒绝从入门到放弃_《Python 核心编程 (第二版)》必读目录

目录 目录 关于这本书 必看知识点 最后 关于这本书 <Python 核心编程 (第二版)>是一本 Python 编程的入门书,分为 Python 核心(其实并不核心,应该叫基础) 和 高级主题 两大部分,以 Python 2.x 作为主要演示版本,涵盖的知识面广,知识点较齐全,代码多且好理解,但对 Python 版本特性的内容太久远,不合时宜. 整体来说 Python 核心 部分是主要内容,高级主题 部分作为应用扩展内容.后半部分篇幅较短,内容不够深入,只到了解的层面,好在横向够广(每一个主

谁有&amp;amp;lt;&amp;amp;lt;CLR Via C#&amp;amp;gt;&amp;amp;gt;第三版中文版的电子书

问题描述 谁有<<CLRViaC#>>第三版中文版的电子书,我是个初学者,看网上推荐此书的人多,想看一下,我的QQ:330784617.谢谢!! 解决方案 解决方案二:试一试我一般看英文的,虽然很少看书:(解决方案三: 解决方案四:第二版有的,想看第三版.

(六十二)第四章总结——《C++ Primer Plus 第6版 中文版》

书是<C++ Primer Plus  第6版  中文版> 数组.指针.结构 是C++的3种复合类型.   注:为了方便,类型名用int为主,变量名用a为主.   数组: 包括数组(例如int a[10];)和字符串(例如char a[10];),还有string类(例如string a="abc";),vector类(例如vector<int>a(5)).array类(array<int,3>a)等. 数组名表示数组所在的(第一个元素)内存地址.

求大神解答一下-C++ primer plus 第6版 中文版 第16章复习题的一个问题

问题描述 C++ primer plus 第6版 中文版 第16章复习题的一个问题 奇葩的是课后居然没答案...... 求正规.严谨.简洁的标准答案! 程序清单16.15(在p708页):functor.cpp //functor.cpp--using a functor #include尖括号iostream尖括号 #include尖括号list尖括号 #include尖括号iterator尖括号 #include尖括号algorithm尖括号 template//functor class

【转】c++.primer.plus.第五版.中文版[下载]

c++.primer.plus.第五版.中文版[下载] 一共有5部分.全部下载完才可解压阅读. c++.primer.plus.第五版.中文版(一) c++.primer.plus.第五版.中文版(二) c++.primer.plus.第五版.中文版(三) c++.primer.plus.第五版.中文版(四) c++.primer.plus.第五版.中文版(五) "在遇到无法解决的问题时,我总会求助于C++ Primer一书."--Bruce Eckel,"编程思想"

发布火狐3.6.13版,修复了3.6.12版存在的13个安全漏洞

国外媒体报道,火狐(Firefox)浏览器开发商Mozilla基金会今天表示,已发布火狐3.6.13版,该升级版修复了3.6.12版存在的13个安全漏洞,其中11个危害级别为"危急"(critical),1个为"高级" (high),另1个为"中等"(moderate). Mozilla称,利用这11个危急漏洞,网络犯罪者可向用户发起远程攻击,进而在用户机器中安装恶意软件.在此次修复的13个安全漏洞中,其中一个Mozilla以为在今年3月已经修复

《Python Cookbook(第2版)中文版》——1.13 访问子字符串

1.13 访问子字符串 任务 获取字符串的某个部分.比如,你读取了一条定长的记录,但只想获取这条记录中的某些字段的数据. 解决方案 切片是个好方法,但是它一次只能取得一个字段: afield = theline[3:8] 如果还需考虑字段的长度,struct.unpack可能更适合.比如: import struct # 得到一个5字节的字符串,跳过3字节,得到两个8字节字符串,以及其余部分: baseformat = "5s 3x 8s 8s" # theline超出的长度也由这个b

《Python Cookbook(第3版)中文版》——1.13 通过公共键对字典列表排序

1.13 通过公共键对字典列表排序 1.13.1 问题 我们有一个字典列表,想根据一个或多个字典中的值来对列表排序. 1.13.2 解决方案 利用operator模块中的itemgetter函数对这类结构进行排序是非常简单的.假设通过查询数据库表项获取网站上的成员列表,我们得到了如下的数据结构: rows = [ {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003}, {'fname': 'David', 'lname': 'Beazley', 'u