{"id":1132446,"date":"2025-01-08T20:57:19","date_gmt":"2025-01-08T12:57:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132446.html"},"modified":"2025-01-08T20:57:21","modified_gmt":"2025-01-08T12:57:21","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84nan%e8%bd%ac%e5%8c%96%e4%b8%ba%e7%a9%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132446.html","title":{"rendered":"python\u5982\u4f55\u628a\u6570\u7ec4\u4e2d\u7684nan\u8f6c\u5316\u4e3a\u7a7a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102010\/20407d35-59a6-4e18-aaf4-c115acd726f4.webp\" alt=\"python\u5982\u4f55\u628a\u6570\u7ec4\u4e2d\u7684nan\u8f6c\u5316\u4e3a\u7a7a\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u7ec4\u4e2d\u7684NaN\u503c\u8f6c\u5316\u4e3a\u7a7a\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u503c\u3002\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u3001Pandas\u4ee5\u53ca\u6807\u51c6\u7684Python\u5217\u8868\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8be6\u7ec6\u7684\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528NumPy\u3001\u4f7f\u7528Pandas\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u6570\u636e\u7684\u5f3a\u5927\u5e93\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528NumPy\u7684<code>isnan<\/code>\u51fd\u6570\u6765\u68c0\u6d4b\u6570\u7ec4\u4e2d\u7684NaN\u503c\uff0c\u5e76\u7528<code>where<\/code>\u51fd\u6570\u5c06\u5b83\u4eec\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u521d\u59cb\u5316\u6570\u7ec4\u5e76\u5bfc\u5165NumPy<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u5982\u679c\u6ca1\u6709\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5bfc\u5165NumPy\u5e76\u521b\u5efa\u4e00\u4e2a\u5305\u542bNaN\u503c\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542bNaN\u503c\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, np.nan, 4, np.nan, 6])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528NumPy\u7684<code>where<\/code>\u51fd\u6570\u66ff\u6362NaN\u503c<\/h4>\n<\/p>\n<p><p>\u5229\u7528<code>np.isnan<\/code>\u68c0\u6d4b\u6570\u7ec4\u4e2d\u7684NaN\u503c\uff0c\u5e76\u7528<code>np.where<\/code>\u5c06\u5176\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06NaN\u503c\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32<\/p>\n<p>arr = np.where(np.isnan(arr), &#39;&#39;, arr)<\/p>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002Pandas\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684\u65b9\u6cd5\u6765\u5904\u7406NaN\u503c\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u521d\u59cb\u5316\u6570\u636e\u5e76\u5bfc\u5165Pandas<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Pandas\u5e93\u3002\u5982\u679c\u6ca1\u6709\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5bfc\u5165Pandas\u5e76\u521b\u5efa\u4e00\u4e2a\u5305\u542bNaN\u503c\u7684DataFrame\u6216Series\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542bNaN\u503c\u7684Series<\/strong><\/h2>\n<p>series = pd.Series([1, 2, np.nan, 4, np.nan, 6])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528Pandas\u7684<code>fillna<\/code>\u65b9\u6cd5\u66ff\u6362NaN\u503c<\/h4>\n<\/p>\n<p><p>\u5229\u7528Pandas\u7684<code>fillna<\/code>\u65b9\u6cd5\u5c06NaN\u503c\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06NaN\u503c\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32<\/p>\n<p>series = series.fillna(&#39;&#39;)<\/p>\n<p>print(series)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4e0d\u60f3\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5217\u8868\u89e3\u6790\u6765\u5904\u7406NaN\u503c\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u521d\u59cb\u5316\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u5305\u542bNaN\u503c\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542bNaN\u503c\u7684\u5217\u8868<\/strong><\/h2>\n<p>arr = [1, 2, float(&#39;nan&#39;), 4, float(&#39;nan&#39;), 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528\u5217\u8868\u89e3\u6790\u66ff\u6362NaN\u503c<\/h4>\n<\/p>\n<p><p>\u5229\u7528\u5217\u8868\u89e3\u6790\u548c<code>math.isnan<\/code>\u51fd\u6570\u68c0\u6d4bNaN\u503c\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06NaN\u503c\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32<\/p>\n<p>arr = [x if not math.isnan(x) else &#39;&#39; for x in arr]<\/p>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u4f55\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u6570\u636e\u7c7b\u578b\u548c\u9700\u6c42\u3002\u5982\u679c\u4f60\u5904\u7406\u7684\u662f\u591a\u7ef4\u6570\u7ec4\u6216\u77e9\u9635\u6570\u636e\uff0cNumPy\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5982\u679c\u4f60\u5904\u7406\u7684\u662f\u8868\u683c\u6570\u636e\uff0cPandas\u5219\u66f4\u52a0\u9002\u5408\u3002\u800c\u5217\u8868\u89e3\u6790\u5219\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5217\u8868\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u6570\u636e\u89c4\u6a21\u548c\u6027\u80fd\u8003\u91cf<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0cNumPy\u548cPandas\u7684\u6027\u80fd\u901a\u5e38\u4f18\u4e8e\u7eafPython\u7684\u5217\u8868\u89e3\u6790\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u5e95\u5c42\u8fdb\u884c\u4e86\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u6570\u636e\u7c7b\u578b\u7684\u517c\u5bb9\u6027<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u7684\u6570\u636e\u5305\u542b\u591a\u79cd\u7c7b\u578b\uff08\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u7b49\uff09\uff0cPandas\u7684<code>fillna<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u517c\u5bb9\u6027\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h4>4.3 \u6613\u7528\u6027\u548c\u4ee3\u7801\u7b80\u6d01\u5ea6<\/h4>\n<\/p>\n<p><p>Pandas\u548cNumPy\u63d0\u4f9b\u7684\u9ad8\u5c42\u6b21API\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\uff0c\u76f8\u6bd4\u4e8e\u7eafPython\u7684\u5217\u8868\u89e3\u6790\u66f4\u4e3a\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u5904\u7406NaN\u503c\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5c06NaN\u503c\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u9700\u8981\u5176\u4ed6\u5904\u7406\u65b9\u6cd5\uff0c\u5982\u5220\u9664\u5305\u542bNaN\u503c\u7684\u884c\u6216\u5217\u3001\u586b\u5145\u7279\u5b9a\u7684\u9ed8\u8ba4\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u4f7f\u7528NumPy\u5220\u9664NaN\u503c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u5305\u542bNaN\u503c\u7684\u5143\u7d20<\/p>\n<p>arr = arr[~np.isnan(arr)]<\/p>\n<p>print(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u4f7f\u7528Pandas\u5220\u9664NaN\u503c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u5305\u542bNaN\u503c\u7684\u884c<\/p>\n<p>series = series.dropna()<\/p>\n<p>print(series)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.3 \u4f7f\u7528Pandas\u586b\u5145\u7279\u5b9a\u503c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06NaN\u503c\u66ff\u6362\u4e3a0<\/p>\n<p>series = series.fillna(0)<\/p>\n<p>print(series)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5904\u7406NaN\u503c\u662f\u6570\u636e\u6e05\u6d17\u7684\u91cd\u8981\u6b65\u9aa4\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7c7b\u578b\u3001\u89c4\u6a21\u548c\u5177\u4f53\u9700\u6c42\u3002<\/strong>NumPy\u548cPandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u800c\u7b80\u6d01\u7684API\u6765\u5904\u7406NaN\u503c\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u800c\u5217\u8868\u89e3\u6790\u5219\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5217\u8868\u64cd\u4f5c\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u7406\u89e3\u548c\u638c\u63e1\u5728Python\u4e2d\u5c06NaN\u503c\u8f6c\u5316\u4e3a\u7a7a\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u6570\u7ec4\u4e2d\u7684NaN\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u8bc6\u522b\u6570\u7ec4\u4e2d\u7684NaN\u503c\u3002NumPy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>np.isnan()<\/code>\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u6570\u7ec4\uff0c\u6307\u793a\u54ea\u4e9b\u5143\u7d20\u662fNaN\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\narr = np.array([1, 2, np.nan, 4])\nnan_mask = np.isnan(arr)\nprint(nan_mask)  # \u8f93\u51fa: [False False  True False]\n<\/code><\/pre>\n<p><strong>\u5c06\u6570\u7ec4\u4e2d\u7684NaN\u503c\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>np.where()<\/code>\u51fd\u6570\u6765\u5c06NaN\u503c\u66ff\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\narr = np.array([1, 2, np.nan, 4])\narr_with_empty_strings = np.where(np.isnan(arr), &#39;&#39;, arr)\nprint(arr_with_empty_strings)  # \u8f93\u51fa: [&#39;1&#39; &#39;2&#39; &#39;&#39; &#39;4&#39;]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u539f\u6570\u7ec4\u4e2d\u7684NaN\u503c\u5c31\u88ab\u7a7a\u5b57\u7b26\u4e32\u66ff\u6362\u4e86\u3002<\/p>\n<p><strong>\u5728Pandas\u4e2d\u5982\u4f55\u5904\u7406\u6570\u7ec4\u4e2d\u7684NaN\u503c\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u4f7f\u7528\u7684\u662fPandas\uff0c\u53ef\u4ee5\u5229\u7528<code>fillna()<\/code>\u65b9\u6cd5\u8f7b\u677e\u5c06NaN\u503c\u8f6c\u6362\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = pd.Series([1, 2, np.nan, 4])\ncleaned_data = data.fillna(&#39;&#39;)\nprint(cleaned_data)  # \u8f93\u51fa: 0    1\n                     #         1    2\n                     #         2      \n                     #         3    4\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u65b9\u4fbf\uff0c\u8fd8\u80fd\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u7ec4\u4e2d\u7684NaN\u503c\u8f6c\u5316\u4e3a\u7a7a\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u503c\u3002\u5e38\u89c1\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u3001Pan [&hellip;]","protected":false},"author":3,"featured_media":1132448,"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\/1132446"}],"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=1132446"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132446\/revisions"}],"predecessor-version":[{"id":1132450,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132446\/revisions\/1132450"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132448"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}