Skip to main content

sqldf for pandas

Project description

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. 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 folder or on our blog.

Analytics

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pandasql-0.6.3.tar.gz (24.7 kB view details)

Uploaded Source

Built Distribution

pandasql-0.6.3-py2.7.egg (28.3 kB view details)

Uploaded Egg

File details

Details for the file pandasql-0.6.3.tar.gz.

File metadata

  • Download URL: pandasql-0.6.3.tar.gz
  • Upload date:
  • Size: 24.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pandasql-0.6.3.tar.gz
Algorithm Hash digest
SHA256 6717a0478db77c64782d4cdf5622bd8c510de986b3ddccfa515fdc1fb469c3ba
MD5 b6b8bded04d8df5876ab7b8fd8161e21
BLAKE2b-256 c061a714ff457c13d04699713b5b936ab276eda06cfb59b21d41921bfb6da4fd

See more details on using hashes here.

File details

Details for the file pandasql-0.6.3-py2.7.egg.

File metadata

  • Download URL: pandasql-0.6.3-py2.7.egg
  • Upload date:
  • Size: 28.3 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pandasql-0.6.3-py2.7.egg
Algorithm Hash digest
SHA256 ae1e21d8a05fa386580dc9d3aa4a7d799f9f52599ff65a9b7d0716e846ac01fa
MD5 6817bb3ee2cc0187e14447113260ce46
BLAKE2b-256 2d113aa2345b02475a2ad737e6bb70982c4e361b3ec75dbb3c43a219ddf7d81d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page