Skip to content

Commit d09c0d7

Browse files
committed
6.13小节完成,第6章完成。噢耶!
1 parent 4255ffa commit d09c0d7

2 files changed

Lines changed: 112 additions & 6 deletions

File tree

‎README.rst‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@
5050

5151
-----------------------------------------------------
5252

53-
++++++++++++++++
53+
+++++++++++++++++++
5454
How to Contribute
55-
++++++++++++++++
55+
+++++++++++++++++++
5656

5757
You are welcome to contribute to mango-test as follow
5858

59+
* fork project and commit pull requests
5960
* add/edit wiki
6061
* report/fix issue
6162
* code review
@@ -74,7 +75,7 @@ License
7475

7576
(The Apache License)
7677

77-
Copyright (c) 2013-2014 [WinHong, Inc.](http://www.winhong.com/) and other contributors
78+
Copyright (c) 2010-2014 [WinHong, Inc.](http://www.winhong.com/) and other contributors
7879

7980
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
8081

‎source/c06/p13_summarizing_and_perform_statistics.rst‎

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,119 @@
55
----------
66
问题
77
----------
8-
todo...
8+
你需要处理一个很大的数据集并需要计算数据总和或其他统计量。
9+
10+
|
911
1012
----------
1113
解决方案
1214
----------
13-
todo...
15+
对于任何涉及到统计、时间序列以及其他相关技术的数据分析问题,都可以考虑使用 `Pandas库 <http://pandas.pydata.org/>`_ 。
16+
17+
为了让你先体验下,下面是一个使用Pandas来分析芝加哥城市的
18+
`老鼠和啮齿类动物数据库 <https://data.cityofchicago.org/Service-Requests/311-Service-Requests-Rodent-Baiting/97t6-zrhs>`_ 的例子。
19+
在我写这篇文章的时候,这个数据库是一个拥有大概74,000行数据的CSV文件。
20+
21+
.. code-block:: python
22+
23+
>>> import pandas
24+
25+
>>> # Read a CSV file, skipping last line
26+
>>> rats = pandas.read_csv('rats.csv', skip_footer=1)
27+
>>> rats
28+
<class 'pandas.core.frame.DataFrame'>
29+
Int64Index: 74055 entries, 0 to 74054
30+
Data columns:
31+
Creation Date 74055 non-null values
32+
Status 74055 non-null values
33+
Completion Date 72154 non-null values
34+
Service Request Number 74055 non-null values
35+
Type of Service Request 74055 non-null values
36+
Number of Premises Baited 65804 non-null values
37+
Number of Premises with Garbage 65600 non-null values
38+
Number of Premises with Rats 65752 non-null values
39+
Current Activity 66041 non-null values
40+
Most Recent Action 66023 non-null values
41+
Street Address 74055 non-null values
42+
ZIP Code 73584 non-null values
43+
X Coordinate 74043 non-null values
44+
Y Coordinate 74043 non-null values
45+
Ward 74044 non-null values
46+
Police District 74044 non-null values
47+
Community Area 74044 non-null values
48+
Latitude 74043 non-null values
49+
Longitude 74043 non-null values
50+
Location 74043 non-null values
51+
dtypes: float64(11), object(9)
52+
53+
>>> # Investigate range of values for a certain field
54+
>>> rats['Current Activity'].unique()
55+
array([nan, Dispatch Crew, Request Sanitation Inspector], dtype=object)
56+
>>> # Filter the data
57+
>>> crew_dispatched = rats[rats['Current Activity'] == 'Dispatch Crew']
58+
>>> len(crew_dispatched)
59+
65676
60+
>>>
61+
62+
>>> # Find 10 most rat-infested ZIP codes in Chicago
63+
>>> crew_dispatched['ZIP Code'].value_counts()[:10]
64+
60647 3837
65+
60618 3530
66+
60614 3284
67+
60629 3251
68+
60636 2801
69+
60657 2465
70+
60641 2238
71+
60609 2206
72+
60651 2152
73+
60632 2071
74+
>>>
75+
76+
>>> # Group by completion date
77+
>>> dates = crew_dispatched.groupby('Completion Date')
78+
<pandas.core.groupby.DataFrameGroupBy object at 0x10d0a2a10>
79+
>>> len(dates)
80+
472
81+
>>>
82+
83+
>>> # Determine counts on each day
84+
>>> date_counts = dates.size()
85+
>>> date_counts[0:10]
86+
Completion Date
87+
01/03/2011 4
88+
01/03/2012 125
89+
01/04/2011 54
90+
01/04/2012 38
91+
01/05/2011 78
92+
01/05/2012 100
93+
01/06/2011 100
94+
01/06/2012 58
95+
01/07/2011 1
96+
01/09/2012 12
97+
>>>
98+
99+
>>> # Sort the counts
100+
>>> date_counts.sort()
101+
>>> date_counts[-10:]
102+
Completion Date
103+
10/12/2012 313
104+
10/21/2011 314
105+
09/20/2011 316
106+
10/26/2011 319
107+
02/22/2011 325
108+
10/26/2012 333
109+
03/17/2011 336
110+
10/13/2011 378
111+
10/14/2011 391
112+
10/07/2011 457
113+
>>>
114+
嗯,看样子2011年10月7日对老鼠们来说是个很忙碌的日子啊!^_^
115+
116+
|
14117
15118
----------
16119
讨论
17120
----------
18-
todo...
121+
Pandas是一个拥有很多特性的大型函数库,我在这里不可能介绍完。
122+
但是只要你需要去分析大型数据集合、对数据分组、计算各种统计量或其他类似任务的话,这个函数库真的值得你去看一看。
123+

0 commit comments

Comments
 (0)