{"id":96978,"date":"2024-10-20T13:58:17","date_gmt":"2024-10-20T05:58:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96978\/"},"modified":"2024-10-20T13:58:18","modified_gmt":"2024-10-20T05:58:18","slug":"python-%e6%95%b8%e6%93%9a%e5%88%86%e6%9e%90%ef%bc%9a%e5%88%a9%e7%94%a8-pandas-%e5%92%8c-sql-%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-pandas-%e6%95%b8%e6%93%9a%e5%ba%ab-sql","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96978\/","title":{"rendered":"Python \u6578\u64da\u5206\u6790\uff1a\u5229\u7528 pandas \u548c SQL \u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c (pandas \u6578\u64da\u5eab sql)"},"content":{"rendered":"<h1 id=\"python-%e6%95%b8%e6%93%9a%e5%88%86%e6%9e%90%ef%bc%9a%e5%88%a9%e7%94%a8-pandas-%e5%92%8c-sql-%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-rshPhDWLOm\">Python \u6578\u64da\u5206\u6790\uff1a\u5229\u7528 pandas \u548c SQL \u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5206\u6790\u5df2\u6210\u70ba\u5404\u884c\u5404\u696d\u7684\u91cd\u8981\u7d44\u6210\u90e8\u5206\u3002Python \u4f5c\u70ba\u4e00\u7a2e\u5f37\u5927\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u56e0\u5176\u7c21\u55ae\u6613\u7528\u548c\u5f37\u5927\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u800c\u53d7\u5230\u5ee3\u6cdb\u6b61\u8fce\u3002\u7279\u5225\u662f\uff0cpandas \u5eab\u548c SQL \u8a9e\u8a00\u7684\u7d50\u5408\u4f7f\u5f97\u6578\u64da\u5eab\u64cd\u4f5c\u8b8a\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u9748\u6d3b\u3002<\/p>\n<h2 id=\"pandas-%e7%b0%a1%e4%bb%8b-rshPhDWLOm\">pandas \u7c21\u4ecb<\/h2>\n<p>pandas \u662f\u4e00\u500b\u958b\u6e90\u7684 Python \u6578\u64da\u5206\u6790\u5eab\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6578\u64da\u7d50\u69cb\u548c\u6578\u64da\u5206\u6790\u5de5\u5177\u3002\u5b83\u7684\u6838\u5fc3\u6578\u64da\u7d50\u69cb\u662f Series \u548c DataFrame\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u7684\u64cd\u4f5c\u8b8a\u5f97\u76f4\u89c0\u4e14\u9ad8\u6548\u3002<\/p>\n<ul>\n<li><strong>Series\uff1a<\/strong>\u4e00\u7dad\u6578\u7d44\uff0c\u985e\u4f3c\u65bc\u5217\u8868\u6216\u5b57\u5178\uff0c\u9069\u5408\u8655\u7406\u4e00\u7dad\u6578\u64da\u3002<\/li>\n<li><strong>DataFrame\uff1a<\/strong>\u4e8c\u7dad\u6578\u7d44\uff0c\u985e\u4f3c\u65bc\u96fb\u5b50\u8868\u683c\uff0c\u9069\u5408\u8655\u7406\u8868\u683c\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"sql-%e7%b0%a1%e4%bb%8b-rshPhDWLOm\">SQL \u7c21\u4ecb<\/h2>\n<p>SQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u662f\u4e00\u7a2e\u5c08\u9580\u7528\u65bc\u7ba1\u7406\u548c\u64cd\u4f5c\u95dc\u4fc2\u578b\u6578\u64da\u5eab\u7684\u8a9e\u8a00\u3002\u5b83\u5141\u8a31\u7528\u6236\u57f7\u884c\u67e5\u8a62\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u522a\u9664\u6578\u64da\u7b49\u64cd\u4f5c\u3002SQL \u7684\u8a9e\u6cd5\u7c21\u6f54\u660e\u4e86\uff0c\u4f7f\u5f97\u6578\u64da\u5eab\u64cd\u4f5c\u8b8a\u5f97\u9ad8\u6548\u3002<\/p>\n<h2 id=\"pandas-%e8%88%87-sql-%e7%9a%84%e7%b5%90%e5%90%88-rshPhDWLOm\">pandas \u8207 SQL \u7684\u7d50\u5408<\/h2>\n<p>\u5728\u6578\u64da\u5206\u6790\u904e\u7a0b\u4e2d\uff0c\u7d93\u5e38\u9700\u8981\u5f9e\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u6578\u64da\u4e26\u9032\u884c\u9032\u4e00\u6b65\u5206\u6790\u3002pandas \u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u8207 SQL \u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\uff0c\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 <code>pandas.read_sql()<\/code> \u51fd\u6578\u3002<\/p>\n<h3 id=\"%e4%bd%bf%e7%94%a8-pandas-%e9%80%b2%e8%a1%8c-sql-%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-rshPhDWLOm\">\u4f7f\u7528 pandas \u9032\u884c SQL \u6578\u64da\u5eab\u64cd\u4f5c<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 pandas \u9032\u884c SQL \u6578\u64da\u5eab\u64cd\u4f5c\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li><strong>\u5b89\u88dd\u5fc5\u8981\u7684\u5eab\uff1a<\/strong>\u9996\u5148\uff0c\u78ba\u4fdd\u5df2\u5b89\u88dd pandas \u548c SQLAlchemy\uff08\u7528\u65bc\u9023\u63a5\u6578\u64da\u5eab\u7684\u5eab\uff09\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/li>\n<pre><code>pip install pandas sqlalchemy<\/code><\/pre>\n<li><strong>\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\uff1a<\/strong>\u4f7f\u7528 SQLAlchemy \u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002<\/li>\n<pre><code>from sqlalchemy import create_engine\n\n# \u66ff\u63db\u70ba\u60a8\u7684\u6578\u64da\u5eab\u4fe1\u606f\nengine = create_engine('mysql+pymysql:\/\/username:password@localhost\/dbname')<\/code><\/pre>\n<li><strong>\u57f7\u884c SQL \u67e5\u8a62\uff1a<\/strong>\u4f7f\u7528 <code>pandas.read_sql()<\/code> \u51fd\u6578\u57f7\u884c SQL \u67e5\u8a62\u4e26\u5c07\u7d50\u679c\u52a0\u8f09\u5230 DataFrame \u4e2d\u3002<\/li>\n<pre><code>import pandas as pd\n\n# \u57f7\u884c\u67e5\u8a62\ndf = pd.read_sql('SELECT * FROM your_table', con=engine)<\/code><\/pre>\n<li><strong>\u6578\u64da\u5206\u6790\uff1a<\/strong>\u4e00\u65e6\u6578\u64da\u52a0\u8f09\u5230 DataFrame \u4e2d\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528 pandas \u63d0\u4f9b\u7684\u5404\u7a2e\u529f\u80fd\u9032\u884c\u6578\u64da\u5206\u6790\u3002<\/li>\n<pre><code># \u7372\u53d6\u6578\u64da\u7684\u57fa\u672c\u4fe1\u606f\nprint(df.info())\n\n# \u8a08\u7b97\u67d0\u5217\u7684\u5e73\u5747\u503c\naverage_value = df['your_column'].mean()\nprint(average_value)<\/code><\/pre>\n<\/ol>\n<h2 id=\"%e5%af%a6%e9%9a%9b%e6%87%89%e7%94%a8%e6%a1%88%e4%be%8b-rshPhDWLOm\">\u5be6\u969b\u61c9\u7528\u6848\u4f8b<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba <code>sales<\/code> \u7684\u6578\u64da\u8868\uff0c\u5305\u542b\u92b7\u552e\u6578\u64da\u3002\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 pandas \u548c SQL \u4f86\u5206\u6790\u9019\u4e9b\u6578\u64da\uff0c\u4f8b\u5982\u8a08\u7b97\u6bcf\u500b\u7522\u54c1\u7684\u7e3d\u92b7\u552e\u984d\u3002<\/p>\n<pre><code># \u67e5\u8a62\u6bcf\u500b\u7522\u54c1\u7684\u7e3d\u92b7\u552e\u984d\nquery = '''\nSELECT product_id, SUM(sales_amount) as total_sales\nFROM sales\nGROUP BY product_id\n'''\ntotal_sales_df = pd.read_sql(query, con=engine)\nprint(total_sales_df)<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u6211\u5011\u5c31\u80fd\u5920\u5feb\u901f\u7372\u5f97\u6bcf\u500b\u7522\u54c1\u7684\u92b7\u552e\u60c5\u6cc1\uff0c\u4e26\u9032\u4e00\u6b65\u9032\u884c\u6578\u64da\u53ef\u8996\u5316\u6216\u5831\u544a\u751f\u6210\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-rshPhDWLOm\">\u7e3d\u7d50<\/h2>\n<p>\u5229\u7528 Python \u7684 pandas \u5eab\u548c SQL \u8a9e\u8a00\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u8655\u7406\u7684\u6548\u7387\uff0c\u9084\u4f7f\u5f97\u6578\u64da\u5206\u6790\u8b8a\u5f97\u66f4\u52a0\u9748\u6d3b\u3002\u7121\u8ad6\u662f\u5f9e\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u6578\u64da\uff0c\u9084\u662f\u9032\u884c\u6df1\u5165\u7684\u6578\u64da\u5206\u6790\uff0c\u9019\u7a2e\u7d44\u5408\u90fd\u80fd\u5920\u6eff\u8db3\u5404\u7a2e\u9700\u6c42\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5206\u6790\u5de5\u4f5c\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528 Python \u7684 pandas \u548c SQL \u9032\u884c\u6578\u64da\u5206\u6790\uff0c\u8f15\u9b06\u8655\u7406\u548c\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u63d0\u5347\u6578\u64da\u6d1e\u5bdf\u529b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-96978","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=96978"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96978\/revisions"}],"predecessor-version":[{"id":96979,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96978\/revisions\/96979"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}