{"id":1132614,"date":"2025-01-08T20:58:57","date_gmt":"2025-01-08T12:58:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132614.html"},"modified":"2025-01-08T20:58:59","modified_gmt":"2025-01-08T12:58:59","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e7%ac%ac10%e5%88%97%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132614.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8868\u793a\u7b2c10\u5217\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102116\/f16fee64-1639-4eed-babc-466d377f1587.webp\" alt=\"python\u4e2d\u5982\u4f55\u8868\u793a\u7b2c10\u5217\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8868\u793a\u7b2c10\u5217\u6570\u636e\u5e93\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528SQL\u67e5\u8be2\u8bed\u53e5\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4f7f\u7528SQLite3\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7SQL\u67e5\u8be2\u8bed\u53e5\u6765\u63d0\u53d6\u7279\u5b9a\u5217\u7684\u6570\u636e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91ca\u4f7f\u7528SQL\u67e5\u8be2\u8bed\u53e5\u7684\u65b9\u6cd5\uff0c\u5e76\u7b80\u8981\u4ecb\u7ecd\u4f7f\u7528Pandas\u5e93\u548cSQLite3\u5e93\u7684\u5176\u4ed6\u4e24\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001SQL\u67e5\u8be2\u8bed\u53e5<\/h3>\n<\/p>\n<p><h4>\u4f7f\u7528Python\u8fde\u63a5\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528SQL\u67e5\u8be2\u8bed\u53e5\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3002Python\u4e2d\u5e38\u7528\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\u6709<code>sqlite3<\/code>\u3001<code>MySQLdb<\/code>\u3001<code>psycopg2<\/code>\u7b49\u3002\u4e0b\u9762\u4ee5<code>sqlite3<\/code>\u4e3a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u8fde\u63a5\u5230\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u67e5\u8be2\u7b2c10\u5217\u7684\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u8fde\u63a5\u5230\u6570\u636e\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8be2\u8bed\u53e5\u6765\u63d0\u53d6\u7b2c10\u5217\u7684\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_table<\/code>\u7684\u8868\uff0c\u8868\u4e2d\u6709\u591a\u5217\u6570\u636e\uff0c\u6211\u4eec\u9700\u8981\u63d0\u53d6\u7b2c10\u5217\u7684\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bSQL\u67e5\u8be2\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.execute(&quot;SELECT column_name FROM my_table&quot;)<\/p>\n<p>result = cursor.fetchall()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>column_name<\/code>\u662f\u7b2c10\u5217\u7684\u5217\u540d\u3002\u6267\u884c\u67e5\u8be2\u540e\uff0c<code>result<\/code>\u5c06\u5305\u542b\u7b2c10\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u67e5\u8be2\u5b8c\u6210\u540e\uff0c\u8bb0\u5f97\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u4ee5\u91ca\u653e\u8d44\u6e90\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662fPython\u4e2d\u5e38\u7528\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u63d0\u53d6\u7279\u5b9a\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Pandas\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Pandas\u8bfb\u53d6\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u5e93\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u63d0\u53d6\u7b2c10\u5217\u7684\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a<code>example.db<\/code>\u7684SQLite\u6570\u636e\u5e93\uff0c\u6570\u636e\u5e93\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_table<\/code>\u7684\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<h2><strong>\u4f7f\u7528Pandas\u8bfb\u53d6\u6570\u636e\u5e93<\/strong><\/h2>\n<p>df = pd.read_sql_query(&quot;SELECT * FROM my_table&quot;, conn)<\/p>\n<h2><strong>\u63d0\u53d6\u7b2c10\u5217\u7684\u6570\u636e<\/strong><\/h2>\n<p>column_10_data = df.iloc[:, 9]<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>df<\/code>\u662f\u8bfb\u53d6\u5230\u7684\u6570\u636e\u8868\uff0c<code>df.iloc[:, 9]<\/code>\u8868\u793a\u63d0\u53d6\u7b2c10\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528SQLite3\u5e93<\/h3>\n<\/p>\n<p><p>SQLite3\u5e93\u662fPython\u4e2d\u5e38\u7528\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5e93\uff0c\u4f7f\u7528SQLite3\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fde\u63a5\u548c\u64cd\u4f5cSQLite\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5SQLite3\u5e93<\/h4>\n<\/p>\n<p><p>SQLite3\u5e93\u662fPython\u7684\u6807\u51c6\u5e93\uff0c\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528SQLite3\u8bfb\u53d6\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528SQLite3\u5e93\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u63d0\u53d6\u7b2c10\u5217\u7684\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a<code>example.db<\/code>\u7684SQLite\u6570\u636e\u5e93\uff0c\u6570\u636e\u5e93\u4e2d\u6709\u4e00\u4e2a\u540d\u4e3a<code>my_table<\/code>\u7684\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u67e5\u8be2\u7b2c10\u5217\u7684\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT column_name FROM my_table&quot;)<\/p>\n<p>result = cursor.fetchall()<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>column_name<\/code>\u662f\u7b2c10\u5217\u7684\u5217\u540d\uff0c\u6267\u884c\u67e5\u8be2\u540e\uff0c<code>result<\/code>\u5c06\u5305\u542b\u7b2c10\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8868\u793a\u7b2c10\u5217\u6570\u636e\u5e93\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528SQL\u67e5\u8be2\u8bed\u53e5\u3001\u4f7f\u7528Pandas\u5e93\u548c\u4f7f\u7528SQLite3\u5e93\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7SQL\u67e5\u8be2\u8bed\u53e5\u6765\u63d0\u53d6\u7279\u5b9a\u5217\u7684\u6570\u636e\u3002\u4f7f\u7528Pandas\u5e93\u548cSQLite3\u5e93\u4e5f\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u63d0\u53d6\u7279\u5b9a\u5217\u7684\u6570\u636e\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u5148\u8fde\u63a5\u5230\u6570\u636e\u5e93\uff0c\u7136\u540e\u6267\u884c\u67e5\u8be2\u8bed\u53e5\u6216\u4f7f\u7528\u5e93\u51fd\u6570\u6765\u63d0\u53d6\u6570\u636e\uff0c\u6700\u540e\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u6570\u636e\u5e93\u7684\u7b2c10\u5217\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u8bfb\u53d6\u6570\u636e\u5e93\uff0c\u4f8b\u5982pandas\u548cSQLAlchemy\u3002\u5047\u8bbe\u60a8\u4f7f\u7528pandas\u8bfb\u53d6\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pandas.read_sql()<\/code>\u51fd\u6570\u6267\u884cSQL\u67e5\u8be2\uff0c\u4ece\u800c\u9009\u62e9\u7279\u5b9a\u7684\u5217\u3002\u6bd4\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6\u7b2c10\u5217\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\nimport sqlite3\n\n# \u8fde\u63a5\u5230\u6570\u636e\u5e93\nconn = sqlite3.connect(&#39;your_database.db&#39;)\n\n# \u6267\u884cSQL\u67e5\u8be2\uff0c\u9009\u62e9\u7b2c10\u5217\ndf = pd.read_sql(&#39;SELECT column10 FROM your_table&#39;, conn)\n\n# \u5173\u95ed\u8fde\u63a5\nconn.close()\n<\/code><\/pre>\n<p>\u786e\u4fdd\u66ff\u6362<code>column10<\/code>\u548c<code>your_table<\/code>\u4e3a\u5b9e\u9645\u7684\u5217\u540d\u548c\u8868\u540d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u901a\u8fc7\u7d22\u5f15\u8bbf\u95eeDataFrame\u7684\u7b2c10\u5217\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5df2\u7ecf\u5c06\u6570\u636e\u5e93\u7684\u6570\u636e\u52a0\u8f7d\u5230pandas DataFrame\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u7684\u7d22\u5f15\u76f4\u63a5\u8bbf\u95ee\u7b2c10\u5217\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<pre><code class=\"language-python\"># \u5047\u8bbedf\u662f\u60a8\u7684DataFrame\ntenth_column = df.iloc[:, 9]  # Python\u7684\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u7b2c10\u5217\u7684\u7d22\u5f15\u4e3a9\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5230\u7b2c10\u5217\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u6570\u636e\u5e93\u4e2d\u7b2c10\u5217\u7684\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5904\u7406\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u65f6\uff0c\u4e86\u89e3\u7b2c10\u5217\u7684\u6570\u636e\u7c7b\u578b\u975e\u5e38\u91cd\u8981\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528pandas\u7684<code>dtypes<\/code>\u5c5e\u6027\u6765\u67e5\u770bDataFrame\u4e2d\u6bcf\u4e00\u5217\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\"># \u6253\u5370\u6bcf\u4e00\u5217\u7684\u6570\u636e\u7c7b\u578b\nprint(df.dtypes)\n\n# \u82e5\u9700\u67e5\u770b\u7b2c10\u5217\u7684\u6570\u636e\u7c7b\u578b\nprint(df.iloc[:, 9].dtype)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u5bf9\u6570\u636e\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u5206\u6790\u65f6\uff0c\u60a8\u4f7f\u7528\u7684\u64cd\u4f5c\u4e0e\u6570\u636e\u7c7b\u578b\u76f8\u5339\u914d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8868\u793a\u7b2c10\u5217\u6570\u636e\u5e93\u7684\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528SQL\u67e5\u8be2\u8bed\u53e5\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4f7f\u7528SQLite3\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1132623,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132614"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1132614"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132614\/revisions"}],"predecessor-version":[{"id":1132627,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132614\/revisions\/1132627"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132623"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}