{"id":13797989,"url":"https://github.com/yhat/pandasql","last_synced_at":"2025-05-14T05:12:11.599Z","repository":{"id":6995753,"uuid":"8259891","full_name":"yhat/pandasql","owner":"yhat","description":"sqldf for pandas","archived":false,"fork":false,"pushed_at":"2024-07-24T14:51:15.000Z","size":124,"stargazers_count":1347,"open_issues_count":61,"forks_count":185,"subscribers_count":48,"default_branch":"master","last_synced_at":"2025-05-08T03:03:12.450Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yhat.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.txt","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.md","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-02-18T01:53:56.000Z","updated_at":"2025-04-28T17:56:11.000Z","dependencies_parsed_at":"2023-02-16T16:01:28.822Z","dependency_job_id":"ea34fddd-5806-4aef-b892-058ca92a1202","html_url":"https://github.com/yhat/pandasql","commit_stats":{"total_commits":111,"total_committers":15,"mean_commits":7.4,"dds":0.5045045045045045,"last_synced_commit":"e799c6f53be9653e8998a25adb5e2f1643442699"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yhat%2Fpandasql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yhat%2Fpandasql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yhat%2Fpandasql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yhat%2Fpandasql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yhat","download_url":"https://codeload.github.com/yhat/pandasql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254076850,"owners_count":22010611,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T00:00:37.455Z","updated_at":"2025-05-14T05:12:06.586Z","avatar_url":"https://github.com/yhat.png","language":"Python","funding_links":[],"categories":["Data Manipulation","Other Libraries and Ports from R","Python (1887)","数据容器和结构","Python"],"sub_categories":["Data Frames","Higher Level APIs"],"readme":"pandasql\n========\n\n`pandasql` allows you to query `pandas` DataFrames using SQL syntax. It works \nsimilarly to `sqldf` in R. `pandasql` seeks to provide a more familiar way of \nmanipulating and cleaning data for people new to Python or `pandas`.\n\n#### Installation\n```\n$ pip install -U pandasql\n```\n\n#### Basics\nThe main function used in pandasql is `sqldf`. `sqldf` accepts 2 parametrs\n   - a sql query string\n   - a set of session/environment variables (`locals()` or `globals()`)\n\nSpecifying `locals()` or `globals()` can get tedious. You can define a short \nhelper function to fix this.\n\n    from pandasql import sqldf\n    pysqldf = lambda q: sqldf(q, globals())\n\n#### Querying\n`pandasql` uses [SQLite syntax](http://www.sqlite.org/lang.html). Any `pandas` \ndataframes will be automatically detected by `pandasql`. You can query them as \nyou would any regular SQL table.\n\n\n```\n$ python\n\u003e\u003e\u003e from pandasql import sqldf, load_meat, load_births\n\u003e\u003e\u003e pysqldf = lambda q: sqldf(q, globals())\n\u003e\u003e\u003e meat = load_meat()\n\u003e\u003e\u003e births = load_births()\n\u003e\u003e\u003e print pysqldf(\"SELECT * FROM meat LIMIT 10;\").head()\n                  date  beef  veal  pork  lamb_and_mutton broilers other_chicken turkey\n0  1944-01-01 00:00:00   751    85  1280               89     None          None   None\n1  1944-02-01 00:00:00   713    77  1169               72     None          None   None\n2  1944-03-01 00:00:00   741    90  1128               75     None          None   None\n3  1944-04-01 00:00:00   650    89   978               66     None          None   None\n4  1944-05-01 00:00:00   681   106  1029               78     None          None   None\n```\n\njoins and aggregations are also supported\n```\n\u003e\u003e\u003e q = \"\"\"SELECT\n        m.date, m.beef, b.births\n     FROM\n        meats m\n     INNER JOIN\n        births b\n           ON m.date = b.date;\"\"\"\n\u003e\u003e\u003e joined = pyqldf(q)\n\u003e\u003e\u003e print joined.head()\n                    date    beef  births\n403  2012-07-01 00:00:00  2200.8  368450\n404  2012-08-01 00:00:00  2367.5  359554\n405  2012-09-01 00:00:00  2016.0  361922\n406  2012-10-01 00:00:00  2343.7  347625\n407  2012-11-01 00:00:00  2206.6  320195\n\n\u003e\u003e\u003e q = \"select\n           strftime('%Y', date) as year\n           , SUM(beef) as beef_total\n           FROM\n              meat\n           GROUP BY\n              year;\"\n\u003e\u003e\u003e print pysqldf(q).head()\n   year  beef_total\n0  1944        8801\n1  1945        9936\n2  1946        9010\n3  1947       10096\n4  1948        8766\n```\n\nMore information and code samples available in the [examples](https://github.com/yhat/pandasql/blob/master/examples/demo.py)\n folder or on [our blog](http://blog.yhathq.com/posts/pandasql-sql-for-pandas-dataframes.html).\n\n\n\n[![Analytics](https://ga-beacon.appspot.com/UA-46996803-1/pandasql/README.md)](https://github.com/yhat/pandasql)    \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyhat%2Fpandasql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyhat%2Fpandasql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyhat%2Fpandasql/lists"}