{"id":1036768,"date":"2024-12-31T12:09:35","date_gmt":"2024-12-31T04:09:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1036768.html"},"modified":"2024-12-31T12:09:37","modified_gmt":"2024-12-31T04:09:37","slug":"python%e5%a6%82%e4%bd%95%e9%81%8d%e5%8e%86df%e6%89%be%e5%87%ba%e6%9c%80%e5%a4%a7%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1036768.html","title":{"rendered":"python\u5982\u4f55\u904d\u5386df\u627e\u51fa\u6700\u5927\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/2b5cd9c8-1cc0-4b6c-b5d9-fd51c8a5036c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u904d\u5386df\u627e\u51fa\u6700\u5927\u503c\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u904d\u5386DataFrame\u627e\u51fa\u6700\u5927\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ec\u4f7f\u7528<code>max<\/code>\u51fd\u6570\u3001<code>idxmax<\/code>\u51fd\u6570\u3001\u4ee5\u53ca<code>apply<\/code>\u51fd\u6570\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5206\u522b\u5177\u6709\u7b80\u6d01\u6027\u3001\u7075\u6d3b\u6027\u548c\u9ad8\u6548\u6027\u7b49\u7279\u70b9\u3002<\/strong> \u4f7f\u7528<code>max<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u627e\u5230\u6700\u5927\u503c\u3001<code>idxmax<\/code>\u51fd\u6570\u53ef\u4ee5\u627e\u5230\u6700\u5927\u503c\u6240\u5728\u7684\u7d22\u5f15\u3001<code>apply<\/code>\u51fd\u6570\u53ef\u4ee5\u904d\u5386\u6bcf\u4e00\u5217\u6216\u6bcf\u4e00\u884c\u627e\u5230\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>max<\/code>\u51fd\u6570\u627e\u6700\u5927\u503c<\/h3>\n<\/p>\n<p><p><code>max<\/code>\u51fd\u6570\u662fPandas\u5e93\u4e2d\u6700\u7b80\u5355\u76f4\u63a5\u7684\u627e\u6700\u5927\u503c\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u7528\u4e8eDataFrame\u5bf9\u8c61\uff0c\u8fd4\u56de\u6bcf\u5217\u6216\u6bcf\u884c\u7684\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3],<\/p>\n<p>        &#39;B&#39;: [4, 5, 6],<\/p>\n<p>        &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u627e\u5230\u6bcf\u5217\u7684\u6700\u5927\u503c<\/strong><\/h2>\n<p>max_values = df.max()<\/p>\n<p>print(&quot;\u6bcf\u5217\u7684\u6700\u5927\u503c\uff1a&quot;)<\/p>\n<p>print(max_values)<\/p>\n<h2><strong>\u627e\u5230\u6bcf\u884c\u7684\u6700\u5927\u503c<\/strong><\/h2>\n<p>max_values_row = df.max(axis=1)<\/p>\n<p>print(&quot;\u6bcf\u884c\u7684\u6700\u5927\u503c\uff1a&quot;)<\/p>\n<p>print(max_values_row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>idxmax<\/code>\u51fd\u6570\u627e\u6700\u5927\u503c\u7684\u7d22\u5f15<\/h3>\n<\/p>\n<p><p><code>idxmax<\/code>\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u627e\u5230\u6700\u5927\u503c\u6240\u5728\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u8fd9\u5bf9\u4e8e\u8fdb\u4e00\u6b65\u7684\u5206\u6790\u548c\u64cd\u4f5c\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u627e\u5230\u6bcf\u5217\u6700\u5927\u503c\u6240\u5728\u7684\u7d22\u5f15<\/p>\n<p>max_index = df.idxmax()<\/p>\n<p>print(&quot;\u6bcf\u5217\u6700\u5927\u503c\u7684\u7d22\u5f15\uff1a&quot;)<\/p>\n<p>print(max_index)<\/p>\n<h2><strong>\u627e\u5230\u6bcf\u884c\u6700\u5927\u503c\u6240\u5728\u7684\u7d22\u5f15<\/strong><\/h2>\n<p>max_index_row = df.idxmax(axis=1)<\/p>\n<p>print(&quot;\u6bcf\u884c\u6700\u5927\u503c\u7684\u7d22\u5f15\uff1a&quot;)<\/p>\n<p>print(max_index_row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>apply<\/code>\u51fd\u6570\u904d\u5386DataFrame<\/h3>\n<\/p>\n<p><p><code>apply<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5bf9DataFrame\u7684\u6bcf\u5217\u6216\u6bcf\u884c\u8fdb\u884c\u81ea\u5b9a\u4e49\u64cd\u4f5c\u3002\u901a\u8fc7\u4f7f\u7528<code>apply<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u904d\u5386DataFrame\u5e76\u627e\u5230\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u627e\u5230\u6bcf\u5217\u7684\u6700\u5927\u503c<\/p>\n<p>def find_max(column):<\/p>\n<p>    return column.max()<\/p>\n<h2><strong>\u4f7f\u7528apply\u51fd\u6570\u627e\u5230\u6bcf\u5217\u7684\u6700\u5927\u503c<\/strong><\/h2>\n<p>max_values_apply = df.apply(find_max)<\/p>\n<p>print(&quot;\u4f7f\u7528apply\u51fd\u6570\u627e\u5230\u6bcf\u5217\u7684\u6700\u5927\u503c\uff1a&quot;)<\/p>\n<p>print(max_values_apply)<\/p>\n<h2><strong>\u627e\u5230\u6bcf\u884c\u7684\u6700\u5927\u503c<\/strong><\/h2>\n<p>max_values_apply_row = df.apply(lambda x: x.max(), axis=1)<\/p>\n<p>print(&quot;\u4f7f\u7528apply\u51fd\u6570\u627e\u5230\u6bcf\u884c\u7684\u6700\u5927\u503c\uff1a&quot;)<\/p>\n<p>print(max_values_apply_row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u7684\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u5f80\u5f80\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u627e\u51fa\u6700\u5927\u503c\u7684\u540c\u65f6\uff0c\u8fd8\u9700\u8981\u77e5\u9053\u5176\u6240\u5728\u7684\u5217\u548c\u884c\uff0c\u751a\u81f3\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u627e\u5230\u6700\u5927\u503c\u53ca\u5176\u6240\u5728\u7684\u5217\u548c\u884c<\/p>\n<p>max_value = df.max().max()<\/p>\n<p>max_row = df.idxmax(axis=1).loc[df.max(axis=1).idxmax()]<\/p>\n<p>max_col = df.idxmax().loc[df.max().idxmax()]<\/p>\n<p>print(f&quot;\u6700\u5927\u503c: {max_value}&quot;)<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u6240\u5728\u7684\u884c: {max_row}&quot;)<\/p>\n<p>print(f&quot;\u6700\u5927\u503c\u6240\u5728\u7684\u5217: {max_col}&quot;)<\/p>\n<h2><strong>\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u4e00\u6b65\u5904\u7406<\/strong><\/h2>\n<p>max_info = {<\/p>\n<p>    &#39;\u6700\u5927\u503c&#39;: max_value,<\/p>\n<p>    &#39;\u6700\u5927\u503c\u6240\u5728\u7684\u884c&#39;: max_row,<\/p>\n<p>    &#39;\u6700\u5927\u503c\u6240\u5728\u7684\u5217&#39;: max_col<\/p>\n<p>}<\/p>\n<p>print(&quot;\u6700\u5927\u503c\u7684\u8be6\u7ec6\u4fe1\u606f\uff1a&quot;)<\/p>\n<p>print(max_info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u76f8\u5173\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u627e\u5230\u6700\u5927\u503c\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5bf9DataFrame\u8fdb\u884c\u5176\u4ed6\u76f8\u5173\u7684\u64cd\u4f5c\uff0c\u5982\u627e\u6700\u5c0f\u503c\u3001\u7edf\u8ba1\u63cf\u8ff0\u6027\u7edf\u8ba1\u91cf\u7b49\u3002\u8fd9\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u5168\u9762\u5730\u4e86\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u7279\u5f81\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u627e\u5230\u6bcf\u5217\u7684\u6700\u5c0f\u503c<\/p>\n<p>min_values = df.min()<\/p>\n<p>print(&quot;\u6bcf\u5217\u7684\u6700\u5c0f\u503c\uff1a&quot;)<\/p>\n<p>print(min_values)<\/p>\n<h2><strong>\u627e\u5230\u6bcf\u884c\u7684\u6700\u5c0f\u503c<\/strong><\/h2>\n<p>min_values_row = df.min(axis=1)<\/p>\n<p>print(&quot;\u6bcf\u884c\u7684\u6700\u5c0f\u503c\uff1a&quot;)<\/p>\n<p>print(min_values_row)<\/p>\n<h2><strong>\u7edf\u8ba1\u63cf\u8ff0\u6027\u7edf\u8ba1\u91cf<\/strong><\/h2>\n<p>description = df.describe()<\/p>\n<p>print(&quot;\u63cf\u8ff0\u6027\u7edf\u8ba1\u91cf\uff1a&quot;)<\/p>\n<p>print(description)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728Python\u4e2d\u904d\u5386DataFrame\u627e\u51fa\u6700\u5927\u503c\u7684\u65b9\u6cd5\u975e\u5e38\u591a\u6837\u5316\u3002<strong>\u4f7f\u7528<code>max<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u627e\u5230\u6700\u5927\u503c\u3001\u4f7f\u7528<code>idxmax<\/code>\u51fd\u6570\u53ef\u4ee5\u627e\u5230\u6700\u5927\u503c\u6240\u5728\u7684\u7d22\u5f15\u3001\u4f7f\u7528<code>apply<\/code>\u51fd\u6570\u53ef\u4ee5\u904d\u5386\u6bcf\u4e00\u5217\u6216\u6bcf\u4e00\u884c\u627e\u5230\u6700\u5927\u503c\u3002<\/strong> \u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u5f80\u5f80\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\uff0c\u4ece\u800c\u66f4\u5168\u9762\u5730\u4e86\u89e3\u548c\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Pandas\u5e93\u904d\u5386DataFrame\u4ee5\u627e\u51fa\u6700\u5927\u503c\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528Pandas\u5e93\u627e\u51faDataFrame\u7684\u6700\u5927\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>DataFrame.max()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u6bcf\u4e00\u5217\u7684\u6700\u5927\u503c\u3002\u904d\u5386DataFrame\u65f6\uff0c\u4f60\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>iterrows()<\/code>\u6216<code>itertuples()<\/code>\uff0c\u9010\u884c\u68c0\u67e5\u6bcf\u4e2a\u503c\u6765\u786e\u5b9a\u54ea\u4e2a\u662f\u6700\u5927\u503c\u3002<\/p>\n<p><strong>\u5728\u904d\u5386DataFrame\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u627e\u5230\u7684\u6700\u5927\u503c\u662f\u6b63\u786e\u7684\uff1f<\/strong><br \/>\u786e\u4fdd\u627e\u5230\u7684\u6700\u5927\u503c\u51c6\u786e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>DataFrame.max()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6bcf\u5217\u7684\u6700\u5927\u503c\uff0c\u5e76\u4e0e\u904d\u5386\u7ed3\u679c\u8fdb\u884c\u6bd4\u5bf9\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>DataFrame.idxmax()<\/code>\u53ef\u4ee5\u83b7\u5f97\u6700\u5927\u503c\u7684\u7d22\u5f15\uff0c\u5e2e\u52a9\u9a8c\u8bc1\u904d\u5386\u7684\u7ed3\u679c\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u907f\u514d\u9057\u6f0f\u6216\u9519\u8bef\u3002<\/p>\n<p><strong>\u5982\u679cDataFrame\u4e2d\u6709\u7f3a\u5931\u503c\uff0c\u5982\u4f55\u5904\u7406\u4ee5\u627e\u5230\u6700\u5927\u503c\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542b\u7f3a\u5931\u503c\u7684DataFrame\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>DataFrame.max(skipna=True)<\/code>\uff0c\u8fd9\u4e2a\u53c2\u6570\u4f1a\u81ea\u52a8\u5ffd\u7565\u7f3a\u5931\u503c\u3002\u5982\u679c\u5e0c\u671b\u5728\u67e5\u627e\u6700\u5927\u503c\u65f6\u4e0d\u5ffd\u7565\u7f3a\u5931\u503c\uff0c\u53ef\u4ee5\u5c06<code>skipna<\/code>\u8bbe\u7f6e\u4e3a<code>False<\/code>\uff0c\u4f46\u8fd9\u6837\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ed3\u679c\u4e0d\u51c6\u786e\uff0c\u56e0\u6b64\u901a\u5e38\u5efa\u8bae\u4f7f\u7528\u9ed8\u8ba4\u8bbe\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u904d\u5386DataFrame\u627e\u51fa\u6700\u5927\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ec\u4f7f\u7528max\u51fd\u6570\u3001idxmax\u51fd\u6570\u3001\u4ee5\u53ca [&hellip;]","protected":false},"author":3,"featured_media":1036776,"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\/1036768"}],"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=1036768"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036768\/revisions"}],"predecessor-version":[{"id":1036778,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1036768\/revisions\/1036778"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1036776"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1036768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1036768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1036768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}