{"id":968605,"date":"2024-12-27T05:08:39","date_gmt":"2024-12-26T21:08:39","guid":{"rendered":""},"modified":"2024-12-27T05:08:41","modified_gmt":"2024-12-26T21:08:41","slug":"python-%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e8%a1%a8%e6%a0%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/968605.html","title":{"rendered":"python \u5982\u4f55\u6253\u5370\u8868\u683c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183301\/f5d888f3-1ef4-4536-bc60-2084dae98973.webp\" alt=\"python \u5982\u4f55\u6253\u5370\u8868\u683c\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u6253\u5370\u8868\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>pandas<\/code>\u5e93\u3001<code>tabulate<\/code>\u5e93\u3001<code>prettytable<\/code>\u5e93\u3001\u4ee5\u53ca\u624b\u52a8\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u3001\u6570\u636e\u6765\u6e90\u4ee5\u53ca\u8f93\u51fa\u7684\u590d\u6742\u5ea6<\/strong>\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528<code>pandas<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\uff0c\u5e76\u4e14\u53ef\u4ee5\u5c06\u6570\u636e\u4ee5\u8868\u683c\u5f62\u5f0f\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u662fPython\u4e2d\u975e\u5e38\u6d41\u884c\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u80fd\u591f\u65b9\u4fbf\u5730\u5904\u7406\u6570\u636e\uff0c\u5e76\u4e14\u80fd\u591f\u4ee5\u8868\u683c\u5f62\u5f0f\u8f93\u51fa\u6570\u636e\u3002\u4f7f\u7528<code>pandas<\/code>\u6253\u5370\u8868\u683c\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5e93\u3001\u521b\u5efa\u6570\u636e\u7ed3\u6784\u3001\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u4f7f\u7528PANDAS\u5e93\u6253\u5370\u8868\u683c<\/p>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u662fPython\u6570\u636e\u5904\u7406\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u80fd\u591f\u8f7b\u677e\u5730\u5c06\u6570\u636e\u4ee5\u8868\u683c\u5f62\u5f0f\u8f93\u51fa\u3002\u5b83\u63d0\u4f9b\u4e86<code>DataFrame<\/code>\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5904\u7406\u548c\u663e\u793a\u4e8c\u7ef4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u548c\u5bfc\u5165PANDAS<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>pandas<\/code>\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86<code>pandas<\/code>\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528pip\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<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165<code>pandas<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u521b\u5efa\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u8981\u6253\u5370\u8868\u683c\uff0c\u9996\u5148\u9700\u8981\u5c06\u6570\u636e\u5b58\u50a8\u5728<code>pandas<\/code>\u7684<code>DataFrame<\/code>\u4e2d\u3002<code>DataFrame<\/code>\u53ef\u4ee5\u4ece\u591a\u79cd\u6570\u636e\u7ed3\u6784\u4e2d\u521b\u5efa\uff0c\u4f8b\u5982\u5b57\u5178\u3001\u5217\u8868\u3001Numpy\u6570\u7ec4\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u5217\u6570\u636e\u7684<code>DataFrame<\/code>\uff1a\u59d3\u540d\u3001\u5e74\u9f84\u548c\u57ce\u5e02\u3002<\/p>\n<\/p>\n<p><h3>3. \u6253\u5370\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u521b\u5efa<code>DataFrame<\/code>\u540e\uff0c\u53ef\u4ee5\u76f4\u63a5\u6253\u5370\u5b83\u3002<code>pandas<\/code>\u4f1a\u81ea\u52a8\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u4f7f\u5176\u770b\u8d77\u6765\u50cf\u4e00\u4e2a\u8868\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>      Name  Age         City<\/p>\n<p>0    Alice   25     New York<\/p>\n<p>1      Bob   30  Los Angeles<\/p>\n<p>2  Charlie   35      Chicago<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u683c\u5f0f\u5df2\u7ecf\u9002\u5408\u4e8e\u5927\u591a\u6570\u7b80\u5355\u7684\u8868\u683c\u6253\u5370\u9700\u6c42\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e8c\u3001\u683c\u5f0f\u5316\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u5bf9\u8868\u683c\u8fdb\u884c\u66f4\u590d\u6742\u7684\u683c\u5f0f\u5316\uff0c\u4ee5\u9002\u5e94\u7279\u5b9a\u7684\u9700\u6c42\u6216\u7f8e\u89c2\u7684\u8981\u6c42\u3002<code>pandas<\/code>\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u6570\u636e\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>1. \u8bbe\u7f6e\u5217\u5bbd\u548c\u5bf9\u9f50\u65b9\u5f0f<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u7684\u9009\u9879\u6765\u8bbe\u7f6e\u5217\u5bbd\u548c\u5bf9\u9f50\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e\u663e\u793a\u7684\u6700\u5927\u5217\u5bbd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pd.set_option(&#39;display.max_colwidth&#39;, 100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u8bbe\u7f6e\u5217\u5bf9\u9f50\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pd.set_option(&#39;display.colheader_justify&#39;, &#39;center&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u63a7\u5236\u5c0f\u6570\u70b9\u7cbe\u5ea6<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u6570\u636e\u4e2d\u5305\u542b\u6d6e\u70b9\u6570\uff0c\u53ef\u80fd\u9700\u8981\u63a7\u5236\u5c0f\u6570\u70b9\u7cbe\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pd.set_option(&#39;display.float_format&#39;, &#39;{:.2f}&#39;.format)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u5e94\u7528\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u683c\u5f0f\u5316\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u7684\u6837\u5f0f\u7cfb\u7edf\u3002\u4f8b\u5982\uff0c\u5e94\u7528\u6761\u4ef6\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.style.applymap(lambda x: &#39;color: red&#39; if x &lt; 30 else &#39;color: green&#39;, subset=[&#39;Age&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u683c\u5f0f\u5316\u53ef\u4ee5\u5e2e\u52a9\u7a81\u51fa\u663e\u793a\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e09\u3001\u4f7f\u7528TABULATE\u5e93<\/p>\n<\/p>\n<p><p><code>tabulate<\/code>\u5e93\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Python\u5e93\uff0c\u7528\u4e8e\u5c06\u5217\u8868\u3001\u5b57\u5178\u7b49\u6570\u636e\u7ed3\u6784\u4ee5\u6f02\u4eae\u7684\u8868\u683c\u5f62\u5f0f\u8f93\u51fa\u3002\u4e0e<code>pandas<\/code>\u76f8\u6bd4\uff0c<code>tabulate<\/code>\u66f4\u52a0\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u4e8e\u9700\u8981\u5feb\u901f\u8f93\u51fa\u683c\u5f0f\u5316\u8868\u683c\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u548c\u5bfc\u5165TABULATE<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5<code>tabulate<\/code>\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tabulate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5bfc\u5165\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from tabulate import tabulate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u521b\u5efa\u548c\u6253\u5370\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>tabulate<\/code>\u53ef\u4ee5\u5c06\u5217\u8868\u6216\u5b57\u5178\u6570\u636e\u8f93\u51fa\u4e3a\u8868\u683c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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<p>headers = [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;]<\/p>\n<p>table = tabulate(data, headers, tablefmt=&#39;grid&#39;)<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>+---------+-----+-------------+<\/p>\n<p>| Name    | Age | City        |<\/p>\n<p>+---------+-----+-------------+<\/p>\n<p>| Alice   |  25 | New York    |<\/p>\n<p>| Bob     |  30 | Los Angeles |<\/p>\n<p>| Charlie |  35 | Chicago     |<\/p>\n<p>+---------+-----+-------------+<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u6570\u636e\u548c\u5934\u90e8\u4fe1\u606f\uff0c\u7136\u540e\u8c03\u7528<code>tabulate<\/code>\u51fd\u6570\u751f\u6210\u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>3. \u591a\u79cd\u683c\u5f0f<\/h3>\n<\/p>\n<p><p><code>tabulate<\/code>\u652f\u6301\u591a\u79cd\u8f93\u51fa\u683c\u5f0f\uff0c\u5305\u62ec<code>pl<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/code>\u3001<code>grid<\/code>\u3001<code>pipe<\/code>\u7b49\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">table_plain = tabulate(data, headers, tablefmt=&#39;plain&#39;)<\/p>\n<p>table_pipe = tabulate(data, headers, tablefmt=&#39;pipe&#39;)<\/p>\n<p>print(table_plain)<\/p>\n<p>print(table_pipe)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u683c\u5f0f\u9002\u5408\u4e0d\u540c\u7684\u663e\u793a\u9700\u6c42\uff0c\u4f8b\u5982<code>grid<\/code>\u683c\u5f0f\u9002\u5408\u4e8e\u7ec8\u7aef\u663e\u793a\uff0c\u800c<code>pipe<\/code>\u683c\u5f0f\u9002\u5408\u4e8eMarkdown\u6587\u6863\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u56db\u3001\u4f7f\u7528PRETTYTABLE\u5e93<\/p>\n<\/p>\n<p><p><code>prettytable<\/code>\u5e93\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u5728Python\u4e2d\u6253\u5370\u8868\u683c\u7684\u5de5\u5177\u3002\u5b83\u652f\u6301\u591a\u79cd\u683c\u5f0f\u548c\u6837\u5f0f\u9009\u9879\uff0c\u9002\u7528\u4e8e\u9700\u8981\u590d\u6742\u683c\u5f0f\u5316\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u548c\u5bfc\u5165PRETTYTABLE<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86<code>prettytable<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install prettytable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5bfc\u5165\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u521b\u5efa\u548c\u6253\u5370\u8868\u683c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>PrettyTable<\/code>\u7c7b\u53ef\u4ee5\u521b\u5efa\u8868\u683c\uff0c\u5e76\u6dfb\u52a0\u884c\u548c\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">table = PrettyTable()<\/p>\n<p>table.field_names = [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;]<\/p>\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<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>+---------+-----+-------------+<\/p>\n<p>|  Name   | Age |    City     |<\/p>\n<p>+---------+-----+-------------+<\/p>\n<p>|  Alice  |  25 |  New York   |<\/p>\n<p>|   Bob   |  30 | Los Angeles |<\/p>\n<p>| Charlie |  35 |   Chicago   |<\/p>\n<p>+---------+-----+-------------+<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u81ea\u5b9a\u4e49\u6837\u5f0f<\/h3>\n<\/p>\n<p><p><code>prettytable<\/code>\u5141\u8bb8\u81ea\u5b9a\u4e49\u8868\u683c\u6837\u5f0f\uff0c\u5982\u8fb9\u6846\u5b57\u7b26\u3001\u5bf9\u9f50\u65b9\u5f0f\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">table.align[&#39;Name&#39;] = &#39;l&#39;<\/p>\n<p>table.border = False<\/p>\n<p>table.header = True<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u914d\u7f6e\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u81ea\u5b9a\u4e49\u8868\u683c\u7684\u663e\u793a\u98ce\u683c\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e94\u3001\u624b\u52a8\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u7b80\u5355\u573a\u666f\u4e0b\uff0c\u4f60\u53ef\u80fd\u4e0d\u9700\u8981\u4f7f\u7528\u989d\u5916\u7684\u5e93\uff0c\u800c\u662f\u624b\u52a8\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u6765\u6253\u5370\u8868\u683c\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u5982\u524d\u9762\u7684\u65b9\u6cd5\u7075\u6d3b\uff0c\u4f46\u5728\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\u65f6\u975e\u5e38\u6709\u6548\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u6253\u5370\u8868\u683c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/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<p>for row in data:<\/p>\n<p>    print(&quot;{:&lt;10} {:&lt;5} {:&lt;12}&quot;.format(*row))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>Name       Age   City        <\/p>\n<p>Alice      25    New York    <\/p>\n<p>Bob        30    Los Angeles <\/p>\n<p>Charlie    35    Chicago     <\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u81ea\u5b9a\u4e49\u5217\u5bbd<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u957f\u5ea6\u81ea\u5b9a\u4e49\u5217\u5bbd\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u6570\u636e\u96c6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">col_widths = [max(len(str(item)) for item in col) for col in zip(*data)]<\/p>\n<p>for row in data:<\/p>\n<p>    print(&quot;&quot;.join(&quot;{:&lt;{}}&quot;.format(item, col_width) for item, col_width in zip(row, col_widths)))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u6bcf\u5217\u7684\u5bbd\u5ea6\u6839\u636e\u5176\u5185\u5bb9\u81ea\u52a8\u8c03\u6574\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u6253\u5370\u8868\u683c\uff0c\u4ece\u7b80\u5355\u7684\u624b\u52a8\u683c\u5f0f\u5316\u5230\u4f7f\u7528\u529f\u80fd\u5f3a\u5927\u7684\u5e93\u5982<code>pandas<\/code>\u3001<code>tabulate<\/code>\u548c<code>prettytable<\/code>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4f60\u7684\u6570\u636e\u5c55\u793a\u66f4\u6e05\u6670\u3001\u66f4\u4e13\u4e1a\u3002\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u662f\u5173\u952e\u3002\u65e0\u8bba\u662f\u5904\u7406\u7b80\u5355\u7684\u6570\u636e\u96c6\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u5206\u6790\uff0cPython\u90fd\u6709\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u6765\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u6253\u5370\u4e00\u4e2a\u7b80\u5355\u7684\u8868\u683c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u521b\u5efa\u548c\u6253\u5370\u7b80\u5355\u7684\u8868\u683c\u3002\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>pandas<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5<code>pandas<\/code>\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aDataFrame\u5bf9\u8c61\u6765\u6784\u5efa\u8868\u683c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {\n    &#39;\u59d3\u540d&#39;: [&#39;\u5f20\u4e09&#39;, &#39;\u674e\u56db&#39;, &#39;\u738b\u4e94&#39;],\n    &#39;\u5e74\u9f84&#39;: [25, 30, 22],\n    &#39;\u57ce\u5e02&#39;: [&#39;\u5317\u4eac&#39;, &#39;\u4e0a\u6d77&#39;, &#39;\u5e7f\u5dde&#39;]\n}\n\ndf = pd.DataFrame(data)\nprint(df)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u540e\uff0c\u60a8\u5c06\u770b\u5230\u4e00\u4e2a\u683c\u5f0f\u826f\u597d\u7684\u8868\u683c\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u6253\u5370\u8868\u683c\uff1f<\/strong><br \/>\u9664\u4e86<code>pandas<\/code>\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u6253\u5370\u8868\u683c\uff0c\u4f8b\u5982<code>PrettyTable<\/code>\u548c<code>tabulate<\/code>\u3002<code>PrettyTable<\/code>\u5141\u8bb8\u60a8\u8f7b\u677e\u5730\u521b\u5efa\u548c\u683c\u5f0f\u5316\u8868\u683c\uff0c\u800c<code>tabulate<\/code>\u5219\u63d0\u4f9b\u4e86\u591a\u79cd\u8f93\u51fa\u683c\u5f0f\uff0c\u4f8b\u5982\u6587\u672c\u3001HTML\u548cLaTeX\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u8ba9\u60a8\u7684\u8f93\u51fa\u66f4\u52a0\u7f8e\u89c2\u548c\u5b9e\u7528\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>PrettyTable<\/code>\u7684\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">from prettytable import PrettyTable\n\ntable = PrettyTable()\ntable.field_names = [&quot;\u59d3\u540d&quot;, &quot;\u5e74\u9f84&quot;, &quot;\u57ce\u5e02&quot;]\ntable.add_row([&quot;\u5f20\u4e09&quot;, 25, &quot;\u5317\u4eac&quot;])\ntable.add_row([&quot;\u674e\u56db&quot;, 30, &quot;\u4e0a\u6d77&quot;])\ntable.add_row([&quot;\u738b\u4e94&quot;, 22, &quot;\u5e7f\u5dde&quot;])\n\nprint(table)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u8868\u683c\u8f93\u51fa\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u5c06\u8868\u683c\u6570\u636e\u5bfc\u51fa\u5230\u591a\u79cd\u6587\u4ef6\u683c\u5f0f\uff0c\u4f8b\u5982CSV\u6216Excel\u3002\u4f7f\u7528<code>DataFrame<\/code>\u7684<code>to_csv<\/code>\u6216<code>to_excel<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">df.to_csv(&#39;output.csv&#39;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06DataFrame\u4e2d\u7684\u6570\u636e\u4fdd\u5b58\u5230\u540d\u4e3a<code>output.csv<\/code>\u7684\u6587\u4ef6\u4e2d\uff0c<code>index=False<\/code>\u8868\u793a\u4e0d\u4fdd\u5b58\u884c\u7d22\u5f15\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u4e0e\u5176\u4ed6\u5de5\u5177\u5171\u4eab\u6570\u636e\u6216\u8fdb\u884c\u540e\u7eed\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u6253\u5370\u8868\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001tabulate\u5e93\u3001prettytabl [&hellip;]","protected":false},"author":3,"featured_media":968615,"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\/968605"}],"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=968605"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968605\/revisions"}],"predecessor-version":[{"id":968618,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968605\/revisions\/968618"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/968615"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=968605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=968605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=968605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}