{"id":96350,"date":"2024-10-20T08:41:16","date_gmt":"2024-10-20T00:41:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96350\/"},"modified":"2024-10-20T08:41:17","modified_gmt":"2024-10-20T00:41:17","slug":"pandas%e8%bc%95%e9%ac%86%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e7%b0%a1%e5%8c%96%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86-pandas-%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96350\/","title":{"rendered":"Pandas\u8f15\u9b06\u8b80\u53d6\u6578\u64da\u5eab\uff0c\u7c21\u5316\u6578\u64da\u8655\u7406 (pandas \u8b80\u53d6\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"pandas%e8%bc%95%e9%ac%86%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e7%b0%a1%e5%8c%96%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86-VSYmUFeVkP\">Pandas\u8f15\u9b06\u8b80\u53d6\u6578\u64da\u5eab\uff0c\u7c21\u5316\u6578\u64da\u8655\u7406<\/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\u7684Pandas\u5eab\u56e0\u5176\u5f37\u5927\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u7279\u5225\u662f\u7576\u6d89\u53ca\u5230\u5f9e\u6578\u64da\u5eab\u4e2d\u8b80\u53d6\u6578\u64da\u6642\uff0cPandas\u63d0\u4f9b\u4e86\u7c21\u55ae\u800c\u9ad8\u6548\u7684\u65b9\u6cd5\u4f86\u7c21\u5316\u9019\u4e00\u904e\u7a0b\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87pandas%e4%be%86%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab-VSYmUFeVkP\">\u70ba\u4ec0\u9ebc\u9078\u64c7Pandas\u4f86\u8b80\u53d6\u6578\u64da\u5eab<\/h2>\n<p>Pandas\u4e0d\u50c5\u662f\u4e00\u500b\u6578\u64da\u5206\u6790\u5de5\u5177\uff0c\u5b83\u9084\u80fd\u5920\u8f15\u9b06\u5730\u8207\u5404\u7a2e\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u7121\u8ad6\u662fMySQL\u3001PostgreSQL\u9084\u662fSQLite\uff0cPandas\u90fd\u80fd\u5920\u901a\u904e\u7c21\u55ae\u7684API\u4f86\u8b80\u53d6\u548c\u8655\u7406\u6578\u64da\u3002\u9019\u4f7f\u5f97\u6578\u64da\u79d1\u5b78\u5bb6\u548c\u5206\u6790\u5e2b\u80fd\u5920\u5c08\u6ce8\u65bc\u6578\u64da\u5206\u6790\uff0c\u800c\u4e0d\u5fc5\u82b1\u8cbb\u904e\u591a\u6642\u9593\u5728\u6578\u64da\u63d0\u53d6\u4e0a\u3002<\/p>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8pandas%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab-VSYmUFeVkP\">\u5982\u4f55\u4f7f\u7528Pandas\u8b80\u53d6\u6578\u64da\u5eab<\/h2>\n<p>\u8981\u4f7f\u7528Pandas\u8b80\u53d6\u6578\u64da\u5eab\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd\u76f8\u61c9\u7684\u6578\u64da\u5eab\u9a45\u52d5\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u6578\u64da\u5eab\u7684\u5b89\u88dd\u6307\u4ee4\uff1a<\/p>\n<ul>\n<li>MySQL: <code>pip install mysql-connector-python<\/code><\/li>\n<li>PostgreSQL: <code>pip install psycopg2<\/code><\/li>\n<li>SQLite: <code>pip install sqlite3<\/code>\uff08\u901a\u5e38\u96a8Python\u81ea\u5e36\uff09<\/code><\/li>\n<\/ul>\n<p>\u5b89\u88dd\u5b8c\u9a45\u52d5\u7a0b\u5e8f\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u9023\u63a5\u6578\u64da\u5eab\u4e26\u8b80\u53d6\u6578\u64da\uff1a<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e5%be%9emysql%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a-VSYmUFeVkP\">\u793a\u4f8b\uff1a\u5f9eMySQL\u6578\u64da\u5eab\u8b80\u53d6\u6578\u64da<\/h3>\n<pre><code>import pandas as pd\nimport mysql.connector\n\n# \u9023\u63a5\u5230MySQL\u6578\u64da\u5eab\nconnection = mysql.connector.connect(\n    host='localhost',\n    user='your_username',\n    password='your_password',\n    database='your_database'\n)\n\n# \u4f7f\u7528Pandas\u8b80\u53d6\u6578\u64da\nquery = \"SELECT * FROM your_table\"\ndf = pd.read_sql(query, connection)\n\n# \u95dc\u9589\u9023\u63a5\nconnection.close()\n\n# \u986f\u793a\u6578\u64da\nprint(df.head())<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5efa\u7acb\u4e86\u4e00\u500b\u5230MySQL\u6578\u64da\u5eab\u7684\u9023\u63a5\uff0c\u7136\u5f8c\u4f7f\u7528Pandas\u7684<code>read_sql<\/code>\u51fd\u6578\u4f86\u57f7\u884cSQL\u67e5\u8a62\u4e26\u5c07\u7d50\u679c\u5b58\u5132\u5728DataFrame\u4e2d\u3002\u9019\u6a23\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u8f15\u9b06\u5730\u5c0d\u6578\u64da\u9032\u884c\u9032\u4e00\u6b65\u7684\u5206\u6790\u548c\u8655\u7406\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e5%be%9epostgresql%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a-VSYmUFeVkP\">\u793a\u4f8b\uff1a\u5f9ePostgreSQL\u6578\u64da\u5eab\u8b80\u53d6\u6578\u64da<\/h3>\n<pre><code>import pandas as pd\nimport psycopg2\n\n# \u9023\u63a5\u5230PostgreSQL\u6578\u64da\u5eab\nconnection = psycopg2.connect(\n    host='localhost',\n    user='your_username',\n    password='your_password',\n    dbname='your_database'\n)\n\n# \u4f7f\u7528Pandas\u8b80\u53d6\u6578\u64da\nquery = \"SELECT * FROM your_table\"\ndf = pd.read_sql(query, connection)\n\n# \u95dc\u9589\u9023\u63a5\nconnection.close()\n\n# \u986f\u793a\u6578\u64da\nprint(df.head())<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u8207MySQL\u7684\u793a\u4f8b\u985e\u4f3c\uff0c\u53ea\u662f\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u9023\u63a5\u65b9\u5f0f\u548c\u9a45\u52d5\u7a0b\u5e8f\u3002\u7121\u8ad6\u4f7f\u7528\u54ea\u7a2e\u6578\u64da\u5eab\uff0cPandas\u90fd\u80fd\u5920\u63d0\u4f9b\u4e00\u81f4\u7684\u63a5\u53e3\u4f86\u8b80\u53d6\u6578\u64da\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86%e7%9a%84%e9%9d%88%e6%b4%bb%e6%80%a7-VSYmUFeVkP\">\u6578\u64da\u8655\u7406\u7684\u9748\u6d3b\u6027<\/h2>\n<p>Pandas\u4e0d\u50c5\u80fd\u5920\u8b80\u53d6\u6578\u64da\uff0c\u9084\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u6578\u64da\u8655\u7406\u529f\u80fd\u3002\u7528\u6236\u53ef\u4ee5\u8f15\u9b06\u5730\u9032\u884c\u6578\u64da\u6e05\u7406\u3001\u8f49\u63db\u548c\u5206\u6790\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>groupby<\/code>\u3001<code>merge<\/code>\u548c<code>pivot_table<\/code>\u7b49\u51fd\u6578\uff0c\u53ef\u4ee5\u5feb\u901f\u5730\u5c0d\u6578\u64da\u9032\u884c\u805a\u5408\u548c\u91cd\u5851\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-VSYmUFeVkP\">\u7d50\u8ad6<\/h2>\n<p>\u7e3d\u7684\u4f86\u8aaa\uff0cPandas\u70ba\u6578\u64da\u5206\u6790\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u800c\u9748\u6d3b\u7684\u5de5\u5177\uff0c\u7279\u5225\u662f\u5728\u5f9e\u6578\u64da\u5eab\u4e2d\u8b80\u53d6\u6578\u64da\u7684\u904e\u7a0b\u4e2d\u3002\u901a\u904e\u7c21\u55ae\u7684\u4ee3\u78bc\uff0c\u4f7f\u7528\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9023\u63a5\u5230\u5404\u7a2e\u6578\u64da\u5eab\uff0c\u4e26\u9032\u884c\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u3002\u9019\u4f7f\u5f97Pandas\u6210\u70ba\u6578\u64da\u79d1\u5b78\u5bb6\u548c\u5206\u6790\u5e2b\u7684\u9996\u9078\u5de5\u5177\u4e4b\u4e00\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u4f86\u904b\u884c\u60a8\u7684\u6578\u64da\u5206\u6790\u4efb\u52d9\uff0c\u9019\u5c07\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u74b0\u5883\u548c\u9748\u6d3b\u7684\u8cc7\u6e90\u914d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Pandas\u8f15\u9b06\u8b80\u53d6\u6578\u64da\u5eab\uff0c\u7c21\u5316\u6578\u64da\u8655\u7406\u6d41\u7a0b\uff0c\u63d0\u5347\u6578\u64da\u5206\u6790\u6548\u7387\uff0c\u8b93\u6578\u64da\u64cd\u4f5c\u8b8a\u5f97\u66f4\u7c21\u55ae\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-96350","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96350","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=96350"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96350\/revisions"}],"predecessor-version":[{"id":96351,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96350\/revisions\/96351"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}