<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          14種方式,34個案例:對比SQL,學習Pandas操作

          共 1619字,需瀏覽 4分鐘

           ·

          2022-01-15 18:16

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

          后臺回復【大禮包】送你Python自學大禮包

          大家好,我是龍哥!

          本文主題:對比SQL,學習Pandas操作

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

          比較方向

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

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

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

          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、一份用戶表

          3、一份水果商品價格表

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

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

          SQL實現(xiàn)

          select?*??from?Student;

          Pandas實現(xiàn)

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

          SQL實現(xiàn)

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

          Pandas實現(xiàn)

          head方法默認是前5條:

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

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

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

          Pandas實現(xiàn)

          tail方法默認是后5條:

          指定查看4條

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

          SQL實現(xiàn)

          Pandas實現(xiàn)

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

          取出部分字段

          SQL實現(xiàn)

          Pandas實現(xiàn)

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

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

          指定等式條件

          SQL實現(xiàn)

          Pandas實現(xiàn)

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

          指定id號或者年齡age:

          指定不等式條件

          SQL實現(xiàn)

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

          Pandas實現(xiàn)

          取反操作

          SQL實現(xiàn)

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

          Pandas實現(xiàn)

          指定多個條件

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

          指定計算等式

          SQL實現(xiàn)

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

          Pandas實現(xiàn)

          模糊查詢

          SQL實現(xiàn)

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

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

          Pandas實現(xiàn)

          排序

          默認是升序,可以指定為降序

          SQL實現(xiàn)

          1、單個字段

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

          2、多個字段的排序

          Pandas實現(xiàn)

          1、單個字段

          2、多個字段

          分組統(tǒng)計

          SQL實現(xiàn)

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

          Pandas實現(xiàn)

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

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

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

          取別名

          SQL實現(xiàn)

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

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

          Pandas實現(xiàn)

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

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

          推薦閱讀

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

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

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



          瀏覽 42
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日本日逼视频 | 17c.白丝喷水 自慰 | 亚洲自拍欧美色图 | 91极品在线 | 乳交打奶炮泄精合集 |