{"id":204362,"date":"2025-05-29T08:34:21","date_gmt":"2025-05-29T00:34:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204362\/"},"modified":"2025-05-29T08:34:21","modified_gmt":"2025-05-29T00:34:21","slug":"pandas-%e5%a6%82%e4%bd%95%e6%a0%b9%e6%8d%ae%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e8%ae%be%e7%bd%ae%e6%a0%bc%e5%bc%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204362\/","title":{"rendered":"Pandas \u5982\u4f55\u6839\u636e\u6570\u636e\u7c7b\u578b\u8bbe\u7f6e\u683c\u5f0f\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Pandas \u5982\u4f55\u6839\u636e\u6570\u636e\u7c7b\u578b\u8bbe\u7f6e\u683c\u5f0f\uff1f<\/h1>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241105\/17307974296729df752cfac.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>pandas \u5982\u4f55\u6309\u6570\u636e\u7c7b\u578b\u8bbe\u7f6e\u683c\u5f0f<\/strong><\/p>\n<p>\u5728 pandas \u4e2d\u8f93\u51fa\u8868\u683c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e0d\u540c\u7c7b\u578b\u6570\u636e\u9700\u8981\u6309\u7279\u5b9a\u683c\u5f0f\u663e\u793a\u7684\u60c5\u51b5\u3002\u4ee5\u4e0b\u65b9\u6cd5\u4ecb\u7ecd\u5982\u4f55\u6309\u7167\u6570\u636e\u7c7b\u578b\u4e00\u6b21\u6027\u5bf9\u8868\u683c\u8fdb\u884c\u683c\u5f0f\u5316\u5904\u7406\u3002<\/p>\n<p><strong>\u6309\u5b57\u7b26\u4e32\u3001\u6574\u6570\u548c\u6d6e\u70b9\u6570\u7c7b\u578b\u8bbe\u7f6e\u683c\u5f0f<\/strong><\/p>\n<p>\u7ed9\u5b9a\u6570\u636e\u5982\u4e0b\uff1a<\/p>\n<pre>import pandas as pd\ndata=[[\u201ca\u201d,10000,5000,0.5],[\u201cb\u201d,20000,30000,1.5],[\u201cc\u201d,30000,10000,0.3333333]]\ndt=pd.dataframe(data,columns=[\u201c\u4ea7\u54c1\u201d,\u201d\u4efb\u52a1\u201d,\u201d\u9500\u552e\u201d,\u201d\u5b8c\u6210\u7387\u201d])<\/pre>\n<p>\u8981\u6309\u7167\u6570\u636e\u7c7b\u578b\u4e00\u6b21\u6027\u8bbe\u7f6e\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 applymap() \u65b9\u6cd5\uff1a<\/p>\n<pre>dt = 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)<\/pre>\n<p>\u6b64\u65b9\u6cd5\u4f7f\u7528 lambda \u51fd\u6570\u6839\u636e\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u8bbe\u7f6e\u683c\u5f0f\uff1a<\/p>\n<ul>\n<li>\u6d6e\u70b9\u6570\u683c\u5f0f\u5316\u6210\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u767e\u5206\u6570\u3002<\/li>\n<li>\u6574\u6570\u683c\u5f0f\u5316\u6210\u4e07\u4f4d\u6570\u5e76\u56db\u820d\u4e94\u5165\u4fdd\u7559\u5c0f\u6570\u70b9\u540e\u4e00\u4f4d\u3002<\/li>\n<li>\u5b57\u7b26\u4e32\u4fdd\u6301\u4e0d\u53d8\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8868\u683c\u5c06\u6309\u6570\u636e\u7c7b\u578b\u683c\u5f0f\u5316\u663e\u793a\uff1a<\/p>\n<pre>   \u4ea7\u54c1   \u4efb\u52a1    \u9500\u552e  \u5b8c\u6210\u7387\n0    A  10000.0  5000.0   50.00%\n1    B  20000.0 30000.0  150.00%\n2    C  30000.0 10000.0   33.3%<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPandas \u5982\u4f55\u6839\u636e\u6570\u636e\u7c7b\u578b\u8bbe\u7f6e\u683c\u5f0f\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pandas \u5982\u4f55\u6839\u636e\u6570\u636e\u7c7b\u578b\u8bbe&#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-204362","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204362","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=204362"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204362\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}