zoukankan      html  css  js  c++  java
  • python 用 pymysql 执行 case when 实现分组统计

     

     1、加载库、定义函数

    import pandas as pd
    import pymysql
    
    def mycursor(db_name=None):
        '''连接数据库,创建游标'''
        config = dict(zip(['host', 'user', 'port', 'password'],
                          ['192.168.137.155', 'shanger', 3306, '0123']))
        config.update(database=db_name)
        connection = pymysql.connect(**config)
        cursor = connection.cursor()
        return cursor
    
    def use(db_name):
        '''切换数据库,返回游标'''
        return mycursor(db_name)
        
    def query(sql):
        '''以数据框形式返回查询据结果'''
        cursor.execute(sql)
        data = cursor.fetchall()  # 以元组形式返回查询数据
        header = [t[0] for t in cursor.description]
        df = pd.DataFrame(list(data), columns=header)  # pd.DataFrem 对列表具有更好的兼容性
        return df
    
    def select_all_from(table):
        sql = f'select * from {table};'
        return query(sql)

     

    2、统计

    cursor = use('sql123')   # 切换数据库
    
    select_all_from('poptbl')  # 查看所有记录

    分组统计

    sql = '''
    select case pref_name
                 when '德岛' then '四国'
                 when '香川' then '四国'
                 when '爱媛' then '四国'
                 when '高知' then '四国'
                 when '福冈' then '九州'
                 when '佐贺' then '九州'
                 when '长崎' then '九州'
                 else '其他' end as 地区,
           sum(population) as 人口数
      from poptbl
     group by 1;
    '''
    
    query(sql).sort_values(by='人口数', ascending=False)

     

  • 相关阅读:
    Google服务
    Duwamish深入剖析配置篇
    Duwamish Online SQL XML 分类浏览
    数据库操作类
    搜索引擎Google的小秘密
    微软.NET经典架构例程Duwamish 7.0分析
    一个ASP.NET中使用的MessageBox类
    轻松解决页面回传后页面滚动到顶端
    Duwamish 7 初探——数据流程
    使用ADO.NET的最佳实践
  • 原文地址:https://www.cnblogs.com/shanger/p/12982745.html
Copyright © 2011-2022 走看看