网站首页 > 技术文章 正文
作者:俊欣
来源:关于数据分析与可视化
对于数据分析师而言,Pandas与SQL可能是大家用的比较多的两个工具,两者都可以对数据集进行深度的分析,挖掘出有价值的信息,但是二者的语法有着诸多的不同,今天小编就来总结归纳一下Pandas与SQL这两者之间在语法上到底有哪些不同。
导入数据
对于Pandas而言,我们需要提前导入数据集,然后再进行进一步的分析与挖掘
import pandas as pd
airports = pd.read_csv('data/airports.csv')
airport_freq = pd.read_csv('data/airport-frequencies.csv')
runways = pd.read_csv('data/runways.csv')
基础语法
在SQL当中,我们用SELECT来查找数据,WHERE来过滤数据,DISTINCT来去重,LIMIT来限制输出结果的数量,
输出数据集
## SQL
select * from airports
## Pandas
airports
输出数据集的前三行数据,代码如下
## SQL
select * from airports limit 3
## Pandas
airports.head(3)
对数据集进行过滤筛查
## SQL
select id from airports where ident = 'KLAX'
## Pandas
airports[airports.ident == 'KLAX'].id
对于筛选出来的数据进行去重
## SQL
select distinct type from airport
## Pandas
airports.type.unique()
多个条件交集来筛选数据
多个条件的交集来筛选数据,代码如下
## SQL
select * from airports
where iso_region = 'US-CA' and
type = 'seaplane_base'
## Pandas
airports[(airports.iso_region == 'US-CA') &
(airports.type == 'seaplane_base')]
或者是
## SQL
select ident, name, municipality from airports
where iso_region = 'US-CA' and
type = 'large_airport'
## Pandas
airports[(airports.iso_region == 'US-CA') &
(airports.type == 'large_airport')][['ident', 'name', 'municipality']]
排序
在Pandas当中默认是对数据进行升序排序,要是我们希望对数据进行降序排序,需要设定ascending参数
## SQL
select * from airport_freq
where airport_ident = 'KLAX'
order by type
## Pandas
airport_freq[airport_freq.airport_ident == 'KLAX']
.sort_values('type')
又或者是
## SQL
select * from airport_freq
where airport_ident = 'KLAX'
order by type desc
## Pandas
airport_freq[airport_freq.airport_ident == 'KLAX']
.sort_values('type', ascending=False)
筛选出列表当中的数据
要是我们需要筛选出来的数据在一个列表当中,这里就需要用到isin()方法,代码如下
## SQL
select * from airports
where type in ('heliport', 'balloonport')
## Pandas
airports[airports.type.isin(['heliport', 'balloonport'])]
又或者是
## SQL
select * from airports
where type not in ('heliport', 'balloonport')
## Pandas
airports[~airports.type.isin(['heliport', 'balloonport'])]
删除数据
在Pandas当中删除数据用的是drop()方法,代码如下
## SQL
delete from dataframe where col_name = 'MISC'
## Pandas
df = df[df.type != 'MISC']
df.drop(df[df.type == 'MISC'].index)
更新数据
在SQL当中更新数据使用的是update和set方法,代码如下
### SQL
update airports set home_link = '......'
where ident == 'KLAX'
### Pandas
airports.loc[airports['ident'] == 'KLAX', 'home_link'] = '......'
调用统计函数
对于给定的数据集,如下图所示
runways.head()
output
我们调用min()、max()、mean()以及median()函数作用于length_ft这一列上面,代码如下
## SQL
select max(length_ft), min(length_ft),
avg(length_ft), median(length_ft) from runways
## Pandas
runways.agg({'length_ft': ['min', 'max', 'mean', 'median']})
合并两表格
在Pandas当中合并表格用的是pd.concat()方法,在SQL当中则是UNION ALL,代码如下
## SQL
select name, municipality from airports
where ident = 'KLAX'
union all
select name, municipality from airports
where ident = 'KLGB'
## Pandas
pd.concat([airports[airports.ident == 'KLAX'][['name', 'municipality']],
airports[airports.ident == 'KLGB'][['name', 'municipality']]])
分组
顾名思义也就是groupby()方法,代码如下
## SQL
select iso_country, type, count(*) from airports
group by iso_country, type
order by iso_country, type
## Pandas
airports.groupby(['iso_country', 'type']).size()
分组之后再做筛选
在Pandas当中是在进行了groupby()之后调用filter()方法,而在SQL当中则是调用HAVING方法,代码如下
## SQL
select type, count(*) from airports
where iso_country = 'US'
group by type
having count(*) > 1000
order by count(*) desc
## Pandas
airports[airports.iso_country == 'US']
.groupby('type')
.filter(lambda g: len(g) > 1000)
.groupby('type')
.size()
.sort_values(ascending=False)
TOP N records
代码如下
## SQL
select 列名 from 表名
order by size
desc limit 10
## Pandas
表名.nlargest(10, columns='列名')
猜你喜欢
- 2024-10-20 常用SQL系列之(八):列值累计、占比、平均值以及日期运算等
- 2024-10-20 选读SQL经典实例笔记02_多表查询(sql语句多表查询案例)
- 2024-10-20 不知道怎么分析MySQL查询瓶颈,这款自带工具太香了,强烈推荐
- 2024-10-20 怎么找出连续5天登录的用户,大数据面试题sql
- 2024-10-20 Pandas与SQL的数据操作语句对照(pandas和sql哪个速度快)
- 2024-10-20 「Excel」筛选不重复的数据—Part1
- 2024-10-20 Excel零基础学SQL22:中式排名,美式排名,分组排名
- 2024-10-20 MySql基础使用「增删改查」20211221
- 2024-10-20 Excel多条件不重复计数,4种方法,总有一种适合你
- 2024-10-20 hive select 语法使用详解(hive select * from)
- 1509℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 527℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 492℃MySQL service启动脚本浅析(r12笔记第59天)
- 472℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 469℃启用MySQL查询缓存(mysql8.0查询缓存)
- 450℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 429℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 426℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)