1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        14種方式,34個案例:對比SQL,學(xué)習(xí)Pandas操作

        共 1619字,需瀏覽 4分鐘

         ·

        2022-01-15 18:16

        ↑?關(guān)注 + 星標(biāo)?,每天學(xué)Python新技能

        后臺回復(fù)【大禮包】送你Python自學(xué)大禮包

        大家好,我是龍哥!

        本文主題:對比SQL,學(xué)習(xí)Pandas操作!

        在SQL中查詢數(shù)據(jù)的時(shí)候我們所有各種操作,主要是通過select、where、group by等多個關(guān)鍵詞的組合查詢來實(shí)現(xiàn)的。本文中介紹的如何在相同的需求下,通過pandas來實(shí)現(xiàn)取數(shù)操作。

        比較方向

        1. 查詢?nèi)繑?shù)據(jù)
        2. 前N條
        3. 后N條
        4. 中間段數(shù)據(jù)
        5. 部分字段
        6. 指定等式條件
        7. 指定不等式條件
        8. 取反操作
        9. 指定多個條件
        10. 指定計(jì)算等式
        11. 模糊查詢
        12. 排序
        13. 分組統(tǒng)計(jì)
        14. 取別名

        模擬數(shù)據(jù)

        在數(shù)據(jù)庫中,我們先模擬了3份數(shù)據(jù):

        1、學(xué)生信息表

        --?學(xué)生信息

        mysql>?select?*??from?Student;
        +------+--------+------------+-------+
        |?s_id?|?s_name?|?s_birth????|?s_sex?|
        +------+--------+------------+-------+
        |?01???|?趙雷???|?1990-01-01?|?男????|
        |?02???|?錢電???|?1990-12-21?|?男????|
        |?03???|?孫風(fēng)???|?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、一份用戶表

        3、一份水果商品價(jià)格表

        下面開始介紹不同需求下基于pandas和SQL的取數(shù)實(shí)現(xiàn)

        取出全部數(shù)據(jù)

        SQL實(shí)現(xiàn)

        select?*??from?Student;

        Pandas實(shí)現(xiàn)

        前N條數(shù)據(jù)

        SQL實(shí)現(xiàn)

        查看前5條數(shù)據(jù):

        Pandas實(shí)現(xiàn)

        head方法默認(rèn)是前5條:

        指定查看前7條數(shù)據(jù):

        后N條數(shù)據(jù)

        select?*?
        from?(select?*?from?Student?
        ??????order?by?s_id?desc?
        ??????limit?5)t???--?臨時(shí)結(jié)果表:倒序輸出的最后5條
        order?by?s_id;??--?再使用一次排序,將順序還原

        Pandas實(shí)現(xiàn)

        tail方法默認(rèn)是后5條:

        指定查看4條

        切片數(shù)據(jù)

        SQL實(shí)現(xiàn)

        Pandas實(shí)現(xiàn)

        使用pandas中的切片來查看某個連續(xù)區(qū)間內(nèi)的數(shù)據(jù):

        取出部分字段

        SQL實(shí)現(xiàn)

        Pandas實(shí)現(xiàn)

        df1[["id","name","sex"]]??#?方式1

        df2.filter(items=["id","age","createtime"])???#?方式2

        指定等式條件

        SQL實(shí)現(xiàn)

        Pandas實(shí)現(xiàn)

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

        指定id號或者年齡age:

        指定不等式條件

        SQL實(shí)現(xiàn)

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

        Pandas實(shí)現(xiàn)

        取反操作

        SQL實(shí)現(xiàn)

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

        Pandas實(shí)現(xiàn)

        指定多個條件

        SQL實(shí)現(xiàn)

        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實(shí)現(xiàn)

        指定計(jì)算等式

        SQL實(shí)現(xiàn)

        select?*?from?user?where?age?%?3?=?0;??--?年齡分別是3或者2的倍數(shù)
        select?*?from?user?where?age?%?2?=?0;??

        Pandas實(shí)現(xiàn)

        模糊查詢

        SQL實(shí)現(xiàn)

        SQL的關(guān)鍵詞是like:

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

        Pandas實(shí)現(xiàn)

        排序

        默認(rèn)是升序,可以指定為降序

        SQL實(shí)現(xiàn)

        1、單個字段

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

        2、多個字段的排序

        Pandas實(shí)現(xiàn)

        1、單個字段

        2、多個字段

        分組統(tǒng)計(jì)

        SQL實(shí)現(xiàn)

        通過group by 來進(jìn)行分組統(tǒng)計(jì):

        Pandas實(shí)現(xiàn)

        先看看df3的數(shù)據(jù),一個水果會對應(yīng)多個價(jià)格,我們水果的名稱對價(jià)格匯總:

        df3.groupby("name").agg({"price":"sum"}).reset_index()??#?方式1

        df3.groupby("name")["price"].sum().reset_index()???#?方式2

        取別名

        SQL實(shí)現(xiàn)

        通過使用as 關(guān)鍵詞:

        select?name?as??水果,?sum(price)?as??價(jià)格?from?products?group?by?name;

        Pandas實(shí)現(xiàn)

        Pandas是通過rename函數(shù)來實(shí)現(xiàn)的:

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

        推薦閱讀

        1. 搞到了!《網(wǎng)易數(shù)據(jù)分析指南》免費(fèi)領(lǐng)!

        2. 天花板級的數(shù)據(jù)分析項(xiàng)目,不愧是大廠特有的,吹爆!

        3. 分享用 Python 獲得 A股數(shù)據(jù)的 4 種方法



        瀏覽 42
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            色戒完整版2小时38分片集网 | 黄色小说免费 | 午夜天堂在线 | 爱爱三级视频 | 婷婷色色网 | 高清视频在线观看一区 | 中文字幕一区二区三区人妻在线视频 | 国产视频国产区 | 精品人妻无码一区二区三区91麻豆 | 鸡巴视频αV |