公眾號:尤而小屋
作者:Peter
編輯:Peter
大家好罗洗,我是Peter~
本文主題:對比SQL愉舔,學(xué)習(xí)Pandas操作!
在SQL中查詢數(shù)據(jù)的時候我們所有各種操作伙菜,主要是通過select轩缤、where、group by等多個關(guān)鍵詞的組合查詢來實現(xiàn)的贩绕。本文中介紹的如何在相同的需求下火的,通過pandas來實現(xiàn)取數(shù)操作。
image
比較方向
- 查詢?nèi)繑?shù)據(jù)
- 前N條
- 后N條
- 中間段數(shù)據(jù)
- 部分字段
- 指定等式條件
- 指定不等式條件
- 取反操作
- 指定多個條件
- 指定計算等式
- 模糊查詢
- 排序
- 分組統(tǒng)計
- 取別名
參考資料
因為本文主要介紹的是如何通過pandas來獲取我們想要的數(shù)據(jù)淑倾,也是pandas的各種取數(shù)技巧馏鹤,參考之前介紹的3篇文章:
模擬數(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湃累、一份用戶表
image
3勃救、一份水果商品價格表
image
下面開始介紹不同需求下基于pandas和SQL的取數(shù)實現(xiàn)
取出全部數(shù)據(jù)
SQL實現(xiàn)
select * from Student;
image
Pandas實現(xiàn)
image
前N條數(shù)據(jù)
SQL實現(xiàn)
查看前5條數(shù)據(jù):
image
image
Pandas實現(xiàn)
head方法默認(rèn)是前5條:
image
指定查看前7條數(shù)據(jù):
image
后N條數(shù)據(jù)
select *
from (select * from Student
order by s_id desc
limit 5)t -- 臨時結(jié)果表:倒序輸出的最后5條
order by s_id; -- 再使用一次排序,將順序還原
image
Pandas實現(xiàn)
tail方法默認(rèn)是后5條:
image
指定查看4條
image
切片數(shù)據(jù)
SQL實現(xiàn)
image
Pandas實現(xiàn)
使用pandas中的切片來查看某個連續(xù)區(qū)間內(nèi)的數(shù)據(jù):
image
取出部分字段
SQL實現(xiàn)
image
Pandas實現(xiàn)
df1[["id","name","sex"]] # 方式1
df2.filter(items=["id","age","createtime"]) # 方式2
image
指定等式條件
SQL實現(xiàn)
image
image
Pandas實現(xiàn)
df1[df1["sex"] == "男"] # 方式1
df1.query('sex=="男"') # 方式2
image
指定id號或者年齡age:
image
指定不等式條件
SQL實現(xiàn)
select * from Student where s_sex!= "男";
select * from user where age > 18;
select * from user where id <= 3;
image
Pandas實現(xiàn)
image
image
image
取反操作
SQL實現(xiàn)
mysql> select * from Student where s_sex != "男";
image
Pandas實現(xiàn)
image
image
image
指定多個條件
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)
image
指定計算等式
SQL實現(xiàn)
select * from user where age % 3 = 0; -- 年齡分別是3或者2的倍數(shù)
select * from user where age % 2 = 0;
image
Pandas實現(xiàn)
image
image
模糊查詢
SQL實現(xiàn)
SQL的關(guān)鍵詞是like:
- 左匹配
- 右匹配
- 全匹配
image
Pandas實現(xiàn)
image
image
排序
默認(rèn)是升序脱茉,可以指定為降序
SQL實現(xiàn)
1剪芥、單個字段
image
select * from Student order by s_birth desc; -- 改成升序
2、多個字段的排序
image
Pandas實現(xiàn)
1琴许、單個字段
image
image
2税肪、多個字段
image
分組統(tǒng)計
SQL實現(xiàn)
通過group by 來進(jìn)行分組統(tǒng)計:
image
Pandas實現(xiàn)
先看看df3的數(shù)據(jù),一個水果會對應(yīng)多個價格榜田,我們水果的名稱對價格匯總:
image
df3.groupby("name").agg({"price":"sum"}).reset_index() # 方式1
df3.groupby("name")["price"].sum().reset_index() # 方式2
image
取別名
SQL實現(xiàn)
通過使用as 關(guān)鍵詞:
select name as 水果, sum(price) as 價格 from products group by name;
image
Pandas實現(xiàn)
Pandas是通過rename函數(shù)來實現(xiàn)的:
df3.groupby("name").agg({"price":"sum"}).reset_index().rename(columns={"name":"水果","price":"價格"})
image