浏览工具排名-Pandas-Python数据分析(11)

HH python1 8,5682字数 1423阅读4分44秒阅读模式

1.1. Pandas分析步骤

  1. 载入数据
  2. 将 浏览工具排名 进行 COUNT。类似如下SQL:
SELECT browser,
    count(*)
FROM log
GROUP BY browser
ORDER BY count(*)
LIMIT 0, 100;

1.2. 代码

cat pd_ng_log_stat.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
 
from ng_line_parser import NgLineParser
 
import pandas as pd
import socket
import struct
 
class PDNgLogStat(object):
 
    def __init__(self):
        self.ng_line_parser = NgLineParser()
 
    def _log_line_iter(self, pathes):
        """解析文件中的每一行并生成一个迭代器"""
        for path in pathes:
            with open(path, 'r') as f:
                for index, line in enumerate(f):
                    self.ng_line_parser.parse(line)
                    yield self.ng_line_parser.to_dict()
 
    def load_data(self, path):
        """通过给的文件路径加载数据生成 DataFrame"""
        self.df = pd.DataFrame(self._log_line_iter(path))
 
    def browser_stat(self):
        """统计不同浏览器访问次数"""
        group_by_cols = ['browser'] # 需要分组的列,只计算和显示该列
         
        # 直接统计次数
        url_req_grp = self.df[group_by_cols].groupby(
                                     self.df['browser'])
        return url_req_grp.agg(['count'])['browser'].nlargest(100, 'count')
 
def main():
    file_pathes = ['www.ttmark.com.access.log']
 
    pd_ng_log_stat = PDNgLogStat()
    pd_ng_log_stat.load_data(file_pathes)
 
    # 统计 统计不同浏览器访问次数
    print pd_ng_log_stat.browser_stat()
 
if __name__ == '__main__':
    main()

运行统计和输出结果

python pd_ng_log_stat.py
 
                                                     count
browser                                                   
Googlebot/2.1; +http://www.google.com/bot.html)     104533
Chrome/47.0.2526.106 larbin2.6.3@unspecified.mail   101013
bingbot/2.0; +http://www.bing.com/bingbot.htm)       57072
......
P1 4.1.2)                                              613
Safari/534.30 OppoBrowser/3.9.2                        610
9.3.2; zh_CN)                                          601
 
[100 rows x 1 columns]

昵称: HH文章源自运维生存时间-https://www.ttlsa.com/python/python-big-data-borwser-order-pandas/

QQ: 275258836文章源自运维生存时间-https://www.ttlsa.com/python/python-big-data-borwser-order-pandas/

ttlsa群交流沟通(QQ群②: 6690706 QQ群③: 168085569 QQ群④: 415230207(新) 微信公众号: ttlsacom)文章源自运维生存时间-https://www.ttlsa.com/python/python-big-data-borwser-order-pandas/

感觉本文内容不错,读后有收获?文章源自运维生存时间-https://www.ttlsa.com/python/python-big-data-borwser-order-pandas/

逛逛衣服店,鼓励作者写出更好文章。文章源自运维生存时间-https://www.ttlsa.com/python/python-big-data-borwser-order-pandas/ 文章源自运维生存时间-https://www.ttlsa.com/python/python-big-data-borwser-order-pandas/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
HH
  • 本文由 发表于 05/11/2016 00:17:59
  • 转载请务必保留本文链接:https://www.ttlsa.com/python/python-big-data-borwser-order-pandas/
评论  1  访客  1
    • O(∩_∩)O哈哈~
      O(∩_∩)O哈哈~ 9

      ( ^_^ )不错嘛

    评论已关闭!