美文网首页通过python看世界python基础学习Mysql
14种方式,34个案例:对比SQL,学习Pandas操作

14种方式,34个案例:对比SQL,学习Pandas操作

作者: 皮皮大 | 来源:发表于2021-08-23 09:53 被阅读0次

    公众号:尤而小屋
    作者:Peter
    编辑:Peter

    大家好,我是Peter~

    本文主题:对比SQL,学习Pandas操作!

    在SQL中查询数据的时候我们所有各种操作,主要是通过select、where、group by等多个关键词的组合查询来实现的。本文中介绍的如何在相同的需求下,通过pandas来实现取数操作。

    image

    比较方向

    1. 查询全部数据
    2. 前N条
    3. 后N条
    4. 中间段数据
    5. 部分字段
    6. 指定等式条件
    7. 指定不等式条件
    8. 取反操作
    9. 指定多个条件
    10. 指定计算等式
    11. 模糊查询
    12. 排序
    13. 分组统计
    14. 取别名

    参考资料

    因为本文主要介绍的是如何通过pandas来获取我们想要的数据,也是pandas的各种取数技巧,参考之前介绍的3篇文章:

    模拟数据

    在数据库中,我们先模拟了3份数据:

    1、学生信息表

    -- 学生信息
    
    mysql> select *  from Student;
    +------+--------+------------+-------+
    | s_id | s_name | s_birth    | s_sex |
    +------+--------+------------+-------+
    | 01   | 赵雷   | 1990-01-01 | 男    |
    | 02   | 钱电   | 1990-12-21 | 男    |
    | 03   | 孙风   | 1990-05-20 | 男    |
    | 04   | 李云   | 1990-08-06 | 男    |
    | 05   | 周梅   | 1991-12-01 | 女    |
    | 06   | 吴兰   | 1992-03-01 | 女    |
    | 07   | 郑竹   | 1989-07-02 | 女    |
    | 08   | 王菊   | 1990-01-20 | 女    |
    +------+--------+------------+-------+
    8 rows in set (0.00 sec)
    

    2、一份用户表

    image

    3、一份水果商品价格表

    image

    下面开始介绍不同需求下基于pandas和SQL的取数实现

    取出全部数据

    SQL实现

    select *  from Student;
    
    image

    Pandas实现

    image

    前N条数据

    SQL实现

    查看前5条数据:

    image image

    Pandas实现

    head方法默认是前5条:

    image

    指定查看前7条数据:

    image

    后N条数据

    select * 
    from (select * from Student 
          order by s_id desc 
          limit 5)t   -- 临时结果表:倒序输出的最后5条
    order by s_id;  -- 再使用一次排序,将顺序还原
    
    image

    Pandas实现

    tail方法默认是后5条:

    image

    指定查看4条

    image

    切片数据

    SQL实现

    image

    Pandas实现

    使用pandas中的切片来查看某个连续区间内的数据:

    image

    取出部分字段

    SQL实现

    image

    Pandas实现

    df1[["id","name","sex"]]  # 方式1
    
    df2.filter(items=["id","age","createtime"])   # 方式2
    
    image

    指定等式条件

    SQL实现

    image image

    Pandas实现

    df1[df1["sex"] == "男"]  # 方式1
    df1.query('sex=="男"')   # 方式2
    
    image

    指定id号或者年龄age:

    image

    指定不等式条件

    SQL实现

    select * from Student where s_sex!= "男";
    select * from user where age > 18;
    select * from user where id <= 3; 
    
    image

    Pandas实现

    image image image

    取反操作

    SQL实现

    mysql> select * from Student where s_sex != "男";
    
    image

    Pandas实现

    image image image

    指定多个条件

    SQL实现

    select * from Student where s_birth <="1991-01-01" and  s_sex= "男";
    select * from user where age < 20 and fee > 60;
    select * from user where age < 20 and fee > 60;
    

    Pandas实现

    image

    指定计算等式

    SQL实现

    select * from user where age % 3 = 0;  -- 年龄分别是3或者2的倍数
    select * from user where age % 2 = 0;  
    
    image

    Pandas实现

    image image

    模糊查询

    SQL实现

    SQL的关键词是like:

    • 左匹配
    • 右匹配
    • 全匹配
    image

    Pandas实现

    image image

    排序

    默认是升序,可以指定为降序

    SQL实现

    1、单个字段

    image
    select * from Student order by s_birth desc;   -- 改成升序
    

    2、多个字段的排序

    image

    Pandas实现

    1、单个字段

    image image

    2、多个字段

    image

    分组统计

    SQL实现

    通过group by 来进行分组统计:

    image

    Pandas实现

    先看看df3的数据,一个水果会对应多个价格,我们水果的名称对价格汇总:

    image
    df3.groupby("name").agg({"price":"sum"}).reset_index()  # 方式1
    
    df3.groupby("name")["price"].sum().reset_index()   # 方式2
    
    image

    取别名

    SQL实现

    通过使用as 关键词:

    select name as  水果, sum(price) as  价格 from products group by name;
    
    image

    Pandas实现

    Pandas是通过rename函数来实现的:

    df3.groupby("name").agg({"price":"sum"}).reset_index().rename(columns={"name":"水果","price":"价格"})
    
    image

    相关文章

      网友评论

        本文标题:14种方式,34个案例:对比SQL,学习Pandas操作

        本文链接:https://www.haomeiwen.com/subject/iqikiltx.html