time_zones[:10]
Out[19]:
[u'America/New_York',
u'America/Denver',
u'America/New_York',
u'America/Sao_Paulo',
u'America/New_York',
u'America/New_York',
u'Europe/Warsaw',
u'',
u'',
u'']

接下来对时区进行计数,这里使用两种方法,一种使用Python标准库,另一个比较简(使用pandas)。

def get_counts(sequence):
counts = {}
for x in sequence:
if x in counts:
counts[x] += 1
else:
counts[x] = 1
return counts get_counts(time_zones[:10])
Out[21]:
{u'': 3,
u'America/Denver': 1,
u'America/New_York': 4,
u'America/Sao_Paulo': 1,
u'Europe/Warsaw': 1}

使用标准款,简洁版

from collections import defaultdict

def get_counts2(sequence):
counts = defaultdict(int) # 所有的值均会被初始化为0
for x in sequence:
counts[x] += 1
return counts get_counts2(time_zones[:10])
Out[24]: defaultdict(<type 'int'>, {u'Europe/Warsaw': 1, u'America/Denver': 1, u'America/Sao_Paulo': 1, u'': 3, u'America/New_York': 4})

要它对时区进行处理,传入time_zones传入即可:

counts = get_counts(time_zones)

counts['America/New_York']
Out[27]: 1251 len(time_zones)
Out[28]: 3440

得到前10位的时区及计数值:

def top_counts(count_dict, n = 10):
value_key_pairs = [(count,tz) for tz,count in count_dict.items()]
value_key_pairs.sort()
return value_key_pairs[-n:] # 得到倒数第n个数到最后 top_counts(counts)
Out[30]:
[(33, u'America/Sao_Paulo'),
(35, u'Europe/Madrid'),
(36, u'Pacific/Honolulu'),
(37, u'Asia/Tokyo'),
(74, u'Europe/London'),
(191, u'America/Denver'),
(382, u'America/Los_Angeles'),
(400, u'America/Chicago'),
(521, u''),
(1251, u'America/New_York')]

使用collection.Counter类,使这个任务变的简单:

from collections import Counter

counts = Counter(time_zones)

counts.most_common(10)
Out[33]:
[(u'America/New_York', 1251),
(u'', 521),
(u'America/Chicago', 400),
(u'America/Los_Angeles', 382),
(u'America/Denver', 191),
(u'Europe/London', 74),
(u'Asia/Tokyo', 37),
(u'Pacific/Honolulu', 36),
(u'Europe/Madrid', 35),
(u'America/Sao_Paulo', 33)]
  • DataFrame是pandas中最重要的数据结构,用于将数据表示为一个表格。从一组数据中创建DataFrame:

from pandas import DataFrame, Series

import pandas as pd; import numpy as np

frame = DataFrame(records)

frame
Out[38]:
frame['tz'][:10]
Out[40]:
0 America/New_York
1 America/Denver
2 America/New_York
3 America/Sao_Paulo
4 America/New_York
5 America/New_York
6 Europe/Warsaw
7
8
9
Name: tz, dtype: object

这里frame的输出形式是摘要视图,主要用于较大的DataFrame对象。frame['tz']所返回的Series对象有一个value_counts方法,改方法可以让我们得到所需的信息:

tz_counts = frame['tz'].value_counts()

tz_counts[:10]
Out[42]:
America/New_York 1251
521
America/Chicago 400
America/Los_Angeles 382
America/Denver 191
Europe/London 74
Asia/Tokyo 37
Pacific/Honolulu 36
Europe/Madrid 35
America/Sao_Paulo 33
dtype: int64

利用绘图库(matplotlib)为这段数据生成一张的图片。

为此,先给记录中未知或缺失的时区填上一个替代值。fillna函数可以替代缺失值(NA),而未知值(空字符串)则可通过 布尔型数组索引加以替换:

clean_tz = frame['tz'].fillna('Missing')  # 替换缺失值(NA)

clean_tz[clean_tz == ''] = 'Unknown'      # 未知值通过布尔型数组索引加以替换,注意这里的 clean_tz是 Series对象

tz_counts = clean_tz.value_counts()

tz_counts[:10]
Out[46]:
America/New_York 1251
Unknown 521
America/Chicago 400
America/Los_Angeles 382
America/Denver 191
Missing 120
Europe/London 74
Asia/Tokyo 37
Pacific/Honolulu 36
Europe/Madrid 35
dtype: int64
tz_counts[:10].plot(kind='barh', rot=0)
Out[52]: <matplotlib.axes._subplots.AxesSubplot at 0xf6de430>

results = Series([x.split()[0] for x in frame.a.dropna()])

results[:5]
Out[65]:
0 Mozilla/5.0
1 GoogleMaps/RochesterNY
2 Mozilla/4.0
3 Mozilla/5.0
4 Mozilla/5.0
dtype: object results.value_counts()[:]
Out[66]:
Mozilla/5.0 2594
Mozilla/4.0 601
GoogleMaps/RochesterNY 121
Opera/9.80 34
TEST_INTERNET_AGENT 24
GoogleProducer 21
Mozilla/6.0 5
BlackBerry8520/5.0.0.681 4
dtype: int64

按Windows和非Windows用户对时区统计信息进行分解。

  • 由于有agent缺失值,首先将他们从数据中移除: 

cframe = frame[frame.a.notull()]
  •  根据a值计算出各行是否是Windows:

  • 根据时区和新得到的操作系统列表对数据进行分组

  • 通过size对分组结果进行计数(类似上面value_counts()),并用unstack()对计数结果进行重塑:

operating_system = np.where(cframe['a'].str.contains('Windows'),
'Windows', 'Not Windows') operating_system[:5]
Out[79]:
array(['Windows', 'Not Windows', 'Windows', 'Not Windows', 'Windows'],
dtype='|S11') by_tz_os = cframe.groupby(['tz', operating_system]) by_tz_os
Out[81]: <pandas.core.groupby.DataFrameGroupBy object at 0x0F9C4D30> agg_counts = by_tz_os.size().unstack().fillna(0) # 拆分列 agg_counts[:10]
Out[83]:
Not Windows Windows
tz
245 276
Africa/Cairo 0 3
Africa/Casablanca 0 1
Africa/Ceuta 0 2
Africa/Johannesburg 0 1
Africa/Lusaka 0 1
America/Anchorage 4 1
America/Argentina/Buenos_Aires 1 0
America/Argentina/Cordoba 0 1
America/Argentina/Mendoza 0 1

 没有拆分列是这种效果(unstack):

Out[87]:
tz
Not Windows 245
Windows 276
Africa/Cairo Windows 3
Africa/Casablanca Windows 1
Africa/Ceuta Windows 2
dtype: int64
# 用于按升序排列
# 行相加(win+nowin之和,排序),返回数值从小到大的索引值
indexer = agg_counts.sum(1).argsort() indexer[:10]
Out[90]:
tz
24
Africa/Cairo 20
Africa/Casablanca 21
Africa/Ceuta 92
Africa/Johannesburg 87
Africa/Lusaka 53
America/Anchorage 54
America/Argentina/Buenos_Aires 57
America/Argentina/Cordoba 26
America/Argentina/Mendoza 55
dtype: int64

通过take,截取了最后10行:(根据索引截取数据)

count_subset = agg_counts.take(indexer)[-10:]

count_subset
Out[97]:
Not Windows Windows
tz
America/Sao_Paulo 13 20
Europe/Madrid 16 19
Pacific/Honolulu 0 36
Asia/Tokyo 2 35
Europe/London 43 31
America/Denver 132 59
America/Los_Angeles 130 252
America/Chicago 115 285
245 276
America/New_York 339 912

生成一张条形图,使用stacked=True 来生成一张堆积条形图

count_subset.plot(kind='barh', stacked=True)
Out[98]: <matplotlib.axes._subplots.AxesSubplot at 0xfd535f0>

将各行规划化为“总计为1”,并重新绘图

normed_subset = count_subset.div(count_subset.sum(1), axis=0)

normed_subset.plot(kind='barh', stacked=True)
Out[100]: <matplotlib.axes._subplots.AxesSubplot at 0xfd53990>

Python进行数据分析(一)初步学习 对时区进行计数的更多相关文章

  1. 数据分析---《Python for Data Analysis》学习笔记【01】

    <Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...

  2. 初步学习python

    自计算机诞生以来,也伴随着计算机语言的诞生,现在,全世界的编程语言有600多种,但流行的编程语言也就20多种. Java和C一直占据着前两名.但是近年来伴随着人工智能的发展,Python发展迅猛,以其 ...

  3. PYTHON学习(三)之利用python进行数据分析(1)---准备工作

    学习一门语言就是不断实践,python是目前用于数据分析最流行的语言,我最近买了本书<利用python进行数据分析>(Wes McKinney著),还去图书馆借了本<Python数据 ...

  4. 利用python进行数据分析——(一)库的学习

    总结一下自己对python常用包:Numpy,Pandas,Matplotlib,Scipy,Scikit-learn 一. Numpy: 标准安装的Python中用列表(list)保存一组值,可以用 ...

  5. $《利用Python进行数据分析》学习笔记系列——IPython

    本文主要介绍IPython这样一个交互工具的基本用法. 1. 简介 IPython是<利用Python进行数据分析>一书中主要用到的Python开发环境,简单来说是对原生python交互环 ...

  6. "利用python进行数据分析"学习记录01

    "利用python进行数据分析"学习记录 --day01 08/02 与书相关的资料在 http://github.com/wesm/pydata-book pandas 的2名字 ...

  7. 数据分析---《Python for Data Analysis》学习笔记【04】

    <Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...

  8. 数据分析---《Python for Data Analysis》学习笔记【03】

    <Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...

  9. 数据分析---《Python for Data Analysis》学习笔记【02】

    <Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...

随机推荐

  1. OOP 1.4 内联函数和重载函数函数参数缺省值

    1.内联函数 存在的背景:函数调用存在开销(调用时候参数压栈,返回地址压栈:返回时从栈取出返回地址,跳转到返回地址.总共需要几条指令的开销).如果函数指令较少,调用多次,函数调用的开销占比大. 内联函 ...

  2. 严重: Failed to destroy end point associated with ProtocolHandler ["http-nio-8080"] java.lang.NullPointer

    刚接触servlet类,按照课本的方法使用eclipse新建了一个servlet类. 新建完成后,在web.xml里面进行注册 这时候就会报错了. 五月 07, 2016 11:23:28 上午 or ...

  3. 关于String和StringBuffer的原理

    public class Foo {2.   public static void main (String [] args)  {3.      StringBuffer a = new Strin ...

  4. 3dContactPointAnnotationTool开发日志(三十)

      在vs2017里生成opencv时遇到了无法打开python27_d.lib的问题,具体解决请看这个,不过我用的是方法2,python37_d.lib找不到同理.   Windows下可以用的op ...

  5. Qt富文本编辑器QTextDocument

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt富文本编辑器QTextDocument     本文地址:https://www.tech ...

  6. js动态显示指定的时间

    <p id="timeShow">17:0:50</p> <script> setInterval("time()", 10 ...

  7. Android Espresso(UI自动化测试)的搭建

    Espresso是Google官方提供的Android UI自动化测试的框架. 为什么叫Espresso(浓咖啡),好像是想让Android码农们轻松的写完自动化用例后能享受着咖啡,看着自动化测试&q ...

  8. python常用模块collections os random sys

    Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代码 ...

  9. Doves and bombs UVA - 10765(统计割顶所连接的连通块的数量)

    题意:给定一个n个点的连通的无向图,一个点的“鸽子值”定义为将它从图中删去后连通块的个数. 求对应的点 和 每个点的“鸽子值” 用一个数组在判断割顶的那个地方 累加标记一下所连接的连通块的数量即可 初 ...

  10. 【题解】APIO2014回文串

    哇哦~想不到我有生之年竟然能够做出字符串的题目ヾ(✿゚▽゚)ノ虽然这题比较裸但依然灰常开心! 首先有一个棒棒的性质:本质不同的回文串最多有 O(n) 个.首先 manacher 把它们都找出来,然后问 ...