内容简介:本篇将解构下面的本篇将解构下面的
本篇将解构下面的 SQL 查询句式, 使用 Pandas 进行实现
SQL 查询句式
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) 复制代码
数据预览
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 |
JOIN
(INNER) JOIN
SQL
SELECT * FROM student INNER JOIN comments ON student.id = comments.student_id; 复制代码
Pandas
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 [过滤条件]; 复制代码
CREATE
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']) 复制代码
INSERT
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() 复制代码
UPDATE
SQL
UPDATE student SET money = 300 WHERE id = 1; 复制代码
Pandas
df.loc[df.id == 1, 'money'] = 300 复制代码
以上所述就是小编给大家介绍的《【Python】SQL to Pandas 速查表(二)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Algorithms for Image Processing and Computer Vision
Parker, J. R. / 2010-12 / 687.00元
A cookbook of algorithms for common image processing applications Thanks to advances in computer hardware and software, algorithms have been developed that support sophisticated image processing with......一起来看看 《Algorithms for Image Processing and Computer Vision》 这本书的介绍吧!