原文地址python
此文爲 SQL to Pandas 系列第二篇sql
可先閱讀post
【Python】當數據離開了 SQL 該怎麼活下去(一)測試
本篇將解構下面的 SQL 查詢句式, 使用 Pandas 進行實現ui
SQL 查詢句式spa
SELECT DISTINCT [字段]
FROM [表] JOIN [bin] ON [鏈接條件]
WHERE [過濾條件]
GROUP BY [字段]
HAVING [條件]
ORDER BY [字段] DESC
LIMIT [個數] OFFSET [個數]
複製代碼
df_comments = pd.read_sql(sql="select * from comments", con=conn)
複製代碼
數據預覽code
df_comments
複製代碼
id | student_id | content | |
---|---|---|---|
0 | 1 | 1 | 測試評論1 |
1 | 2 | 5 | 測試評論5 |
2 | 3 | 2 | 測試評論2 |
3 | 4 | 3 | 測試評論3 |
4 | 5 | 1 | 測試評論11 |
5 | 6 | 9 | 測試評論9 |
(INNER) JOINcdn
SQLci
SELECT
*
FROM
student
INNER JOIN comments ON student.id = comments.student_id;
複製代碼
Pandasget
pd.merge(df, df_comments, left_on='id', right_on='student_id')
複製代碼
LEFT (OUTER) JOIN
SQL
SELECT
*
FROM
student
LEFT JOIN comments ON student.id = comments.student_id;
複製代碼
Pandas
pd.merge(df, df_comments, left_on='id', right_on='student_id', how='left')
複製代碼
RIGHT (OUTER) JOIN
SQL
SELECT
*
FROM
student
RIGHT JOIN comments ON student.id = comments.student_id;
複製代碼
Pandas
pd.merge(df, df_comments, left_on='id', right_on='student_id', how='right')
複製代碼
UNION
SQL
SELECT * FROM student where city ='北京'
UNION
SELECT * FROM student where sex ='男';
複製代碼
Pandas
pd.concat([df[df.city == '北京'], df[df.sex == '男']]).drop_duplicates().reset_index()
複製代碼
UNION ALL
SQL
SELECT * FROM student where city ='北京'
UNION ALL
SELECT * FROM student where sex ='男';
複製代碼
Pandas
pd.concat([df[df.city == '北京'], df[df.sex == '男']]).reset_index()
複製代碼
本篇將解構下面的 SQL 查詢句式, 使用 Pandas 進行實現
SQL 建立句式
CREATE TABLE [表名] (
[列名] [類型],
[列名] [類型],
....
);
複製代碼
SQL 插入句式
INSERT INTO [表名] VALUES ([值], [值], ...);
INSERT INTO [表名] ([列名],[列名] ...) VALUES ([值], [值], ...);
複製代碼
SQL 更新句式
UPDATE [表名]
SET [列名] = [值], [列名] = [值]
WHERE [過濾條件];
複製代碼
SQL 刪除句式
DELETE FROM [表名] WHERE [過濾條件];
複製代碼
SQL
CREATE TABLE student (
id INT ( 11 ) NOT NULL AUTO_INCREMENT,
name VARCHAR ( 10 ) COLLATE utf8mb4_general_ci DEFAULT NULL,
age date DEFAULT NULL,
sex VARCHAR ( 10 ) COLLATE utf8mb4_general_ci DEFAULT NULL,
city VARCHAR ( 255 ) CHARACTER
SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
money DOUBLE ( 255, 2 ) DEFAULT NULL,
);
複製代碼
Pandas
pd.DataFrame(columns=['id', 'name', 'sex', 'city', 'money'])
複製代碼
SQL
INSERT INTO student (id, name, age, sex, city, money )
VALUES
(1, '張三', '2017-12-20', '女', '天津', 20.00 );
複製代碼
Pandas
# 第一種
df.loc[-1] = [1, '張三', '女', '天津', 20.00]
df.index = df.index + 1
df = df.sort_index()
# 第二種
temp_pd = pd.DataFrame({'id': [1], 'name': ['張三'], 'sex': ['女'], 'city': ['天津'], 'money': [20.00]})
df = pd.concat([df,temp_pd], ignore_index=True)
df.reset_index()
# 第三種
temp_pd = pd.DataFrame([[1,'張三1', '女', '天津', 20.00]], columns=df.columns)
df = pd.concat([df, temp_pd])
df.reset_index()
複製代碼
SQL
UPDATE student SET money = 300 WHERE id = 1;
複製代碼
Pandas
df.loc[df.id == 1, 'money'] = 300
複製代碼
SQL
-- 測試時會因外鍵報錯,此處忽略,僅討論句法
DELETE FROM student WHERE id = 1;
複製代碼
Pandas
df = df.loc[df.id != 1]
複製代碼
系列文章