Skip to main content

A simple way to run SQL queries (SQLite3) on pandas.Dataframe objects.

Project description

SQLDF - Structured Query Language (SQL) on DataFrames (DF)

A simple wrapper to run SQL (SQLite) queries on pandas.DataFrame objects (Python).

Requirements

  • 'python' >= 3.5
  • 'pandas' >= 1.0

Installation

With pip (from PyPI repository):

pip install sqldf

Examples of use

  • SELECT query with WHERE condition
# Import libraries
import pandas as pd
import numpy as np
import sqldf

# Create a dummy pd.Dataframe
df = pd.DataFrame({'col1': ['A', 'B', np.NaN, 'C', 'D'], 'col2': ['F', np.NaN, 'G', 'H', 'I']})

# Define a SQL (SQLite3) query
query = """
SELECT *
FROM df
WHERE col_1 IS NOT NULL;
"""

# Run the query
df_view = sqldf.run(query)
  • UPDATE query that change inplace a pd.Dataframe
# Import libraries
import pandas as pd
import sqldf

# Create a dummy pd.Dataframe
url = ('https://raw.github.com/pandas-dev/pandas/master/pandas/tests/data/tips.csv')
tips = pd.read_csv(url)

# Define a SQL (SQLite3) query
query = """
UPDATE tips
SET tip = tip*2
WHERE tip < 2;
"""

# Run the query
sqldf.run(query)

How it works

  1. It create a virtual in-memory SQLite3 database at runtime
  2. It convert the pd.DataFrame input(s) to SQL table(s)
  3. It proceed the SQL query on the table(s)
  4. It convert back the SQL table(s) to updated pd.DataFrame(s) if required
  5. It returns the result of the query if required

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

sqldf-0.4.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

sqldf-0.4.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file sqldf-0.4.2.tar.gz.

File metadata

  • Download URL: sqldf-0.4.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for sqldf-0.4.2.tar.gz
Algorithm Hash digest
SHA256 5e8ed19f3ce2c8eb08fe9ea28afecfa1db50d94e035ab0f264df19ec006b100b
MD5 c3d7cdf7ae221607b0419c71f2d222fe
BLAKE2b-256 e5fe7fcd0546dd9266d1ff136c573d184f05c7402a2b3c2f78ec02996208daaa

See more details on using hashes here.

File details

Details for the file sqldf-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: sqldf-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for sqldf-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 713e3bcce65d11658051b33a99e2504d8882cd254622344a3d6a3997cd18c641
MD5 3fb84fdf6690984df9f6a3a4c97e683c
BLAKE2b-256 604b1a9f9f65bcc7cd8d724c700f469f9bbe3e796b3543e0d346e3e352a283d7

See more details on using hashes here.

Supported by

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