{"id":1097577,"date":"2025-01-08T15:13:11","date_gmt":"2025-01-08T07:13:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097577.html"},"modified":"2025-01-08T15:13:14","modified_gmt":"2025-01-08T07:13:14","slug":"%e5%b0%8f%e4%ba%8e0%e7%9a%84%e6%95%b0%e5%80%bc%e7%94%a8python%e5%a6%82%e4%bd%95%e5%a1%ab%e8%a1%a5-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097577.html","title":{"rendered":"\u5c0f\u4e8e0\u7684\u6570\u503c\u7528Python\u5982\u4f55\u586b\u8865"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212254\/a9d4ea7e-d641-4b04-ab35-13b01904b4b3.webp\" alt=\"\u5c0f\u4e8e0\u7684\u6570\u503c\u7528Python\u5982\u4f55\u586b\u8865\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u586b\u8865\u5c0f\u4e8e0\u7684\u6570\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c<strong>\u5176\u4e2d\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u3001NumPy\u5e93\u3001Pandas\u5e93\u7b49<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u586b\u8865\u5c0f\u4e8e0\u7684\u6570\u503c<\/h3>\n<\/p>\n<p><p>\u6761\u4ef6\u8bed\u53e5\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u63a7\u5236\u7ed3\u6784\u4e4b\u4e00\uff0c\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u6bcf\u4e2a\u6570\u503c\u5e76\u6839\u636e\u6761\u4ef6\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [-5, 3, -2, 8, -1, 7]<\/p>\n<h2><strong>\u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p>data_filled = [0 if x &lt; 0 else x for x in data]<\/p>\n<p>print(data_filled)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u904d\u5386\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u5c0f\u4e8e0\uff0c\u5219\u5c06\u5176\u66ff\u6362\u4e3a0\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u5c0f\u578b\u6570\u636e\u96c6\u6216\u7b80\u5355\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u586b\u8865\u5c0f\u4e8e0\u7684\u6570\u503c<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u7684\u5f3a\u5927\u5de5\u5177\u3002\u4f7f\u7528NumPy\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([-5, 3, -2, 8, -1, 7])<\/p>\n<h2><strong>\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528NumPy\u7684where\u51fd\u6570<\/strong><\/h2>\n<p>data_filled = np.where(data &lt; 0, 0, data)<\/p>\n<p>print(data_filled)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>np.where<\/code>\u51fd\u6570\u7528\u4e8e\u68c0\u67e5\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u5c0f\u4e8e0\uff0c\u5219\u5c06\u5176\u66ff\u6362\u4e3a0\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5728\u5904\u7406\u5927\u578b\u6570\u7ec4\u65f6\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5e93\u586b\u8865\u5c0f\u4e8e0\u7684\u6570\u503c<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u5206\u6790\u4efb\u52a1\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.Series([-5, 3, -2, 8, -1, 7])<\/p>\n<h2><strong>\u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528Pandas\u7684apply\u51fd\u6570<\/strong><\/h2>\n<p>data_filled = data.apply(lambda x: 0 if x &lt; 0 else x)<\/p>\n<p>print(data_filled)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>apply<\/code>\u51fd\u6570\u904d\u5386Series\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u5c0f\u4e8e0\uff0c\u5219\u5c06\u5176\u66ff\u6362\u4e3a0\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8eDataFrame\u548cSeries\u7684\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u9884\u5904\u7406<\/strong>\uff1a\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6570\u636e\u9884\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\u3002\u586b\u8865\u5c0f\u4e8e0\u7684\u6570\u503c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u7f3a\u5931\u503c\u6216\u5f02\u5e38\u503c\uff0c\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8d22\u52a1\u6570\u636e\u5206\u6790<\/strong>\uff1a\u5728\u8d22\u52a1\u6570\u636e\u5206\u6790\u4e2d\uff0c\u67d0\u4e9b\u6307\u6807\u4e0d\u5e94\u4e3a\u8d1f\u503c\uff0c\u5982\u9500\u552e\u989d\u3001\u5229\u6da6\u7b49\u3002\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5408\u7406\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u50cf\u5904\u7406<\/strong>\uff1a\u5728\u56fe\u50cf\u5904\u7406\u4efb\u52a1\u4e2d\uff0c\u50cf\u7d20\u503c\u901a\u5e38\u57280\u5230255\u4e4b\u95f4\u3002\u8d1f\u503c\u50cf\u7d20\u53ef\u80fd\u8868\u793a\u6570\u636e\u635f\u574f\u6216\u9519\u8bef\uff0c\u9700\u8981\u8fdb\u884c\u586b\u8865\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u586b\u8865\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u586b\u8865\u5c0f\u4e8e0\u7684\u6570\u503c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u63d2\u503c\u6cd5<\/strong>\uff1a\u901a\u8fc7\u63d2\u503c\u65b9\u6cd5\uff0c\u6839\u636e\u76f8\u90bb\u7684\u6b63\u503c\u6765\u586b\u8865\u8d1f\u503c\u3002\u8fd9\u5728\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u5904\u7406\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5747\u503c\u586b\u8865<\/strong>\uff1a\u5c06\u8d1f\u503c\u66ff\u6362\u4e3a\u6570\u636e\u96c6\u7684\u5747\u503c\u3002\u8fd9\u5728\u5904\u7406\u7f3a\u5931\u6570\u636e\u65f6\u5e38\u7528\uff0c\u4f46\u9700\u8981\u8c28\u614e\u4f7f\u7528\uff0c\u4ee5\u514d\u5f15\u5165\u504f\u5dee\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u586b\u8865\u65b9\u6cd5<\/strong>\uff1a\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u81ea\u5b9a\u4e49\u586b\u8865\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u4e1a\u52a1\u89c4\u5219\uff0c\u5c06\u8d1f\u503c\u66ff\u6362\u4e3a\u67d0\u4e2a\u7279\u5b9a\u7684\u9608\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u586b\u8865\u5c0f\u4e8e0\u7684\u6570\u503c\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u4efb\u52a1\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u3001NumPy\u5e93\u3001Pandas\u5e93\u7b49<\/strong>\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5408\u7406\u6027\u548c\u4e00\u81f4\u6027\uff0c\u63d0\u9ad8\u5206\u6790\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728\u5904\u7406\u6570\u636e\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u5c0f\u4e8e0\u7684\u6570\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u8bc6\u522b\u5c0f\u4e8e0\u7684\u6570\u503c\u3002\u901a\u8fc7NumPy\u5e93\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5904\u7406\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy.where()<\/code>\u51fd\u6570\u53ef\u4ee5\u627e\u5230\u5c0f\u4e8e0\u7684\u5143\u7d20\u7d22\u5f15\uff0c\u5177\u4f53\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ndata = np.array([-1, 2, -3, 4, 5])\nnegative_indices = np.where(data &lt; 0)\nprint(negative_indices)  # \u8f93\u51fa\u5c0f\u4e8e0\u7684\u5143\u7d20\u7d22\u5f15\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5feb\u901f\u5b9a\u4f4d\u5230\u6570\u7ec4\u4e2d\u6240\u6709\u8d1f\u6570\u7684\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u5982\u4f55\u7528Python\u586b\u8865\u5c0f\u4e8e0\u7684\u6570\u503c\u4e3a\u7279\u5b9a\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5c0f\u4e8e0\u7684\u6570\u503c\u66ff\u6362\u4e3a\u5176\u4ed6\u7279\u5b9a\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>numpy.where()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5c0f\u4e8e0\u7684\u6570\u503c\u66ff\u6362\u4e3a0\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ndata = np.array([-1, 2, -3, 4, 5])\nfilled_data = np.where(data &lt; 0, 0, data)\nprint(filled_data)  # \u8f93\u51fa\uff1a[0 2 0 4 5]\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u6570\u636e\u5904\u7406\u53d8\u5f97\u66f4\u52a0\u7b80\u4fbf\u548c\u9ad8\u6548\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Pandas\u5904\u7406\u5c0f\u4e8e0\u7684\u6570\u503c\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff0cPandas\u63d0\u4f9b\u4e86\u975e\u5e38\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6570\u636e\u6846\u4e2d\u7684\u5c0f\u4e8e0\u7684\u6570\u503c\u3002\u901a\u8fc7<code>DataFrame.apply()<\/code>\u6216<code>DataFrame.where()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u793a\u4f8b\u5c06\u5c0f\u4e8e0\u7684\u503c\u66ff\u6362\u4e3aNaN\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndf = pd.DataFrame({&#39;values&#39;: [-1, 2, -3, 4, 5]})\ndf[&#39;values&#39;] = df[&#39;values&#39;].where(df[&#39;values&#39;] &gt;= 0, other=np.nan)\nprint(df)  # \u8f93\u51fa\uff1avalues\u5217\u4e2d\u7684\u8d1f\u6570\u5c06\u88ab\u66ff\u6362\u4e3aNaN\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u6570\u636e\u6e05\u6d17\u548c\u5206\u6790\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u586b\u8865\u5c0f\u4e8e0\u7684\u6570\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5176\u4e2d\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u3001NumPy\u5e93\u3001Pand [&hellip;]","protected":false},"author":3,"featured_media":1097584,"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\/1097577"}],"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=1097577"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097577\/revisions"}],"predecessor-version":[{"id":1097587,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097577\/revisions\/1097587"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097584"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}