{"id":948917,"date":"2024-12-27T00:08:03","date_gmt":"2024-12-26T16:08:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/948917.html"},"modified":"2024-12-27T00:08:06","modified_gmt":"2024-12-26T16:08:06","slug":"python-%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e8%a1%a8%e6%a0%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/948917.html","title":{"rendered":"python \u5982\u4f55\u751f\u6210\u8868\u683c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084112\/ac994144-c6b5-4cf9-8e35-04efbf3d3596.webp\" alt=\"python \u5982\u4f55\u751f\u6210\u8868\u683c\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210\u8868\u683c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u4f7f\u7528pandas\u5e93\u3001tabulate\u5e93\u3001PrettyTable\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c<strong>pandas\u5e93<\/strong>\u662f\u6570\u636e\u5206\u6790\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u9002\u5408\u5904\u7406\u548c\u751f\u6210\u5927\u578b\u6570\u636e\u8868\u683c\uff1b<strong>tabulate\u5e93<\/strong>\u5219\u53ef\u4ee5\u5feb\u901f\u5730\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u8868\u683c\u683c\u5f0f\uff0c\u9002\u5408\u751f\u6210\u7b80\u5355\u4e14\u53ef\u89c6\u5316\u7684\u6587\u672c\u8868\u683c\uff1b<strong>PrettyTable\u5e93<\/strong>\u5219\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u8868\u683c\u683c\u5f0f\u9009\u9879\uff0c\u652f\u6301\u8f93\u51fa\u4e3aHTML\u548c\u5176\u4ed6\u683c\u5f0f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u751f\u6210\u8868\u683c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PANDAS\u5e93\u751f\u6210\u8868\u683c<\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u3002\u5b83\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u7ec4\u7ec7\u6210\u8868\u683c\u5f62\u5f0f\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Pandas\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86pandas\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u751f\u6210DataFrame\u8868\u683c<\/strong><\/li>\n<\/ol>\n<p><p>Pandas\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u662fDataFrame\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u3001\u5217\u8868\u3001NumPy\u6570\u7ec4\u7b49\u591a\u79cd\u65b9\u5f0f\u521b\u5efa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/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<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6253\u5370DataFrame<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u64cd\u4f5cDataFrame<\/strong><\/li>\n<\/ol>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u64cd\u4f5cDataFrame\uff0c\u4f8b\u5982\u6dfb\u52a0\u3001\u5220\u9664\u5217\uff0c\u6392\u5e8f\uff0c\u8fc7\u6ee4\u6570\u636e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u65b0\u5217<\/p>\n<p>df[&#39;Salary&#39;] = [50000, 60000, 70000]<\/p>\n<h2><strong>\u6309\u5e74\u9f84\u6392\u5e8f<\/strong><\/h2>\n<p>df_sorted = df.sort_values(by=&#39;Age&#39;)<\/p>\n<h2><strong>\u8fc7\u6ee4\u6570\u636e<\/strong><\/h2>\n<p>df_filtered = df[df[&#39;Age&#39;] &gt; 28]<\/p>\n<p>print(df_sorted)<\/p>\n<p>print(df_filtered)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u8f93\u51fa\u4e3a\u5176\u4ed6\u683c\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>Pandas\u8fd8\u652f\u6301\u5c06DataFrame\u8f93\u51fa\u4e3a\u591a\u79cd\u683c\u5f0f\uff0c\u5305\u62ecCSV\u3001Excel\u3001HTML\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u51fa\u4e3aCSV\u6587\u4ef6<\/p>\n<p>df.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<h2><strong>\u8f93\u51fa\u4e3aExcel\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<h2><strong>\u8f93\u51fa\u4e3aHTML\u8868\u683c<\/strong><\/h2>\n<p>html = df.to_html()<\/p>\n<p>print(html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528TABULATE\u5e93\u751f\u6210\u8868\u683c<\/p>\n<\/p>\n<p><p>Tabulate\u5e93\u53ef\u4ee5\u5c06\u6570\u636e\u5feb\u901f\u8f6c\u6362\u4e3a\u6587\u672c\u8868\u683c\u683c\u5f0f\uff0c\u652f\u6301\u591a\u79cd\u6837\u5f0f\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u7ec8\u7aef\u663e\u793a\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Tabulate\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5tabulate\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tabulate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u751f\u6210\u6587\u672c\u8868\u683c<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7tabulate\u5e93\u5c06\u5217\u8868\u3001\u5b57\u5178\u6216NumPy\u6570\u7ec4\u8f6c\u6362\u4e3a\u6587\u672c\u8868\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from tabulate import tabulate<\/p>\n<h2><strong>\u5b9a\u4e49\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>\u751f\u6210\u8868\u683c<\/strong><\/h2>\n<p>table = tabulate(data, headers=[&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;], tablefmt=&#39;grid&#39;)<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u9009\u62e9\u4e0d\u540c\u7684\u8868\u683c\u683c\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>Tabulate\u652f\u6301\u591a\u79cd\u8868\u683c\u683c\u5f0f\uff0c\u5305\u62ecpl<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n\u3001grid\u3001pipe\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u4e0d\u540c\u7684\u683c\u5f0f<\/p>\n<p>table_pipe = tabulate(data, headers=[&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;], tablefmt=&#39;pipe&#39;)<\/p>\n<p>table_html = tabulate(data, headers=[&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;], tablefmt=&#39;html&#39;)<\/p>\n<p>print(table_pipe)<\/p>\n<p>print(table_html)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PRETTYTABLE\u5e93\u751f\u6210\u8868\u683c<\/p>\n<\/p>\n<p><p>PrettyTable\u5e93\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u683c\u5f0f\u5316\u9009\u9879\uff0c\u53ef\u4ee5\u521b\u5efa\u7f8e\u89c2\u7684\u6587\u672c\u548cHTML\u8868\u683c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5PrettyTable\u5e93<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5PrettyTable\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install prettytable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u751f\u6210PrettyTable\u8868\u683c<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7PrettyTable\u5e93\u521b\u5efa\u8868\u683c\uff0c\u5e76\u6dfb\u52a0\u6570\u636e\u3002<\/p>\n<\/p>\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>\u5b9a\u4e49\u5217<\/strong><\/h2>\n<p>table.field_names = [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;]<\/p>\n<h2><strong>\u6dfb\u52a0\u884c<\/strong><\/h2>\n<p>table.add_row([&#39;Alice&#39;, 25, &#39;New York&#39;])<\/p>\n<p>table.add_row([&#39;Bob&#39;, 30, &#39;Los Angeles&#39;])<\/p>\n<p>table.add_row([&#39;Charlie&#39;, 35, &#39;Chicago&#39;])<\/p>\n<h2><strong>\u6253\u5370\u8868\u683c<\/strong><\/h2>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u683c\u5f0f\u5316\u8868\u683c<\/strong><\/li>\n<\/ol>\n<p><p>PrettyTable\u652f\u6301\u5bf9\u8868\u683c\u8fdb\u884c\u591a\u79cd\u683c\u5f0f\u5316\u64cd\u4f5c\uff0c\u4f8b\u5982\u5bf9\u9f50\u3001\u8fb9\u6846\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u5bf9\u9f50\u65b9\u5f0f<\/p>\n<p>table.align[&#39;Name&#39;] = &#39;l&#39;  # \u5de6\u5bf9\u9f50<\/p>\n<p>table.align[&#39;Age&#39;] = &#39;c&#39;   # \u5c45\u4e2d\u5bf9\u9f50<\/p>\n<p>table.align[&#39;City&#39;] = &#39;r&#39;  # \u53f3\u5bf9\u9f50<\/p>\n<h2><strong>\u7981\u7528\u8fb9\u6846<\/strong><\/h2>\n<p>table.border = False<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u8f93\u51fa\u4e3aHTML\u548c\u5176\u4ed6\u683c\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>PrettyTable\u8fd8\u652f\u6301\u5c06\u8868\u683c\u8f93\u51fa\u4e3aHTML\u3001CSV\u7b49\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u51fa\u4e3aHTML<\/p>\n<p>html_table = table.get_html_string()<\/p>\n<p>print(html_table)<\/p>\n<h2><strong>\u8f93\u51fa\u4e3aCSV<\/strong><\/h2>\n<p>csv_table = table.get_csv_string()<\/p>\n<p>print(csv_table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u751f\u6210\u8868\u683c\u7684\u65b9\u6cd5\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002\u5728\u6570\u636e\u5206\u6790\u548c\u5c55\u793a\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9pandas\u3001tabulate\u6216PrettyTable\u7b49\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u751f\u6210Excel\u8868\u683c\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5e93\u6765\u521b\u5efaExcel\u8868\u683c\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f<code>pandas<\/code>\u548c<code>openpyxl<\/code>\u3002\u901a\u8fc7<code>pandas<\/code>\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u6846\u67b6\uff08DataFrame\uff09\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\u3002\u53ea\u9700\u5b89\u88c5<code>pandas<\/code>\u548c<code>openpyxl<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>to_excel()<\/code>\u65b9\u6cd5\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;\u59d3\u540d&#39;: [&#39;\u5f20\u4e09&#39;, &#39;\u674e\u56db&#39;], &#39;\u5e74\u9f84&#39;: [28, 34]}\ndf = pd.DataFrame(data)\ndf.to_excel(&#39;output.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a\u540d\u4e3a<code>output.xlsx<\/code>\u7684Excel\u6587\u4ef6\uff0c\u5185\u5bb9\u4e3a\u59d3\u540d\u548c\u5e74\u9f84\u7684\u8868\u683c\u3002<\/p>\n<p><strong>Python\u751f\u6210HTML\u8868\u683c\u7684\u65b9\u5f0f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4f7f\u7528Python\u751f\u6210HTML\u8868\u683c\u4e5f\u975e\u5e38\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u5229\u7528<code>pandas<\/code>\u5e93\u7684<code>to_html()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u6846\u67b6\u8f6c\u6362\u4e3aHTML\u683c\u5f0f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;\u4ea7\u54c1&#39;: [&#39;\u82f9\u679c&#39;, &#39;\u9999\u8549&#39;], &#39;\u4ef7\u683c&#39;: [3.5, 2.0]}\ndf = pd.DataFrame(data)\nhtml_table = df.to_html()\nprint(html_table)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u4e00\u4e2aHTML\u683c\u5f0f\u7684\u8868\u683c\uff0c\u9002\u5408\u5728\u7f51\u9875\u4e2d\u5c55\u793a\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528Matplotlib\u7ed8\u5236\u8868\u683c\uff1f<\/strong><br \/><code>Matplotlib<\/code>\u4e0d\u4ec5\u4ec5\u7528\u4e8e\u7ed8\u5236\u56fe\u5f62\uff0c\u8fd8\u53ef\u4ee5\u751f\u6210\u8868\u683c\u3002\u4f7f\u7528<code>matplotlib.pyplot.table()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u56fe\u5f62\u4e2d\u6dfb\u52a0\u8868\u683c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\ndata = [[&#39;\u59d3\u540d&#39;, &#39;\u5e74\u9f84&#39;], [&#39;\u5f20\u4e09&#39;, 28], [&#39;\u674e\u56db&#39;, 34]]\nfig, ax = plt.subplots()\nax.axis(&#39;tight&#39;)\nax.axis(&#39;off&#39;)\nax.table(cellText=data, loc=&#39;center&#39;)\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u8868\u683c\u5e76\u5728\u56fe\u5f62\u7a97\u53e3\u4e2d\u663e\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210\u8868\u683c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u6709\u4f7f\u7528pandas\u5e93\u3001tabulate\u5e93\u3001PrettyTable\u5e93\u3002 [&hellip;]","protected":false},"author":3,"featured_media":948924,"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\/948917"}],"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=948917"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948917\/revisions"}],"predecessor-version":[{"id":948927,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948917\/revisions\/948927"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/948924"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=948917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=948917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=948917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}