python统计分析-频数统计

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-

# <editable>

def execute():
    # <editable>
    '''
    载入模块
    '''
    from collections import Counter
    import pandas as pd
    from sqlalchemy import create_engine
    '''
    连接数据库
    '''
    engine = create_engine('mysql+pymysql://root:123123qwe@127.0.0.1:3306/analysis')
    '''
    选择目标数据
    '''
    # 生成数据

    params = {
        "columns": "SUNACTIVITY",
    }
    inputs = {"table": '纯随机性检验'}
    data_sql = 'select ' + params['columns'] + ' from ' + inputs['table']
    data_in = pd.read_sql_query(data_sql, engine)
    print(data_in)

    '''
    频数统计
    '''
    data = data_in[params['columns']]
    data_out = pd.DataFrame.from_dict(Counter(data), orient='index').reset_index()
    data_out.columns = [params['columns'], 'count']

    '''
    将结果写出
    '''
    print(data_out)


# </editable>


if __name__ == '__main__':
    execute()

 

posted @ 2021-04-22 11:29  我当道士那儿些年  阅读(1247)  评论(0编辑  收藏  举报