{"id":1066919,"date":"2024-12-31T16:30:37","date_gmt":"2024-12-31T08:30:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1066919.html"},"modified":"2024-12-31T16:30:40","modified_gmt":"2024-12-31T08:30:40","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%9b%b4%e6%8e%a5%e6%89%93%e5%8d%b0%e4%bf%a1%e6%81%af%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1066919.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u76f4\u63a5\u6253\u5370\u4fe1\u606f\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/aae40eb5-d39c-4b86-92e8-6ada39702c3f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u76f4\u63a5\u6253\u5370\u4fe1\u606f\u8868\" \/><\/p>\n<p><p> <strong>Python\u5b9e\u73b0\u76f4\u63a5\u6253\u5370\u4fe1\u606f\u8868<\/strong>\u7684\u6838\u5fc3\u89c2\u70b9\u5305\u62ec\uff1a\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93\u3001\u4f7f\u7528<code>tabulate<\/code>\u5e93\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u5b9e\u73b0\u76f4\u63a5\u6253\u5370\u4fe1\u606f\u8868\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>pandas<\/code>\u5e93<\/strong>\u662f\u5b9e\u73b0\u76f4\u63a5\u6253\u5370\u4fe1\u606f\u8868\u7684\u6700\u4f73\u9009\u62e9\u4e4b\u4e00\u3002<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5c55\u793a\u529f\u80fd\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u7ec4\u7ec7\u6210\u8868\u683c\u5f62\u5f0f\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u901a\u8fc7<code>pandas<\/code>\u5e93\u521b\u5efa\u4e00\u4e2a\u4fe1\u606f\u8868\u5e76\u6253\u5370\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/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;San Francisco&#39;, &#39;Los Angeles&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6253\u5370\u4fe1\u606f\u8868<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528<code>print<\/code>\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>print<\/code>\u51fd\u6570\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u8f93\u51fa\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u5b57\u7b26\u4e32\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002\u901a\u8fc7\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u7ec4\u7ec7\u6210\u8868\u683c\u5f62\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u636e<\/p>\n<p>names = [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;]<\/p>\n<p>ages = [25, 30, 35]<\/p>\n<p>cities = [&#39;New York&#39;, &#39;San Francisco&#39;, &#39;Los Angeles&#39;]<\/p>\n<h2><strong>\u6253\u5370\u8868\u5934<\/strong><\/h2>\n<p>print(f&quot;{&#39;Name&#39;:&lt;10}{&#39;Age&#39;:&lt;5}{&#39;City&#39;:&lt;15}&quot;)<\/p>\n<h2><strong>\u6253\u5370\u6bcf\u4e00\u884c\u6570\u636e<\/strong><\/h2>\n<p>for name, age, city in zip(names, ages, cities):<\/p>\n<p>    print(f&quot;{name:&lt;10}{age:&lt;5}{city:&lt;15}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u4ee3\u7801\u53ef\u80fd\u4f1a\u663e\u5f97\u5197\u957f\u4e14\u96be\u4ee5\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93<\/strong><\/p>\n<\/p>\n<p><p><code>pandas<\/code>\u662fPython\u4e2d\u7684\u4e00\u4e2a\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002\u901a\u8fc7<code>pandas<\/code>\u5e93\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u7ec4\u7ec7\u6210DataFrame\uff0c\u5e76\u76f4\u63a5\u6253\u5370\u51fa\u6765\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/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;San Francisco&#39;, &#39;Los Angeles&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6253\u5370\u4fe1\u606f\u8868<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\u3001\u6613\u4e8e\u7ef4\u62a4\uff0c\u5e76\u4e14\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u4f7f\u7528<code>tabulate<\/code>\u5e93<\/strong><\/p>\n<\/p>\n<p><p><code>tabulate<\/code>\u5e93\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5e93\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u6216\u5b57\u5178\u6570\u636e\u8f6c\u6362\u4e3a\u6f02\u4eae\u7684\u8868\u683c\u5f62\u5f0f\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from tabulate import tabulate<\/p>\n<h2><strong>\u521b\u5efa\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;San Francisco&#39;],<\/p>\n<p>    [&#39;Charlie&#39;, 35, &#39;Los Angeles&#39;]<\/p>\n<p>]<\/p>\n<h2><strong>\u5b9a\u4e49\u8868\u5934<\/strong><\/h2>\n<p>headers = [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;]<\/p>\n<h2><strong>\u6253\u5370\u4fe1\u606f\u8868<\/strong><\/h2>\n<p>print(tabulate(data, headers, tablefmt=&#39;grid&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>tabulate<\/code>\u5e93\u7684\u4f18\u70b9\u662f\u8f93\u51fa\u7684\u8868\u683c\u7f8e\u89c2\uff0c\u4ee3\u7801\u7b80\u6d01\uff0c\u9002\u5408\u5c55\u793a\u5c0f\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5927\u89c4\u6a21\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [f&#39;Person {i}&#39; for i in range(1, 1001)],<\/p>\n<p>    &#39;Age&#39;: np.random.randint(20, 60, 1000),<\/p>\n<p>    &#39;City&#39;: [f&#39;City {i%10}&#39; for i in range(1, 1001)]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6253\u5370\u524d10\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(df.head(10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>head<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u770b\u524d\u51e0\u884c\u6570\u636e\uff0c\u9002\u5408\u5feb\u901f\u6d4f\u89c8\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u8f93\u51fa\u7684\u8868\u683c\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/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;San Francisco&#39;, &#39;Los Angeles&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/h2>\n<p>print(df.to_string(index=False, justify=&#39;center&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>to_string<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9\u8f93\u51fa\u7684\u8868\u683c\u8fdb\u884c\u66f4\u7ec6\u81f4\u7684\u63a7\u5236\uff0c\u4f8b\u5982\u53bb\u9664\u7d22\u5f15\u3001\u5c45\u4e2d\u5bf9\u9f50\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u516d\u3001\u5bfc\u51fa\u5230\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u9700\u8981\u5c06\u4fe1\u606f\u8868\u5bfc\u51fa\u5230\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5bfc\u51fa\u5230CSV\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/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;San Francisco&#39;, &#39;Los Angeles&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5bfc\u51fa\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;info_table.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>to_csv<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u4fe1\u606f\u8868\u5bfc\u51fa\u4e3aCSV\u6587\u4ef6\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e03\u3001\u4ece\u6587\u4ef6\u8bfb\u53d6<\/strong><\/p>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u4e5f\u53ef\u4ee5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u6253\u5370\u4fe1\u606f\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4eceCSV\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u5e76\u6253\u5370\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4eceCSV\u6587\u4ef6\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;info_table.csv&#39;)<\/p>\n<h2><strong>\u6253\u5370\u4fe1\u606f\u8868<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>read_csv<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4eceCSV\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u5e76\u6253\u5370\u6210\u4fe1\u606f\u8868\u3002<\/p>\n<\/p>\n<p><p><strong>\u516b\u3001\u4e0e\u5176\u4ed6\u5e93\u7ed3\u5408<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u4fe1\u606f\u8868\u4e0e\u5176\u4ed6\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u591a\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7ed3\u5408<code>matplotlib<\/code>\u5e93\u7ed8\u5236\u56fe\u8868\uff0c\u7ed3\u5408<code>openpyxl<\/code>\u5e93\u8bfb\u5199Excel\u6587\u4ef6\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed3\u5408<code>matplotlib<\/code>\u5e93\u7ed8\u5236\u56fe\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/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;San Francisco&#39;, &#39;Los Angeles&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>df.plot(kind=&#39;bar&#39;, x=&#39;Name&#39;, y=&#39;Age&#39;, title=&#39;Age of People&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u5176\u4ed6\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u591a\u529f\u80fd\uff0c\u6ee1\u8db3\u4e0d\u540c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u76f4\u63a5\u6253\u5370\u4fe1\u606f\u8868\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u57fa\u7840\u7684<code>print<\/code>\u51fd\u6570\uff0c\u8fd8\u662f\u5f3a\u5927\u7684<code>pandas<\/code>\u5e93\uff0c\u4ea6\u6216\u662f\u7f8e\u89c2\u7684<code>tabulate<\/code>\u5e93\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u548c\u7ec4\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4fe1\u606f\u8868\u5e76\u6253\u5370\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u521b\u5efa\u548c\u6253\u5370\u4fe1\u606f\u8868\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5pandas\u5e93\uff0c\u901a\u8fc7\u547d\u4ee4<code>pip install pandas<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>DataFrame<\/code>\u6784\u5efa\u8868\u683c\uff0c\u5e76\u8c03\u7528<code>print()<\/code>\u51fd\u6570\u6216\u76f4\u63a5\u8f93\u51fa<code>DataFrame<\/code>\u5bf9\u8c61\u6765\u6253\u5370\u4fe1\u606f\u8868\u3002\u4f8b\u5982\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><strong>\u5982\u4f55\u683c\u5f0f\u5316\u6253\u5370\u4fe1\u606f\u8868\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u4fe1\u606f\u8868\u7684\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u7684<code>to_string()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5141\u8bb8\u4f60\u8bbe\u7f6e\u6253\u5370\u65f6\u7684\u884c\u6570\u3001\u5217\u5bbd\u548c\u5176\u4ed6\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">print(df.to_string(index=False, justify=&#39;center&#39;))\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5c06\u53bb\u6389\u7d22\u5f15\uff0c\u5e76\u4f7f\u6bcf\u4e00\u5217\u7684\u5185\u5bb9\u5c45\u4e2d\u5bf9\u9f50\uff0c\u4ece\u800c\u63d0\u5347\u6574\u4f53\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u7528\u54ea\u4e9b\u5176\u4ed6\u5e93\u5728Python\u4e2d\u6253\u5370\u4fe1\u606f\u8868\uff1f<\/strong><br \/>\u9664\u4e86pandas\uff0cPython\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u7528\u4e8e\u6253\u5370\u4fe1\u606f\u8868\u3002\u4f8b\u5982\uff0cPrettyTable\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4ee5\u7f8e\u89c2\u7684\u8868\u683c\u5f62\u5f0f\u5c55\u793a\u6570\u636e\u3002\u5b89\u88c5PrettyTable\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install PrettyTable<\/code>\u6765\u5b9e\u73b0\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\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>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4f7f\u8f93\u51fa\u7684\u8868\u683c\u66f4\u52a0\u7f8e\u89c2\uff0c\u9002\u5408\u9700\u8981\u5728\u63a7\u5236\u53f0\u4e2d\u5c55\u793a\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9e\u73b0\u76f4\u63a5\u6253\u5370\u4fe1\u606f\u8868\u7684\u6838\u5fc3\u89c2\u70b9\u5305\u62ec\uff1a\u4f7f\u7528print\u51fd\u6570\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528tabulate\u5e93\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1066932,"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\/1066919"}],"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=1066919"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066919\/revisions"}],"predecessor-version":[{"id":1066936,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1066919\/revisions\/1066936"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1066932"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1066919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1066919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1066919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}