{"id":992049,"date":"2024-12-27T08:34:07","date_gmt":"2024-12-27T00:34:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/992049.html"},"modified":"2024-12-27T08:34:09","modified_gmt":"2024-12-27T00:34:09","slug":"python%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e5%a4%9a%e4%ba%86%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/992049.html","title":{"rendered":"python\u5982\u4f55\u663e\u793a\u591a\u4e86\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070052\/7efb6e1e-5503-44ae-bc4e-05b2f20c69bf.webp\" alt=\"python\u5982\u4f55\u663e\u793a\u591a\u4e86\u5217\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528\u6570\u636e\u5904\u7406\u5e93Pandas\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u663e\u793a\u591a\u5217\u6570\u636e\u3001Pandas\u5e93\u5177\u6709\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u80fd\u591f\u5904\u7406\u591a\u7ef4\u6570\u636e\u3001\u4f7f\u7528DataFrame\u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u81ea\u5b9a\u4e49\u663e\u793a\u7684\u5217\u6570<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Pandas\u5e93\u6765\u663e\u793a\u591a\u5217\u6570\u636e\uff0c\u5e76\u63a2\u8ba8\u5982\u4f55\u6839\u636e\u5177\u4f53\u9700\u6c42\u8c03\u6574\u663e\u793a\u7684\u5217\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001PANDAS\u5e93\u7b80\u4ecb<\/p>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u6570\u636e\u5206\u6790\u5e93\u4e4b\u4e00\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u80fd\u591f\u8f7b\u677e\u5730\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u64cd\u4f5c\u548c\u53ef\u89c6\u5316\u3002Pandas\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u662fSeries\uff08\u5e8f\u5217\uff09\u548cDataFrame\uff08\u6570\u636e\u6846\uff09\uff0c\u5176\u4e2dDataFrame\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6807\u8bb0\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u7535\u5b50\u8868\u683c\u6216SQL\u8868\u3002<\/p>\n<\/p>\n<p><p>1\u3001Series\u4e0eDataFrame\u7684\u533a\u522b<\/p>\n<\/p>\n<p><p>Series\u662f\u4e00\u4e2a\u4e00\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8ePython\u7684\u5217\u8868\u6216\u6570\u7ec4\uff0c\u4f46\u5177\u6709\u6807\u7b7e\u3002DataFrame\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u770b\u4f5c\u662f\u591a\u4e2aSeries\u7684\u96c6\u5408\uff0c\u6bcf\u4e2aSeries\u4ee3\u8868DataFrame\u4e2d\u7684\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><p>2\u3001Pandas\u7684\u5b89\u88c5\u4e0e\u5bfc\u5165<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Pandas\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u8fd9\u4e2a\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<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5bfc\u5165Pandas\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u5e76\u663e\u793aDataFrame<\/p>\n<\/p>\n<p><p>1\u3001\u521b\u5efaDataFrame\u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u8981\u521b\u5efa\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u3001\u5217\u8868\u3001NumPy\u6570\u7ec4\u6216\u5176\u4ed6DataFrame\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u5b57\u5178\u521b\u5efaDataFrame\u7684\u793a\u4f8b\uff1a<\/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>2\u3001\u663e\u793aDataFrame<\/p>\n<\/p>\n<p><p>\u521b\u5efaDataFrame\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u663e\u793aDataFrame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51faDataFrame\u7684\u6240\u6709\u884c\u548c\u5217\u3002\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cPandas\u4f1a\u663e\u793a\u6240\u6709\u5217\uff0c\u4f46\u5982\u679c\u5217\u6570\u8fc7\u591a\uff0c\u53ef\u80fd\u4f1a\u88ab\u622a\u65ad\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8c03\u6574DataFrame\u7684\u663e\u793a\u8bbe\u7f6e<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528<code>pd.set_option<\/code>\u8c03\u6574\u663e\u793a\u8bbe\u7f6e<\/p>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86<code>set_option<\/code>\u51fd\u6570\uff0c\u5141\u8bb8\u7528\u6237\u8bbe\u7f6e\u663e\u793a\u7684\u6700\u5927\u884c\u6570\u548c\u5217\u6570\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6700\u5927\u5217\u6570\u8bbe\u7f6e\u4e3a20\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pd.set_option(&#39;display.max_columns&#39;, 20)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u663e\u793a\u7684\u5217\u6570\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u663e\u793a\u6240\u6709\u5217<\/p>\n<\/p>\n<p><p>\u5982\u679c\u5e0c\u671b\u663e\u793aDataFrame\u7684\u6240\u6709\u5217\uff0c\u53ef\u4ee5\u5c06<code>display.max_columns<\/code>\u8bbe\u7f6e\u4e3a<code>None<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pd.set_option(&#39;display.max_columns&#39;, None)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u786e\u4fdd\u5728\u8f93\u51faDataFrame\u65f6\uff0c\u4e0d\u4f1a\u622a\u65ad\u4efb\u4f55\u5217\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9009\u62e9\u6027\u663e\u793a\u5217<\/p>\n<\/p>\n<p><p>1\u3001\u9009\u62e9\u7279\u5b9a\u5217\u8fdb\u884c\u663e\u793a<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7528\u6237\u53ef\u80fd\u53ea\u5bf9DataFrame\u4e2d\u7684\u67d0\u4e9b\u5217\u611f\u5174\u8da3\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u540d\u5217\u8868\u6765\u9009\u62e9\u6027\u663e\u793a\u5217\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(df[[&#39;Name&#39;, &#39;City&#39;]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u53ea\u663e\u793a<code>Name<\/code>\u548c<code>City<\/code>\u5217\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528\u6761\u4ef6\u9009\u62e9\u5217<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u9009\u62e9\u5217\u5e76\u663e\u793a\u3002\u4f8b\u5982\uff0c\u9009\u62e9\u5e74\u9f84\u5927\u4e8e30\u7684\u4eba\u7684\u59d3\u540d\u548c\u57ce\u5e02\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">filtered_df = df[df[&#39;Age&#39;] &gt; 30]<\/p>\n<p>print(filtered_df[[&#39;Name&#39;, &#39;City&#39;]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u8c03\u6574\u8f93\u51fa\u683c\u5f0f<\/p>\n<\/p>\n<p><p>1\u3001\u8bbe\u7f6e\u663e\u793a\u5bbd\u5ea6<\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574<code>display.width<\/code>\u9009\u9879\u6765\u8bbe\u7f6e\u8f93\u51fa\u7684\u5bbd\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pd.set_option(&#39;display.width&#39;, 100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5bf9\u4e8e\u62e5\u6709\u5927\u91cf\u5217\u7684DataFrame\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u907f\u514d\u5728\u8f93\u51fa\u65f6\u6362\u884c\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u8bbe\u7f6e\u5217\u7684\u663e\u793a\u683c\u5f0f<\/p>\n<\/p>\n<p><p>\u5982\u679c\u5e0c\u671b\u683c\u5f0f\u5316\u5217\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>style.format<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u683c\u5f0f\u5316<code>Age<\/code>\u5217\u4e3a\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.style.format({&#39;Age&#39;: &#39;{:0.0f}&#39;})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3\u4e0e\u5b9e\u7528\u6280\u5de7<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u5e93\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236DataFrame\u7684\u5217\u663e\u793a\u3002\u901a\u8fc7<code>set_option<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u663e\u793a\u7684\u5217\u6570\uff0c\u5e76\u786e\u4fdd\u5728\u5206\u6790\u6570\u636e\u65f6\u83b7\u5f97\u6700\u4f73\u7684\u53ef\u89c6\u5316\u6548\u679c\u3002\u540c\u65f6\uff0c\u901a\u8fc7\u9009\u62e9\u6027\u663e\u793a\u548c\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u5b9e\u8df5\u4e2d\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u4e0d\u4ec5\u6709\u52a9\u4e8e\u63d0\u9ad8\u6570\u636e\u5206\u6790\u7684\u6548\u7387\uff0c\u8fd8\u80fd\u589e\u5f3a\u6570\u636e\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7406\u89e3\u6027\u3002\u5bf9\u4e8e\u9700\u8981\u7ecf\u5e38\u5904\u7406\u591a\u5217\u6570\u636e\u7684\u7528\u6237\u800c\u8a00\uff0c\u7075\u6d3b\u4f7f\u7528Pandas\u7684\u663e\u793a\u8bbe\u7f6e\u662f\u975e\u5e38\u91cd\u8981\u7684\u6280\u80fd\u3002\u5e0c\u671b\u672c\u6587\u7684\u4ecb\u7ecd\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u663e\u793aDataFrame\u7684\u6240\u6709\u5217\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Pandas\u5e93\u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u663e\u793a\u7684DataFrame\u5217\u6570\u53ef\u80fd\u4f1a\u53d7\u5230\u9650\u5236\u3002\u4e3a\u4e86\u663e\u793a\u6240\u6709\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pd.set_option(&#39;display.max_columns&#39;, None)<\/code>\u6765\u8c03\u6574\u8bbe\u7f6e\u3002\u8fd9\u6837\uff0c\u6253\u5370DataFrame\u65f6\u5c31\u4e0d\u4f1a\u88ab\u622a\u65ad\uff0c\u53ef\u4ee5\u67e5\u770b\u6240\u6709\u5217\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Jupyter Notebook\u4e2d\u663e\u793a\u5b8c\u6574\u7684DataFrame\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\uff0c\u5982\u679c\u9700\u8981\u67e5\u770b\u5b8c\u6574\u7684DataFrame\u800c\u4e0d\u88ab\u622a\u65ad\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pd.set_option(&#39;display.expand_frame_repr&#39;, False)<\/code>\u3002\u8fd9\u4f1a\u786e\u4fddDataFrame\u5728\u8f93\u51fa\u65f6\u4e0d\u4f1a\u6362\u884c\uff0c\u4ece\u800c\u4f7f\u6240\u6709\u5217\u90fd\u5728\u4e00\u884c\u4e2d\u663e\u793a\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u9009\u62e9\u6027\u663e\u793a\u7279\u5b9a\u5217\uff1f<\/strong><br \/>\u5982\u679c\u53ea\u5bf9DataFrame\u4e2d\u7684\u67d0\u4e9b\u5217\u611f\u5174\u8da3\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u540d\u9009\u62e9\u9700\u8981\u7684\u5217\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>df[[&#39;\u5217\u540d1&#39;, &#39;\u5217\u540d2&#39;]]<\/code>\u53ef\u4ee5\u4ec5\u663e\u793a\u6240\u9700\u7684\u5217\u3002\u6b64\u5916\uff0c\u7ed3\u5408<code>pd.options.display.max_columns<\/code>\u7684\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u786e\u4fdd\u8fd9\u4e9b\u5217\u4e0d\u4f1a\u88ab\u622a\u65ad\u663e\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528\u6570\u636e\u5904\u7406\u5e93Pandas\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u663e\u793a\u591a\u5217\u6570\u636e\u3001Pandas\u5e93\u5177\u6709\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd [&hellip;]","protected":false},"author":3,"featured_media":992058,"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\/992049"}],"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=992049"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/992049\/revisions"}],"predecessor-version":[{"id":992060,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/992049\/revisions\/992060"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/992058"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=992049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=992049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=992049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}