python pandas分组统计

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 4605

2017/11/14 23:59


利用panda便捷的对日志分组统计:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/11/14 下午6:27
# @Author  : wz
# @Email   : 277215243@qq.com
# @File    : testpanda.py
# @web     : https://www.bthlt.com
import pandas

'''
2017-10-30 20:21:06.626735|27024|54622785|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
2017-10-30 20:22:13.790012|27024|54622785|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
2017-10-30 20:22:13.790012|27024|54622785|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList error!
2017-10-30 20:23:31.268376|27024|54622785|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
2017-10-30 20:26:27.457603|27024|54622785|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
2017-10-30 20:28:10.924817|27024|277215243|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
2017-10-30 20:29:39.487600|27024|54622785|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
2017-10-30 20:21:06.626735|27024|54622785|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
2017-10-30 20:22:13.790012|27024|54622785|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList error!
2017-10-30 20:22:13.790012|27024|54622785|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
2017-10-30 20:23:31.268376|27024|54622785|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
2017-10-30 20:26:27.457603|27024|277215243|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
2017-10-30 20:28:10.924817|27024|54622785|0|1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
2017-10-30 20:29:39.487600|27024|54622785||1E9BBD18CF6E16AFE36E0EE5B46E5976|db_handler.cpp:1186:OnGetRoleList|ParseRoleList succ!
'''


def distinct(rdtb, colname):
    return rdtb[colname].value_counts()


if __name__ == '__main__':
    colname = ['time', 'id', 'qq', 'value', 'tag', 'proc', 'result']

    rdtb = pandas.read_table('./testpanda.txt', sep='|', names=colname)
    print distinct(rdtb, 'qq')
    print 'dict格式化'

    print [dict(distinct(rdtb, col)) for col in colname if col!='time']

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1