pandasql
========
`pandasql` allows you to query `pandas` DataFrames using SQL syntax. It works
similarly to `sqldf` in R. `pandasql` seeks to provide a more familiar way of
manipulating and cleaning data for people new to Python or `pandas`.
####Installation
```
$ pip install -U pandasql
```
####Basics
The main function used in pandasql is `sqldf`. `sqldf` accepts 2 parametrs
- a sql query string
- an set of session/environment variables (`locals()` or `globals()`)
Specifying `locals()` or `globals()` can get tedious. You can defined a short
helper function to fix this.
from pandasql import sqldf
pysqldf = lambda q: sqldf(q, globals())
####Querying
`pandasql` uses [SQLite syntax](http://www.sqlite.org/lang.html). Any `pandas`
dataframes will be automatically detected by `pandasql`. You can query them as
you would any regular SQL table.
```
$ python
>>> from pandasql import sqldf, load_meat, load_births
>>> pysqldf = lambda q: sqldf(q, globals())
>>> meat = load_meat()
>>> births = load_births()
>>> print pysqldf("SELECT * FROM meat LIMIT 10;").head()
date beef veal pork lamb_and_mutton broilers other_chicken turkey
0 1944-01-01 00:00:00 751 85 1280 89 None None None
1 1944-02-01 00:00:00 713 77 1169 72 None None None
2 1944-03-01 00:00:00 741 90 1128 75 None None None
3 1944-04-01 00:00:00 650 89 978 66 None None None
4 1944-05-01 00:00:00 681 106 1029 78 None None None
```
joins and aggregations are also supported
```
>>> q = """SELECT
m.date, m.beef, b.births
FROM
meats m
INNER JOIN
births b
ON m.date = b.date;"""
>>> joined = pyqldf(q)
>>> print joined.head()
date beef births
403 2012-07-01 00:00:00 2200.8 368450
404 2012-08-01 00:00:00 2367.5 359554
405 2012-09-01 00:00:00 2016.0 361922
406 2012-10-01 00:00:00 2343.7 347625
407 2012-11-01 00:00:00 2206.6 320195
>>> q = "select
strftime('%Y', date) as year
, SUM(beef) as beef_total
FROM
meat
GROUP BY
year;"
>>> print pysqldf(q).head()
year beef_total
0 1944 8801
1 1945 9936
2 1946 9010
3 1947 10096
4 1948 8766
```
More information and code samples available in the [examples](https://github.com/yhat/pandasql/blob/master/examples/demo.py)
folder or on [our blog](http://blog.yhathq.com/posts/pandasql-sql-for-pandas-dataframes.html).
[](https://github.com/yhat/pandasql)

程序员Chino的日记
- 粉丝: 4259
最新资源
- 直线导轨穿梭车 多层穿梭车使用说明书 、主要技术参数、结构与工作原理、系统说明、操作模式、故障处理及其他异常现象
- 一个简单方便的目标检测框架(PyTorch环境可直接运行,不需要cuda编译),支持Faster-RCNN、Cascade-RCNN、Yolo系列、SSD等经典网络
- RISTDnet:强鲁棒性的红外小目标检测网络
- 基于 YOLO v2 的目标检测系统:可检测图像与视频,适用于公路及实验室场景
- 【Python编程教育】Python基础编程实验:环境搭建、语法掌握及常用库应用实践
- 2020 年中兴捧月阿尔法赛道多目标检测与跟踪初赛冠军方案
- 基于 Halcon 深度学习的分类、目标及缺陷检测
- 2018 至 2019 年目标检测领域论文汇总
- halcon的DeepLearning的分类、目标、缺陷检测
- 2018-2019 年度目标检测领域相关论文汇总整理
- 2021 年和鲸社区 Kesci(湛江)水下目标检测算法赛光学图像赛项
- ROS 机器人系统课程设计(自主导航+YOLO目标检测+语音播报)
- CenterNet 纯版本:便于二次开发且易于理解的目标检测与关键点检测工具
- python实现支持向量机分类器与核函数方法
- 使用 onnxruntime 部署 GroundingDINO 开放世界目标检测的 C++ 与 Python 双版本程序
- 使用 onnxruntime 部署 GroundingDINO 开放世界目标检测的 C++ 与 Python 双版本程序
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


