Calcifer Calcifer 2
Python

pandas分组统计

2017/11/14 23:59 6287 次阅读 王梓
打赏
✸ ✸ ✸

利用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']
✸ ✸ ✸

📜 版权声明

本文作者:王梓 | 原文链接:https://www.bthlt.com/note/5613483-Pythonpandas分组统计

出处:葫芦的运维日志 | 转载请注明出处并保留原文链接

📜 留言板

留言提交后需管理员审核通过才会显示