{"id":204217,"date":"2025-05-29T12:02:13","date_gmt":"2025-05-29T04:02:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204217\/"},"modified":"2025-05-29T12:02:13","modified_gmt":"2025-05-29T04:02:13","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-pandas-%e7%bb%9f%e4%b8%80%e4%bf%ae%e6%94%b9%e6%95%b0%e6%8d%ae%e8%a1%a8%e8%be%93%e5%87%ba%e6%a0%bc%e5%bc%8f%ef%bc%8c%e4%bd%bf%e4%b8%8d%e5%90%8c%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204217\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Pandas \u7edf\u4e00\u4fee\u6539\u6570\u636e\u8868\u8f93\u51fa\u683c\u5f0f\uff0c\u4f7f\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5177\u6709\u7279\u5b9a\u663e\u793a\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Pandas \u7edf\u4e00\u4fee\u6539\u6570\u636e\u8868\u8f93\u51fa\u683c\u5f0f\uff0c\u4f7f\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5177\u6709\u7279\u5b9a\u663e\u793a\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5982\u4f55\u4f7f\u7528 Pandas \u7edf\u4e00\u4fee\u6539\u6570\u636e\u8868\u8f93\u51fa\u683c\u5f0f\uff0c\u4f7f\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5177\u6709\u7279\u5b9a\u663e\u793a\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241101\/17304469636724867356a90.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>pandas \u8f93\u51fa\u683c\u5f0f\u7edf\u4e00\u5904\u7406<\/strong><\/p>\n<p>\u95ee\u9898\uff1a\u5982\u4f55\u7edf\u4e00\u4fee\u6539 pandas \u6570\u636e\u8868\u7684\u8f93\u51fa\u683c\u5f0f\uff0c\u4f7f\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\uff08\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u6d6e\u70b9\u6570\uff09\u5177\u6709\u7279\u5b9a\u7684\u663e\u793a\u683c\u5f0f\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 pandas \u7684 applymap() \u51fd\u6570\uff0c\u5bf9\u6570\u636e\u8868\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\u8fdb\u884c\u683c\u5f0f\u5316\u64cd\u4f5c\uff0c\u6839\u636e\u6570\u636e\u7c7b\u578b\u7684\u4e0d\u540c\uff0c\u8bbe\u7f6e\u4e0d\u540c\u7684\u683c\u5f0f\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>import pandas as pd\n\n# \u521b\u5efa\u6570\u636e\u8868\ndata = [[\"a\", 10000, 5000, 0.5], [\"b\", 20000, 30000, 1.5], [\"c\", 30000, 10000, 0.3333333]]\ndt = pd.dataframe(data, columns=[\"\u4ea7\u54c1\", \"\u4efb\u52a1\", \"\u9500\u552e\", \"\u5b8c\u6210\u7387\"])\n\n# \u7edf\u4e00\u683c\u5f0f\u5316\u6570\u636e\u8868\ndt = dt.applymap(lambda x: format(x, \".2%\") if pd.api.types.is_float(x) else '{:.1f}\u4e07'.format(x\/10000) if pd.api.types.is_integer(x) else x)\n\n# \u8f93\u51fa\u683c\u5f0f\u5316\u540e\u7684\u6570\u636e\u8868\nprint(dt)<\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre>   \u4ea7\u54c1  \u4efb\u52a1    \u9500\u552e \u5b8c\u6210\u7387\n0     A  1.0\u4e07   5.0\u4e07  50.00%\n1     B  2.0\u4e07  3.0\u4e07 150.00%\n2     C  3.0\u4e07  1.0\u4e07  33.3%<\/pre>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>lambda x: format(x, &#8220;.2%&#8221;): \u5982\u679c\u5355\u5143\u683c\u503c\u662f\u6d6e\u70b9\u6570\uff0c\u5219\u5c06\u5b83\u683c\u5f0f\u5316\u4e3a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u767e\u5206\u6bd4\u3002<\/li>\n<li>lambda x: &#8216;{:.1f}\u4e07&#8217;.format(x\/10000): \u5982\u679c\u5355\u5143\u683c\u503c\u662f\u6574\u6570\uff0c\u5219\u5c06\u5b83\u683c\u5f0f\u5316\u4e3a\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\u7684\u4e07\u4e3a\u5355\u4f4d\u3002<\/li>\n<li>lambda x: x: \u5982\u679c\u5355\u5143\u683c\u503c\u4e0d\u662f\u6d6e\u70b9\u6570\u6216\u6574\u6570\uff0c\u5219\u4fdd\u6301\u539f\u6837\u3002<\/li>\n<li>applymap() \u4f1a\u9010\u4e2a\u5904\u7406\u6570\u636e\u8868\u4e2d\u7684\u6bcf\u4e2a\u5355\u5143\u683c\uff0c\u5e76\u5e94\u7528\u683c\u5f0f\u5316\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u4f7f\u7528 Pandas \u7edf\u4e00\u4fee\u6539\u6570\u636e\u8868\u8f93\u51fa\u683c\u5f0f\uff0c\u4f7f\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u5177\u6709\u7279\u5b9a\u663e\u793a\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Pandas \u7edf\u4e00\u4fee\u6539&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204217","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204217"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204217\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}