{"id":204091,"date":"2025-05-29T08:57:20","date_gmt":"2025-05-29T00:57:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204091\/"},"modified":"2025-05-29T08:57:20","modified_gmt":"2025-05-29T00:57:20","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-applymap-%e6%96%b9%e6%b3%95%e6%a0%bc%e5%bc%8f%e5%8c%96-dataframe-%e4%b8%ad%e4%b8%8d%e5%90%8c%e7%b1%bb%e5%9e%8b%e7%9a%84%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204091\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 applymap \u65b9\u6cd5\u683c\u5f0f\u5316 DataFrame \u4e2d\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 applymap \u65b9\u6cd5\u683c\u5f0f\u5316 DataFrame \u4e2d\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff1f<\/h1>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u5982\u4f55\u4f7f\u7528 applymap \u65b9\u6cd5\u683c\u5f0f\u5316 DataFrame \u4e2d\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff1f\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730023430671e100670c6f.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u901a\u8fc7applymap\u65b9\u6cd5\u683c\u5f0f\u5316dataframe<\/strong><\/p>\n<p>\u5728\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u7ecf\u5e38\u4f1a\u51fa\u73b0\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u6df7\u6742\u5728\u4e00\u8d77\u7684\u60c5\u51b5\uff0c\u8fd9\u4e5f\u7ed9\u6570\u636e\u7684\u683c\u5f0f\u5316\u5e26\u6765\u4e86\u4e00\u5b9a\u7684\u56f0\u96be\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cpandas\u63d0\u4f9b\u4e86applymap\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u9010\u4e2a\u5143\u7d20\u5bf9dataframe\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<p>\u4ee5\u7ed9\u5b9a\u7684dataframe\u4e3a\u4f8b\uff0c\u5176\u4e2d\u5305\u542b\u5b57\u7b26\u4e32\u3001\u6574\u6570\u548c\u6d6e\u70b9\u6570\uff1a<\/p>\n<pre>import pandas as pd\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'])<\/pre>\n<p>\u8981\u5c06\u7ed3\u679c\u683c\u5f0f\u5316\u4e3a\u5982\u4e0b\u5f62\u5f0f\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u4ea7\u54c1<\/th>\n<th>\u4efb\u52a1<\/th>\n<th>\u9500\u552e<\/th>\n<th>\u5b8c\u6210\u7387<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>a<\/td>\n<td>&#8211;<\/td>\n<td>1.0\u4e07<\/td>\n<td>50.00%<\/td>\n<\/tr>\n<tr>\n<td>b<\/td>\n<td>&#8211;<\/td>\n<td>3.0\u4e07<\/td>\n<td>150.00%<\/td>\n<\/tr>\n<tr>\n<td>c<\/td>\n<td>&#8211;<\/td>\n<td>1.0\u4e07<\/td>\n<td>33.33%<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53ef\u4ee5\u4f7f\u7528applymap\u65b9\u6cd5\u5e94\u7528\u03bb\u51fd\u6570\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>\u901a\u8fc7lambda\u51fd\u6570\u5224\u65ad\u6bcf\u4e2a\u5143\u7d20\u7684\u7c7b\u578b\uff0c\u5e76\u5e94\u7528\u76f8\u5e94\u7684\u683c\u5f0f\u3002\u6700\u7ec8\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u4ea7\u54c1<\/th>\n<th>\u4efb\u52a1<\/th>\n<th>\u9500\u552e<\/th>\n<th>\u5b8c\u6210\u7387<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>a<\/td>\n<td>&#8211;<\/td>\n<td>1.0\u4e07<\/td>\n<td>50.00%<\/td>\n<\/tr>\n<tr>\n<td>b<\/td>\n<td>&#8211;<\/td>\n<td>3.0\u4e07<\/td>\n<td>150.00%<\/td>\n<\/tr>\n<tr>\n<td>c<\/td>\n<td>&#8211;<\/td>\n<td>1.0\u4e07<\/td>\n<td>33.33%<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5982\u4f55\u4f7f\u7528 applymap \u65b9\u6cd5\u683c\u5f0f\u5316 DataFrame \u4e2d\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 applymap \u65b9\u6cd5&#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-204091","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204091","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=204091"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204091\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}