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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM