{"id":183540,"date":"2024-11-10T05:40:18","date_gmt":"2024-11-09T21:40:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183540\/"},"modified":"2024-11-10T05:40:18","modified_gmt":"2024-11-09T21:40:18","slug":"%e4%bd%bf%e7%94%a8-pandas-%e8%bc%95%e9%ac%86%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a1%a8-pandas-%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183540\/","title":{"rendered":"\u4f7f\u7528 pandas \u8f15\u9b06\u63d2\u5165\u6578\u64da\u5eab\u8868 (pandas \u63d2\u5165\u6578\u64da\u5eab\u8868)"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8-pandas-%e8%bc%95%e9%ac%86%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a1%a8-pandas-%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e8%a1%a8-JSWcmmbSos\">\u4f7f\u7528 pandas \u8f15\u9b06\u63d2\u5165\u6578\u64da\u5eab\u8868 (pandas \u63d2\u5165\u6578\u64da\u5eab\u8868)<\/h1>\n<p>\u5728\u6578\u64da\u79d1\u5b78\u548c\u6578\u64da\u5206\u6790\u7684\u9818\u57df\u4e2d\uff0cPython \u7684 <code>pandas<\/code> \u5eab\u662f\u4e00\u500b\u975e\u5e38\u5f37\u5927\u7684\u5de5\u5177\u3002\u5b83\u4e0d\u50c5\u80fd\u5920\u8655\u7406\u548c\u5206\u6790\u6578\u64da\uff0c\u9084\u80fd\u8f15\u9b06\u5730\u5c07\u6578\u64da\u63d2\u5165\u5230\u6578\u64da\u5eab\u4e2d\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 <code>pandas<\/code> \u5c07\u6578\u64da\u63d2\u5165\u6578\u64da\u5eab\u8868\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87-pandas%ef%bc%9f-JSWcmmbSos\">\u70ba\u4ec0\u9ebc\u9078\u64c7 pandas\uff1f<\/h2>\n<p><code>pandas<\/code> \u662f\u4e00\u500b\u958b\u6e90\u7684\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\u4e3b\u8981\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u6613\u65bc\u4f7f\u7528\u7684\u6578\u64da\u7d50\u69cb\uff0c\u5982 <code>DataFrame<\/code> \u548c <code>Series<\/code>\u3002<\/li>\n<li>\u5f37\u5927\u7684\u6578\u64da\u8655\u7406\u529f\u80fd\uff0c\u5305\u62ec\u6578\u64da\u6e05\u7406\u3001\u904e\u6ffe\u548c\u8f49\u63db\u3002<\/li>\n<li>\u8207\u591a\u7a2e\u6578\u64da\u5eab\u7684\u517c\u5bb9\u6027\uff0c\u652f\u6301 SQLAlchemy \u7b49\u5eab\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%ae%89%e8%a3%9d%e5%bf%85%e8%a6%81%e7%9a%84%e5%ba%ab-JSWcmmbSos\">\u5b89\u88dd\u5fc5\u8981\u7684\u5eab<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86 <code>pandas<\/code> \u548c <code>SQLAlchemy<\/code>\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install pandas sqlalchemy<\/code><\/pre>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab-JSWcmmbSos\">\u9023\u63a5\u5230\u6578\u64da\u5eab<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u9023\u63a5\u5230 SQLite \u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>from sqlalchemy import create_engine\n\n# \u5275\u5efa\u6578\u64da\u5eab\u5f15\u64ce\nengine = create_engine('sqlite:\/\/\/example.db')\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 SQLite \u6578\u64da\u5eab\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u8981\u66f4\u6539\u70ba\u5176\u4ed6\u6578\u64da\u5eab\uff0c\u5982 MySQL \u6216 PostgreSQL\u3002<\/p>\n<h2 id=\"%e5%89%b5%e5%bb%ba%e6%95%b8%e6%93%9a%e8%a1%a8-JSWcmmbSos\">\u5275\u5efa\u6578\u64da\u8868<\/h2>\n<p>\u5728\u63d2\u5165\u6578\u64da\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u6578\u64da\u8868\u5df2\u7d93\u5b58\u5728\u3002\u4ee5\u4e0b\u662f\u5275\u5efa\u4e00\u500b\u7c21\u55ae\u6578\u64da\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>with engine.connect() as connection:\n    connection.execute(\"\"\"\n    CREATE TABLE IF NOT EXISTS users (\n        id INTEGER PRIMARY KEY,\n        name TEXT,\n        age INTEGER\n    )\n    \"\"\")\n<\/code><\/pre>\n<h2 id=\"%e4%bd%bf%e7%94%a8-pandas-%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a-JSWcmmbSos\">\u4f7f\u7528 pandas \u63d2\u5165\u6578\u64da<\/h2>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>pandas<\/code> \u5c07\u6578\u64da\u63d2\u5165\u5230\u6578\u64da\u5eab\u8868\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c07\u6578\u64da\u63d2\u5165\u5230\u525b\u525b\u5275\u5efa\u7684 <code>users<\/code> \u8868\u4e2d\uff1a<\/p>\n<pre><code>import pandas as pd\n\n# \u5275\u5efa\u4e00\u500b DataFrame\ndata = {\n    'name': ['Alice', 'Bob', 'Charlie'],\n    'age': [25, 30, 35]\n}\ndf = pd.DataFrame(data)\n\n# \u5c07\u6578\u64da\u63d2\u5165\u5230\u6578\u64da\u5eab\ndf.to_sql('users', con=engine, if_exists='append', index=False)\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u5305\u542b\u7528\u6236\u59d3\u540d\u548c\u5e74\u9f61\u7684 <code>DataFrame<\/code>\uff0c\u7136\u5f8c\u4f7f\u7528 <code>to_sql<\/code> \u65b9\u6cd5\u5c07\u6578\u64da\u63d2\u5165\u5230 <code>users<\/code> \u8868\u4e2d\u3002\u53c3\u6578 <code>if_exists='append'<\/code> \u8868\u793a\u5982\u679c\u8868\u5df2\u5b58\u5728\uff0c\u5247\u5c07\u6578\u64da\u9644\u52a0\u5230\u8868\u4e2d\u3002<\/p>\n<h2 id=\"%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-JSWcmmbSos\">\u67e5\u8a62\u6578\u64da<\/h2>\n<p>\u63d2\u5165\u6578\u64da\u5f8c\uff0c\u60a8\u53ef\u80fd\u60f3\u8981\u67e5\u8a62\u6578\u64da\u4ee5\u78ba\u8a8d\u63d2\u5165\u662f\u5426\u6210\u529f\u3002\u4ee5\u4e0b\u662f\u67e5\u8a62\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>result = pd.read_sql('SELECT * FROM users', con=engine)\nprint(result)\n<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de <code>users<\/code> \u8868\u4e2d\u7684\u6240\u6709\u6578\u64da\uff0c\u4e26\u5c07\u5176\u986f\u793a\u70ba <code>DataFrame<\/code>\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-JSWcmmbSos\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528 <code>pandas<\/code> \u63d2\u5165\u6578\u64da\u5eab\u8868\u662f\u4e00\u500b\u7c21\u55ae\u800c\u9ad8\u6548\u7684\u904e\u7a0b\u3002\u901a\u904e\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\u3001\u5275\u5efa\u6578\u64da\u8868\u4ee5\u53ca\u4f7f\u7528 <code>to_sql<\/code> \u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u6578\u64da\u63d2\u5165\u5230\u6578\u64da\u5eab\u4e2d\u3002\u9019\u4f7f\u5f97\u6578\u64da\u5206\u6790\u548c\u8655\u7406\u8b8a\u5f97\u66f4\u52a0\u4fbf\u6377\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u5404\u7a2e\u9700\u6c42\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\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 pandas \u8f15\u9b06\u5c07\u6578\u64da\u63d2\u5165\u6578\u64da\u5eab\u8868\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\u6d41\u7a0b\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-183540","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183540","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=183540"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183540\/revisions"}],"predecessor-version":[{"id":183541,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183540\/revisions\/183541"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}