{"id":1094060,"date":"2025-01-08T14:37:17","date_gmt":"2025-01-08T06:37:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094060.html"},"modified":"2025-01-08T14:37:19","modified_gmt":"2025-01-08T06:37:19","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e8%a1%a8%e6%a0%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094060.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u8868\u683c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210343\/02101aca-5fe6-4e0c-9026-e74a552bf59f.webp\" alt=\"python\u4e2d\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u8868\u683c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u8868\u683c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f8b\u5982\u4f7f\u7528pandas\u5e93\u3001tabulate\u5e93\u3001PrettyTable\u5e93\u7b49\u5de5\u5177\u3002\u4f7f\u7528pandas\u5e93\u662f\u6700\u5e38\u89c1\u7684\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u521b\u5efa\u8868\u683c\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u5904\u7406\u3001\u5206\u6790\u7b49\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>pandas\u5e93\u53ef\u4ee5\u901a\u8fc7DataFrame\u5bf9\u8c61\u521b\u5efa\u4e00\u4e2a\u8868\u683c\uff0c\u6b64\u5916\uff0ctabulate\u5e93\u548cPrettyTable\u5e93\u4e5f\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u683c\u5f0f\u5316\u8868\u683c\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u5c55\u793a\u4e00\u4e9b\u5b9e\u9645\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pandas\u5e93\u521b\u5efa\u8868\u683c<\/h3>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u3001\u64cd\u4f5c\u548c\u53ef\u89c6\u5316\u8868\u683c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528pandas\u521b\u5efa\u8868\u683c\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5pandas\u5e93<\/strong>\uff1a<\/p>\n<p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5pandas\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaDataFrame\u5bf9\u8c61<\/strong>\uff1a<\/p>\n<p>DataFrame\u662fpandas\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u5b83\u662f\u4e00\u79cd\u7c7b\u4f3c\u4e8e\u7535\u5b50\u8868\u683c\u7684\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521b\u5efaDataFrame\u5bf9\u8c61\uff0c\u4f8b\u5982\u4ece\u5b57\u5178\u3001\u5217\u8868\u3001CSV\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u64cd\u4f5c\u548c\u663e\u793a\u8868\u683c\u6570\u636e<\/strong>\uff1a<\/p>\n<p>\u4e00\u65e6\u521b\u5efa\u4e86DataFrame\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528pandas\u63d0\u4f9b\u7684\u5404\u79cd\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u548c\u663e\u793a\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4ece\u5b57\u5178\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u663e\u793a\u8868\u683c<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0<\/strong>\uff1a<\/p>\n<p>\u4f7f\u7528pandas\u521b\u5efa\u8868\u683c\u7684\u4e00\u4e2a\u91cd\u8981\u4f18\u70b9\u662f\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u64cd\u4f5c\u548c\u5206\u6790\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\u3001\u7b5b\u9009\u3001\u5206\u7ec4\u3001\u805a\u5408\u7b49\u64cd\u4f5c\u3002\u6b64\u5916\uff0cpandas\u8fd8\u652f\u6301\u4ece\u5404\u79cd\u6570\u636e\u6e90\uff08\u5982CSV\u3001Excel\u3001SQL\u7b49\uff09\u5bfc\u5165\u6570\u636e\uff0c\u5e76\u5c06DataFrame\u5bfc\u51fa\u5230\u8fd9\u4e9b\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528tabulate\u5e93\u521b\u5efa\u8868\u683c<\/h3>\n<\/p>\n<p><p>tabulate\u5e93\u53ef\u4ee5\u5c06Python\u4e2d\u7684\u5217\u8868\u3001\u5b57\u5178\u7b49\u6570\u636e\u7ed3\u6784\u8f6c\u6362\u4e3a\u6f02\u4eae\u7684ASCII\u8868\u683c\u3001HTML\u8868\u683c\u6216Markdown\u8868\u683c\uff0c\u975e\u5e38\u9002\u5408\u5728\u7ec8\u7aef\u6216Markdown\u6587\u4ef6\u4e2d\u663e\u793a\u8868\u683c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528tabulate\u5e93\u521b\u5efa\u8868\u683c\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5tabulate\u5e93<\/strong>\uff1a<\/p>\n<p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5tabulate\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tabulate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u8868\u683c\u5e76\u663e\u793a<\/strong>\uff1a<\/p>\n<p>\u4f7f\u7528tabulate\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u548c\u663e\u793a\u8868\u683c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from tabulate import tabulate<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&#39;Alice&#39;, 25, &#39;New York&#39;],<\/p>\n<p>    [&#39;Bob&#39;, 30, &#39;Los Angeles&#39;],<\/p>\n<p>    [&#39;Charlie&#39;, 35, &#39;Chicago&#39;]<\/p>\n<p>]<\/p>\n<h2><strong>\u5217\u540d<\/strong><\/h2>\n<p>headers = [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;]<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u663e\u793a\u8868\u683c<\/strong><\/h2>\n<p>table = tabulate(data, headers, tablefmt=&#39;pretty&#39;)<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528PrettyTable\u5e93\u521b\u5efa\u8868\u683c<\/h3>\n<\/p>\n<p><p>PrettyTable\u5e93\u662f\u53e6\u4e00\u4e2a\u65b9\u4fbf\u7684\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u683c\u5f0f\u5316\u8868\u683c\u3002\u5b83\u652f\u6301\u591a\u79cd\u8868\u683c\u6837\u5f0f\u548c\u683c\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u6570\u636e\u5c55\u793a\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PrettyTable\u5e93\u521b\u5efa\u8868\u683c\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5PrettyTable\u5e93<\/strong>\uff1a<\/p>\n<p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5PrettyTable\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install prettytable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u8868\u683c\u5e76\u663e\u793a<\/strong>\uff1a<\/p>\n<p>\u4f7f\u7528PrettyTable\u7c7b\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u548c\u663e\u793a\u8868\u683c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<h2><strong>\u521b\u5efaPrettyTable\u5bf9\u8c61<\/strong><\/h2>\n<p>table = PrettyTable()<\/p>\n<h2><strong>\u6dfb\u52a0\u5217<\/strong><\/h2>\n<p>table.add_column(&#39;Name&#39;, [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;])<\/p>\n<p>table.add_column(&#39;Age&#39;, [25, 30, 35])<\/p>\n<p>table.add_column(&#39;City&#39;, [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;])<\/p>\n<h2><strong>\u663e\u793a\u8868\u683c<\/strong><\/h2>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u521b\u5efa\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4ee5\u4e0a\u4e09\u79cd\u5e38\u7528\u65b9\u6cd5\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u548c\u5de5\u5177\u521b\u5efa\u8868\u683c\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528NumPy\u5e93<\/strong>\uff1a<\/p>\n<p>NumPy\u662f\u4e00\u4e2a\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u867d\u7136\u4e3b\u8981\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5c\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528openpyxl\u5e93\u521b\u5efaExcel\u8868\u683c<\/strong>\uff1a<\/p>\n<p>openpyxl\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u5199Excel\u6587\u4ef6\u7684\u5e93\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u521b\u5efa\u548c\u64cd\u4f5cExcel\u8868\u683c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528SQLAlchemy\u5e93\u521b\u5efa\u6570\u636e\u5e93\u8868\u683c<\/strong>\uff1a<\/p>\n<p>SQLAlchemy\u662f\u4e00\u4e2aSQL\u5de5\u5177\u5305\u548c\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff08ORM\uff09\u5e93\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u6570\u636e\u5e93\u8868\u683c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u521b\u5efa\u8868\u683c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u3002\u5982\u679c\u4f60\u9700\u8981\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c\u63a8\u8350\u4f7f\u7528pandas\u5e93\uff1b\u5982\u679c\u4f60\u9700\u8981\u5728\u7ec8\u7aef\u6216Markdown\u6587\u4ef6\u4e2d\u663e\u793a\u8868\u683c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528tabulate\u5e93\u6216PrettyTable\u5e93\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u548c\u64cd\u4f5c\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Pandas\u521b\u5efa\u8868\u683c\uff1f<\/strong><br \/>\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u548c\u64cd\u4f5c\u8868\u683c\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Pandas\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip install pandas<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>pd.DataFrame<\/code>\u521b\u5efa\u8868\u683c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u4f20\u9012\u6570\u636e\uff0c\u50cf\u8fd9\u6837\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {\n    &#39;\u52171&#39;: [1, 2, 3],\n    &#39;\u52172&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;]\n}\ndf = pd.DataFrame(data)\nprint(df)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e24\u5217\u7684\u8868\u683c\u3002Pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u6570\u636e\uff0c\u4f8b\u5982\u7b5b\u9009\u3001\u6392\u5e8f\u548c\u6570\u636e\u5206\u6790\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u521b\u5efa\u8868\u683c\uff1f<\/strong><br \/>\u9664\u4e86Pandas\uff0cPython\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u8868\u683c\uff0c\u4f8b\u5982<code>openpyxl<\/code>\u548c<code>xlsxwriter<\/code>\u3002<code>openpyxl<\/code>\u4e3b\u8981\u7528\u4e8e\u5904\u7406Excel\u6587\u4ef6\uff0c\u800c<code>xlsxwriter<\/code>\u5219\u4e13\u6ce8\u4e8e\u521b\u5efa\u65b0\u7684Excel\u6587\u4ef6\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u60a8\u5177\u4f53\u7684\u9700\u6c42\uff0c\u4f8b\u5982\u662f\u5426\u9700\u8981\u8fdb\u884c\u6570\u636e\u5206\u6790\u6216\u751f\u6210Excel\u62a5\u544a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u8868\u683c\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\uff1f<\/strong><br \/>\u4f7f\u7528Pandas\u5bfc\u51fa\u8868\u683c\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528<code>to_excel<\/code>\u51fd\u6570\u5c06DataFrame\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\u3002\u786e\u4fdd\u5df2\u5b89\u88c5<code>openpyxl<\/code>\u6216<code>xlsxwriter<\/code>\u5e93\uff0c\u4ee5\u652f\u6301Excel\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">df.to_excel(&#39;output.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u884c\u4ee3\u7801\u5c06DataFrame\u5bfc\u51fa\u4e3a\u540d\u4e3a\u201coutput.xlsx\u201d\u7684Excel\u6587\u4ef6\uff0c\u4e0d\u5305\u62ec\u884c\u7d22\u5f15\u3002\u7528\u6237\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6587\u4ef6\u540d\u548c\u8def\u5f84\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u8868\u683c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u4f8b\u5982\u4f7f\u7528pandas\u5e93\u3001tabulate\u5e93\u3001PrettyTable\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1094067,"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\/1094060"}],"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=1094060"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094060\/revisions"}],"predecessor-version":[{"id":1094068,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094060\/revisions\/1094068"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094067"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}