sqldf for pandas
Project description
pandasql
This is a fork of the original pandasql, with support of multiple SQL
backends and more convenient interface. See below for more info.
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
In addition to the original pandasql's sqldf function this fork has
a class PandaSQL, which new users are encouraged to use.
PandaSQL Class
The class is more convenient when you need to perform multiple queries. PandaSQL takes 2 arguments:
db_uri: an optional SQLAlchemy connection string (defaults to in-memory SQLite database)persist: an optional boolean to determine if loaded tables are persisted in the database (holds connection open, default False)
sqldf Function
The main function used in pandasql is sqldf. sqldf accepts 3 parameters:
- an sql query string
- an optional SQLAlchemy connection string (defaults to in-memory SQLite database)
- an optional dict of session/environment variables (defaults to
**locals(),**globals())
Querying
pandasql uses SQLite syntax. Any pandas
dataframes will be automatically detected by pandasql. You can query them as
you would any regular SQL table.
$ python
>>> from pandasql import PandaSQL, load_meat, load_birth
>>> meat = load_meat()
>>> births = load_births()
>>> pdsql = PandaSQL()
>>> 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 Info
More information and code samples available in the examples folder.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pandas_query_sql-0.8.2.tar.gz.
File metadata
- Download URL: pandas_query_sql-0.8.2.tar.gz
- Upload date:
- Size: 25.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"44","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb22a6599c10f0d01477092665d2773aa5874c72cc94d22a14c1274ce016e891
|
|
| MD5 |
755afd511fbf8f03f413663f5e7c7f53
|
|
| BLAKE2b-256 |
495709edb93c6eab151779008df95ab580472eec98ff92802fe4697cb3299568
|
File details
Details for the file pandas_query_sql-0.8.2-py3-none-any.whl.
File metadata
- Download URL: pandas_query_sql-0.8.2-py3-none-any.whl
- Upload date:
- Size: 25.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"44","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7fe5ca7203790f67384a4260cf12e1f9a59bdde8b2a98001a9812206fbc2f8b
|
|
| MD5 |
167519458a0f80439e185031127e81e0
|
|
| BLAKE2b-256 |
5a2108cd4d575f96c2d4bb019e59a5821a50ff82bb155d77003f2c5df81d332a
|