{"id":1044410,"date":"2024-12-31T13:14:19","date_gmt":"2024-12-31T05:14:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1044410.html"},"modified":"2024-12-31T13:14:21","modified_gmt":"2024-12-31T05:14:21","slug":"python%e5%a6%82%e4%bd%95%e5%89%8d%e6%95%b0%e6%8d%ae%e8%a1%a8%e6%a0%bc%e5%8c%96%e5%b1%95%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1044410.html","title":{"rendered":"Python\u5982\u4f55\u524d\u6570\u636e\u8868\u683c\u5316\u5c55\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c5396383-bcef-4b59-b3cc-197c9808c90d.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u524d\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\uff0c\u5305\u62ec\u4f7f\u7528pandas\u3001tabulate\u3001PrettyTable\u7b49\u5e93\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>pandas<\/strong> \u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\u3001\u7075\u6d3b\u6027\u9ad8\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u5927\u6570\u636e\u96c6\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 Pandas<\/h3>\n<\/p>\n<p><p><strong>Pandas<\/strong> \u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684Python\u5e93\uff0c\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 pandas \u5c06\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1.1 \u5b89\u88c5 Pandas<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5 pandas \u5e93\u3002\u4f60\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<p><h4>1.2 \u521b\u5efa DataFrame<\/h4>\n<\/p>\n<p><p>DataFrame \u662f pandas \u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\u3002\u4f60\u53ef\u4ee5\u4ece\u5b57\u5178\u3001\u5217\u8868\u6216\u8005\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u521b\u5efa DataFrame\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [24, 27, 22],<\/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>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\u4e00\u4e2a\u8868\u683c\u5f62\u5f0f\u7684\u6570\u636e\u5c55\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code>      Name  Age         City<\/p>\n<p>0    Alice   24     New York<\/p>\n<p>1      Bob   27  Los Angeles<\/p>\n<p>2  Charlie   22      Chicago<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3 \u8bfb\u53d6\u5916\u90e8\u6570\u636e<\/h4>\n<\/p>\n<p><p>Pandas \u8fd8\u53ef\u4ee5\u4ece\u5916\u90e8\u6570\u636e\u6e90\u8bfb\u53d6\u6570\u636e\uff0c\u6bd4\u5982 CSV \u6587\u4ef6\u3001Excel \u6587\u4ef6\u3001SQL \u6570\u636e\u5e93\u7b49\u3002\u4f8b\u5982\uff0c\u8bfb\u53d6\u4e00\u4e2a CSV \u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p>print(df.head())  # \u6253\u5370\u524d\u4e94\u884c\u6570\u636e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 Tabulate<\/h3>\n<\/p>\n<p><p><strong>Tabulate<\/strong> \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5e93\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u4ee5\u8868\u683c\u5f62\u5f0f\u5c55\u793a\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u5c55\u793a\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b89\u88c5 Tabulate<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5 tabulate \u5e93\u3002\u4f60\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<p><h4>2.2 \u4f7f\u7528 Tabulate<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528 tabulate \u5e93\u5c06\u5217\u8868\u6216\u5b57\u5178\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from tabulate import tabulate<\/p>\n<p>data = [<\/p>\n<p>    [&#39;Alice&#39;, 24, &#39;New York&#39;],<\/p>\n<p>    [&#39;Bob&#39;, 27, &#39;Los Angeles&#39;],<\/p>\n<p>    [&#39;Charlie&#39;, 22, &#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;pretty&#39;)<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\u4e00\u4e2a\u6f02\u4eae\u7684\u8868\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code>+---------+-----+-------------+<\/p>\n<p>|   Name  | Age |    City     |<\/p>\n<p>+---------+-----+-------------+<\/p>\n<p>|  Alice  |  24 |  New York   |<\/p>\n<p>|   Bob   |  27 | Los Angeles |<\/p>\n<p>| Charlie |  22 |   Chicago   |<\/p>\n<p>+---------+-----+-------------+<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 PrettyTable<\/h3>\n<\/p>\n<p><p><strong>PrettyTable<\/strong> \u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u5c06\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\u7684\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u751f\u6210 ASCII \u8868\u683c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5 PrettyTable<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5 PrettyTable \u5e93\u3002\u4f60\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<p><h4>3.2 \u4f7f\u7528 PrettyTable<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528 PrettyTable \u5e93\u5c06\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<p>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;, 24, &#39;New York&#39;])<\/p>\n<p>table.add_row([&#39;Bob&#39;, 27, &#39;Los Angeles&#39;])<\/p>\n<p>table.add_row([&#39;Charlie&#39;, 22, &#39;Chicago&#39;])<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\u4e00\u4e2a\u6f02\u4eae\u7684\u8868\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code>+---------+-----+-------------+<\/p>\n<p>|   Name  | Age |    City     |<\/p>\n<p>+---------+-----+-------------+<\/p>\n<p>|  Alice  |  24 |  New York   |<\/p>\n<p>|   Bob   |  27 | Los Angeles |<\/p>\n<p>| Charlie |  22 |   Chicago   |<\/p>\n<p>+---------+-----+-------------+<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 IPython.display<\/h3>\n<\/p>\n<p><p><strong>IPython.display<\/strong> \u662f\u53e6\u4e00\u4e2a\u53ef\u4ee5\u5c06\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\u7684\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8e Jupyter Notebook \u73af\u5883\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u5b89\u88c5 IPython<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5 IPython \u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install ipython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528 IPython.display<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528 IPython.display \u5e93\u5c06\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from IPython.display import display<\/p>\n<p>import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [24, 27, 22],<\/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>display(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 Jupyter Notebook \u4e2d\uff0c\u8fd9\u5c06\u4ee5 HTML \u8868\u683c\u7684\u5f62\u5f0f\u5c55\u793a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5c06\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\u3002<strong>Pandas<\/strong> \u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5c55\u793a\u6570\u636e\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u3002<strong>Tabulate<\/strong> \u548c <strong>PrettyTable<\/strong> \u662f\u4e24\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5e93\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u5c55\u793a\u6570\u636e\u7684\u573a\u666f\u3002<strong>IPython.display<\/strong> \u5219\u9002\u7528\u4e8e Jupyter Notebook \u73af\u5883\u4e2d\u7684\u6570\u636e\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\uff0c\u5e76\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u5b9e\u73b0\u6570\u636e\u7684\u8868\u683c\u5316\u5c55\u793a\u3002\u5176\u4e2d\uff0cPandas\u662f\u6700\u5e38\u7528\u7684\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u529f\u80fd\u3002\u901a\u8fc7Pandas\u7684DataFrame\u7ed3\u6784\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u52a0\u8f7d\u5230\u8868\u683c\u4e2d\uff0c\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u6392\u5e8f\u3001\u7b5b\u9009\u548c\u6c47\u603b\u3002\u6b64\u5916\uff0c\u7ed3\u5408Matplotlib\u548cSeaborn\u7b49\u53ef\u89c6\u5316\u5e93\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u4ee5\u66f4\u52a0\u7f8e\u89c2\u7684\u5f62\u5f0f\u5c55\u793a\u3002<\/p>\n<p><strong>\u54ea\u4e9bPython\u5e93\u9002\u5408\u8fdb\u884c\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\uff1f<\/strong><br \/>\u9664\u4e86Pandas\uff0c\u7528\u6237\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u8fdb\u884c\u6570\u503c\u8fd0\u7b97\uff0c\u4f7f\u7528PrettyTable\u548cTabulate\u7b49\u5e93\u6765\u521b\u5efa\u7b80\u5355\u7684ASCII\u8868\u683c\u3002\u8fd9\u4e9b\u5e93\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u5feb\u901f\u5c06\u6570\u636e\u683c\u5f0f\u5316\u4e3a\u6613\u8bfb\u7684\u8868\u683c\u5f62\u5f0f\u3002\u5bf9\u4e8e\u9700\u8981\u5728\u7f51\u9875\u4e0a\u5c55\u793a\u6570\u636e\u7684\u7528\u6237\uff0c\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u7b49Web\u6846\u67b6\u7ed3\u5408HTML\u8868\u683c\u6765\u5b9e\u73b0\u5728\u7ebf\u6570\u636e\u5c55\u793a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06Excel\u6216CSV\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u8f6c\u5316\u4e3a\u8868\u683c\u5f62\u5f0f\uff1f<\/strong><br \/>\u901a\u8fc7Pandas\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8bfb\u53d6Excel\u6216CSV\u6587\u4ef6\u3002\u4f7f\u7528<code>pd.read_csv(&#39;file.csv&#39;)<\/code>\u6216<code>pd.read_excel(&#39;file.xlsx&#39;)<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u52a0\u8f7d\u5230DataFrame\u4e2d\u3002\u52a0\u8f7d\u540e\uff0c\u7528\u6237\u53ef\u4ee5\u5229\u7528DataFrame\u7684\u5404\u79cd\u65b9\u6cd5\uff08\u5982<code>head()<\/code>\u3001<code>info()<\/code>\u548c<code>describe()<\/code>\uff09\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u89c8\u548c\u5206\u6790\uff0c\u4ece\u800c\u5b9e\u73b0\u6570\u636e\u7684\u8868\u683c\u5316\u5c55\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u6570\u636e\u8868\u683c\u5316\u5c55\u793a\uff0c\u5305\u62ec\u4f7f\u7528pandas\u3001tabulate\u3001PrettyTable\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1044420,"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\/1044410"}],"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=1044410"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1044410\/revisions"}],"predecessor-version":[{"id":1044425,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1044410\/revisions\/1044425"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1044420"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1044410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1044410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1044410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}