python统计分析-全表统计

 

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

# <editable>

def execute():
    # <editable>
    '''
    载入模块
    '''
    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_out = data_in.describe().T
    data_out.columns = ['count', 'mean', 'std', 'min', 'upper_quartile', 'median', 'lower_quartile', 'max']
    index = pd.DataFrame(data_out.index, columns=['col'], index=data_out.index)
    data_out = data_out.apply(lambda x: round(x, 2), axis=1)
    data_out = pd.concat([index, data_out], axis=1)
    '''
    将结果写出
    '''
    print(dict(data_out))


# </editable>


if __name__ == '__main__':
    execute()

 

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