{"id":1040950,"date":"2024-12-31T12:45:14","date_gmt":"2024-12-31T04:45:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1040950.html"},"modified":"2024-12-31T12:45:16","modified_gmt":"2024-12-31T04:45:16","slug":"python%e5%a6%82%e4%bd%95%e6%9b%bf%e6%8d%a2%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e7%a9%ba%e5%ad%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1040950.html","title":{"rendered":"python\u5982\u4f55\u66ff\u6362\u6570\u7ec4\u4e2d\u7684\u7a7a\u5b57\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/fc72349c-30a0-43d0-afae-7f156e87eb2c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u66ff\u6362\u6570\u7ec4\u4e2d\u7684\u7a7a\u5b57\u7b26\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528 Python \u66ff\u6362\u6570\u7ec4\u4e2d\u7684\u7a7a\u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\uff0c\u5982\u5217\u8868\u89e3\u6790\u3001\u5185\u7f6e\u51fd\u6570\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u5185\u7f6e\u7684 <code>map()<\/code> \u51fd\u6570\u3001NumPy \u5e93\u7b49\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u66ff\u6362\u7a7a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662f Python \u4e2d\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u5904\u7406\u5217\u8868\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5217\u8868\u89e3\u6790\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u904d\u5386\u6570\u7ec4\u5e76\u66ff\u6362\u7a7a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>array = [&quot;a&quot;, &quot;&quot;, &quot;b&quot;, &quot;&quot;, &quot;c&quot;]<\/p>\n<p>replacement = &quot;empty&quot;<\/p>\n<h2><strong>\u5217\u8868\u89e3\u6790\u66ff\u6362\u7a7a\u5b57\u7b26<\/strong><\/h2>\n<p>new_array = [replacement if x == &quot;&quot; else x for x in array]<\/p>\n<p>print(new_array)  # \u8f93\u51fa: [&#39;a&#39;, &#39;empty&#39;, &#39;b&#39;, &#39;empty&#39;, &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u9605\u8bfb\u548c\u7406\u89e3\u3002\u5728\u904d\u5386\u6570\u7ec4\u7684\u540c\u65f6\uff0c\u53ef\u4ee5\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u548c\u66ff\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>map()<\/code> \u51fd\u6570\u66ff\u6362\u7a7a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p><code>map()<\/code> \u51fd\u6570\u7528\u4e8e\u5c06\u6307\u5b9a\u7684\u51fd\u6570\u5e94\u7528\u4e8e\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u7ed3\u679c\u7684\u8fed\u4ee3\u5668\u3002\u7ed3\u5408 <code>lambda<\/code> \u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u66ff\u6362\u6570\u7ec4\u4e2d\u7684\u7a7a\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>array = [&quot;a&quot;, &quot;&quot;, &quot;b&quot;, &quot;&quot;, &quot;c&quot;]<\/p>\n<p>replacement = &quot;empty&quot;<\/p>\n<h2><strong>\u4f7f\u7528 map() \u548c lambda \u8868\u8fbe\u5f0f\u66ff\u6362\u7a7a\u5b57\u7b26<\/strong><\/h2>\n<p>new_array = list(map(lambda x: replacement if x == &quot;&quot; else x, array))<\/p>\n<p>print(new_array)  # \u8f93\u51fa: [&#39;a&#39;, &#39;empty&#39;, &#39;b&#39;, &#39;empty&#39;, &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>map()<\/code> \u51fd\u6570\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u907f\u514d\u663e\u5f0f\u7684\u5faa\u73af\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002\u4f46\u662f\u9700\u8981\u4f7f\u7528 <code>list()<\/code> \u51fd\u6570\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 NumPy \u5e93\u66ff\u6362\u7a7a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>NumPy \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002\u5982\u679c\u6570\u7ec4\u6570\u636e\u91cf\u8f83\u5927\uff0c\u4f7f\u7528 NumPy \u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import numpy as np<\/p>\n<p>array = np.array([&quot;a&quot;, &quot;&quot;, &quot;b&quot;, &quot;&quot;, &quot;c&quot;])<\/p>\n<p>replacement = &quot;empty&quot;<\/p>\n<h2><strong>\u4f7f\u7528 NumPy \u66ff\u6362\u7a7a\u5b57\u7b26<\/strong><\/h2>\n<p>new_array = np.where(array == &quot;&quot;, replacement, array)<\/p>\n<p>print(new_array)  # \u8f93\u51fa: [&#39;a&#39; &#39;empty&#39; &#39;b&#39; &#39;empty&#39; &#39;c&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy \u7684 <code>np.where()<\/code> \u51fd\u6570\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u8fd4\u56de\u5bf9\u5e94\u7684\u6570\u7ec4\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 pandas \u5e93\u66ff\u6362\u7a7a\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>pandas \u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u5982\u679c\u6570\u7ec4\u662f DataFrame \u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528 pandas \u8fdb\u884c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>import pandas as pd<\/p>\n<p>df = pd.DataFrame({&quot;col1&quot;: [&quot;a&quot;, &quot;&quot;, &quot;b&quot;, &quot;&quot;, &quot;c&quot;]})<\/p>\n<p>replacement = &quot;empty&quot;<\/p>\n<h2><strong>\u4f7f\u7528 pandas \u66ff\u6362\u7a7a\u5b57\u7b26<\/strong><\/h2>\n<p>df[&quot;col1&quot;] = df[&quot;col1&quot;].replace(&quot;&quot;, replacement)<\/p>\n<p>print(df)  # \u8f93\u51fa:<\/p>\n<h2><strong>     col1<\/strong><\/h2>\n<h2><strong>0       a<\/strong><\/h2>\n<h2><strong>1   empty<\/strong><\/h2>\n<h2><strong>2       b<\/strong><\/h2>\n<h2><strong>3   empty<\/strong><\/h2>\n<h2><strong>4       c<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pandas \u7684 <code>replace()<\/code> \u65b9\u6cd5\u975e\u5e38\u5f3a\u5927\uff0c\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u66ff\u6362\u64cd\u4f5c\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u6e05\u6d17\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6bd4\u8f83\u51e0\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5217\u8868\u89e3\u6790\uff1a<\/strong> \u7b80\u6d01\u6613\u8bfb\uff0c\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/li>\n<li><strong>map() \u51fd\u6570\uff1a<\/strong> \u907f\u514d\u663e\u5f0f\u5faa\u73af\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u4f46\u9700\u8981\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/li>\n<li><strong>NumPy \u5e93\uff1a<\/strong> \u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0c\u4f46\u9700\u8981\u989d\u5916\u5b89\u88c5\u5e93\u3002<\/li>\n<li><strong>pandas \u5e93\uff1a<\/strong> \u529f\u80fd\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u6e05\u6d17\u4efb\u52a1\uff0c\u4f46\u9700\u8981\u989d\u5916\u5b89\u88c5\u5e93\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u89c4\u6a21\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u9884\u5904\u7406\uff1a<\/strong> \u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4f8b\u5982\u66ff\u6362\u7f3a\u5931\u503c\u6216\u5f02\u5e38\u503c\u3002\u6b64\u65f6\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u66ff\u6362\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u6587\u672c\u5904\u7406\uff1a<\/strong> \u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u4efb\u52a1\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u66ff\u6362\u6587\u672c\u4e2d\u7684\u7279\u5b9a\u5b57\u7b26\u6216\u77ed\u8bed\uff0c\u4f8b\u5982\u53bb\u9664\u7a7a\u5b57\u7b26\u6216\u66ff\u6362\u7279\u5b9a\u6807\u8bb0\u3002<\/li>\n<li><strong>Web \u5f00\u53d1\uff1a<\/strong> \u5728 Web \u5f00\u53d1\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u6e05\u6d17\uff0c\u4f8b\u5982\u66ff\u6362\u7a7a\u5b57\u7b26\u6216\u5220\u9664\u591a\u4f59\u7684\u7a7a\u683c\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u66ff\u6362\u6570\u7ec4\u4e2d\u7684\u7a7a\u5b57\u7b26\u662f\u6570\u636e\u5904\u7406\u4e2d\u7684\u5e38\u89c1\u4efb\u52a1\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u3002\u901a\u8fc7\u5217\u8868\u89e3\u6790\u3001<code>map()<\/code> \u51fd\u6570\u3001NumPy \u548c pandas \u5e93\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u8fdb\u884c\u5904\u7406\u3002\u5728\u9009\u62e9\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u8003\u8651\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u3001\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u7b49\u56e0\u7d20\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u6d4b\u6570\u7ec4\u4e2d\u7684\u7a7a\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4e0e<code>if<\/code>\u6761\u4ef6\u8bed\u53e5\u6765\u68c0\u6d4b\u6570\u7ec4\u4e2d\u7684\u7a7a\u5b57\u7b26\u3002\u901a\u8fc7\u904d\u5386\u6570\u7ec4\u5e76\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4e3a\u7a7a\u5b57\u7b26\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bc6\u522b\u51fa\u6240\u6709\u7a7a\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u68c0\u67e5\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u82e5\u957f\u5ea6\u4e3a0\uff0c\u5219\u8be5\u5b57\u7b26\u4e32\u4e3a\u7a7a\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u66ff\u6362\u7a7a\u5b57\u7b26\u4e3a\u5176\u4ed6\u503c\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5c06\u7a7a\u5b57\u7b26\u66ff\u6362\u4e3a\u7279\u5b9a\u7684\u503c\uff0c\u53ef\u4ee5\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u7a7a\u5b57\u7b26\u66ff\u6362\u4e3a<code>None<\/code>\u3001<code>&quot;N\/A&quot;<\/code>\u6216\u4efb\u4f55\u5176\u4ed6\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u6784\u9020\u65b0\u7684\u6570\u7ec4\uff0c\u5c06\u539f\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u7a7a\u5b57\u7b26\u66ff\u6362\u4e3a\u76ee\u6807\u503c\uff0c\u4ece\u800c\u5b9e\u73b0\u66ff\u6362\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u7a7a\u5b57\u7b26\uff1f<\/strong><br \/>\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u6216\u5d4c\u5957\u5faa\u73af\u6765\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e00\u5c42\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u5143\u7d20\uff0c\u68c0\u67e5\u5176\u662f\u5426\u4e3a\u7a7a\u5b57\u7b26\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u66ff\u6362\u3002\u8fd9\u6837\u7684\u65b9\u5f0f\u80fd\u591f\u786e\u4fdd\u6240\u6709\u5c42\u7ea7\u7684\u7a7a\u5b57\u7b26\u90fd\u80fd\u88ab\u6b63\u786e\u8bc6\u522b\u548c\u66ff\u6362\u3002\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u7b80\u5316\u591a\u7ef4\u6570\u7ec4\u7684\u64cd\u4f5c\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u8f83\u5927\u6570\u636e\u65f6\u66f4\u4e3a\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528 Python \u66ff\u6362\u6570\u7ec4\u4e2d\u7684\u7a7a\u5b57\u7b26\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\uff0c\u5982\u5217\u8868\u89e3\u6790\u3001\u5185\u7f6e\u51fd\u6570\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002 \u5176\u4e2d\uff0c\u6700\u5e38\u89c1 [&hellip;]","protected":false},"author":3,"featured_media":1040957,"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\/1040950"}],"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=1040950"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1040950\/revisions"}],"predecessor-version":[{"id":1040963,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1040950\/revisions\/1040963"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1040957"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1040950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1040950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1040950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}