Package: pysqldf
pysqldfallows you to querypandasDataFrames using SQL syntax.
It works similarly tosqldfin R.pysqldfseeks to provide a
more familiar way of manipulating and cleaning data for people new to
Python orpandas.Installation$ pip install pysqldfBasicsThe main class in pysqldf isSQLDF.SQLDFaccepts 1 enviroment
variable sets or more parametrs in constructor. - an set of
session/environment variables (dictionary of valiables,locals()orglobals()) - temporary file type - user defined functions - user
defined aggregate functionspysqldfusesSQLite syntax.
Any convertable data topandasDataFrames will be automatically
detected bypysqldf. You can query them as you would any regular SQL
table.$python>>>frompysqldfimportSQLDF,load_meat,load_births>>>sqldf=SQLDF(globals())>>>meat=load_meat()>>>births=load_births()>>>printsqldf.execute("SELECT * FROM meat LIMIT 10;").head()datebeefvealporklamb_and_muttonbroilersother_chickenturkey01944-01-0100:00:0075185128089NoneNoneNone11944-02-0100:00:0071377116972NoneNoneNone21944-03-0100:00:0074190112875NoneNoneNone31944-04-0100:00:006508997866NoneNoneNone41944-05-0100:00:00681106102978NoneNoneNone>>>q="SELECT m.date, m.beef, b.births FROM meat m INNER JOIN births b ON m.date = b.date;">>>printsqldf.execute(q).head()datebeefbirths4032012-07-0100:00:002200.83684504042012-08-0100:00:002367.53595544052012-09-0100:00:002016.03619224062012-10-0100:00:002343.73476254072012-11-0100:00:002206.6320195>>>q="SELECT strftime('%Y', date) AS year, SUM(beef) AS beef_total FROM meat GROUP BY year;">>>printsqldf.execute(q).head()yearbeef_total0194488011194599362194690103194710096419488766user defined functions and user defined aggregate functions also
supported.$python>>>frompysqldfimportSQLDF,load_iris>>>importmath>>>importnumpy>>>ceil=lambdax:math.ceil(x)>>>udfs={"ceil":lambdax:math.ceil(x)}>>>udafs={"variance":lambdavalues:numpy.var(values)}>>># or you can also define aggregation function as class>>># class variance(object):...#     def __init__(self):...#         self.a = []...#     def step(self, x):...#         self.a.append(x)...#     def finalize(self):...#         return numpy.var(self.a)...>>># udafs={ "variance": variance }>>>iris=load_iris()>>>sqldf=SQLDF(globals(),udfs=udfs,udafs=udafs)>>>sqldf.execute("""
    SELECT
        ceil(sepal_length) AS sepal_length,
        ceil(sepal_width) AS sepal_width,
        ceil(petal_length) AS petal_length,
        ceil(petal_width) AS petal_width,
        species
    FROM iris;
    """).head()sepal_lengthsepal_widthpetal_lengthpetal_widthspecies06421Iris-setosa15321Iris-setosa25421Iris-setosa35421Iris-setosa45421Iris-setosa>>>sqldf.execute("SELECT species, variance(sepal_width) AS var FROM iris GROUP BY species;")speciesvar0Iris-setosa0.1422761Iris-versicolor0.0965002Iris-virginica0.101924DocumentsSQLDF(env, inmemory=True,udfs={},udafs={})env: variable mapping dictionary of sql executed enviroment. key is
sql variable name and value is your program variable.locals()orglobals()is used for simple assign.inmemory: sqlite db option.udfs: dictionary of user defined functions. dictionary key is
function name, dictionary value is function. seesqlite3
documentudafs: dictionary of user defined aggregate functions. dictionary
key is function name, dictionary value is aggregate function or class.
If value is function, function gets one argument that is list of column
values and it should return aggregated a value. Another case(value is
class), seesqlite3
document.load_iris(),load_meat(),load_births()load example DataFrame data.iris:data
descriptionmeat:data
descriptionbirths:data
description