A simple way to run SQL queries (SQLite3) on pandas.Dataframe objects.
Project description
SQLDF - Structured Query Language (SQL) on DataFrames (DF)
A simple way to run SQL (SQLite) queries on pandas.Dataframe objects.
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
Requirements
- 'pandas>=1.0'
Installation
With pip
(on pypi):
pip install sqldf
Examples of use
- SELECT query with WHERE condition
# Import libraries
import pandas as pd
import numpy as np
from sqldf import run_query
# 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
sql_query = """
SELECT *
FROM df
WHERE col_1 IS NOT NULL;
"""
# Run the query
df_view = run_query(sql_query)
- UPDATE query that change inplace the value of a column
# Import libraries
import pandas as pd
from sqldf import run_query
# 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
sql_query = """
UPDATE tips
SET tip = tip*2
WHERE tip < 2;
"""
# Run the query
run_query(sql_query)
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
sqldf-0.3.2.tar.gz
(3.9 kB
view hashes)
Built Distribution
sqldf-0.3.2-py3-none-any.whl
(4.0 kB
view hashes)