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)
- More examples in the notebook: Demonstration notebook for SQLDF
How it works
- It create a virtual in-memory SQLite3 database at runtime
- It convert the pd.DataFrame input(s) to SQL table(s)
- It proceed the SQL query on the table(s)
- It convert back the SQL table(s) to updated pd.DataFrame(s) if required
- It returns the result of the query if required
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)
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
sqldf-0.4.2-py3-none-any.whl
(4.3 kB
view details)
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e8ed19f3ce2c8eb08fe9ea28afecfa1db50d94e035ab0f264df19ec006b100b
|
|
| MD5 |
c3d7cdf7ae221607b0419c71f2d222fe
|
|
| BLAKE2b-256 |
e5fe7fcd0546dd9266d1ff136c573d184f05c7402a2b3c2f78ec02996208daaa
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
713e3bcce65d11658051b33a99e2504d8882cd254622344a3d6a3997cd18c641
|
|
| MD5 |
3fb84fdf6690984df9f6a3a4c97e683c
|
|
| BLAKE2b-256 |
604b1a9f9f65bcc7cd8d724c700f469f9bbe3e796b3543e0d346e3e352a283d7
|