{"id":1075605,"date":"2025-01-08T11:45:45","date_gmt":"2025-01-08T03:45:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1075605.html"},"modified":"2025-01-08T11:45:47","modified_gmt":"2025-01-08T03:45:47","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e4%b8%80%e4%b8%aanan%e7%9f%a9%e9%98%b5-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1075605.html","title":{"rendered":"python\u5982\u4f55\u751f\u6210\u4e00\u4e2anan\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180618\/276f1e49-0cde-41d4-9116-d02a66097aac.webp\" alt=\"python\u5982\u4f55\u751f\u6210\u4e00\u4e2anan\u77e9\u9635\" \/><\/p>\n<p><p> <strong>Python\u751f\u6210\u4e00\u4e2anan\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u5229\u7528Pandas\u5e93\u3001\u4ee5\u53ca\u624b\u52a8\u521b\u5efa\u3002\u6700\u5e38\u7528\u548c\u4fbf\u6377\u7684\u65b9\u5f0f\u662f\u4f7f\u7528NumPy\u5e93\uff0c\u4e3b\u8981\u65b9\u6cd5\u6709\uff1anp.full\u3001np.empty\u3001np.nan\u548cnp.full_like\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u751f\u6210nan\u77e9\u9635\uff0c\u5e76\u8be6\u7ec6\u89e3\u91ca\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff08np.full\uff09\u3002\u6b64\u5916\uff0c\u8fd8\u4f1a\u4ecb\u7ecdPandas\u5e93\u548c\u624b\u52a8\u521b\u5efa\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528NumPy\u5e93\u751f\u6210nan\u77e9\u9635<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528np.full<\/li>\n<li>\u4f7f\u7528np.empty<\/li>\n<li>\u4f7f\u7528np.nan<\/li>\n<li>\u4f7f\u7528np.full_like<\/li>\n<\/ol>\n<p><h3>1. \u4f7f\u7528np.full<\/h3>\n<\/p>\n<p><p><code>np.full<\/code>\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u5f62\u72b6\u7684\u77e9\u9635\uff0c\u5e76\u7528\u6307\u5b9a\u7684\u503c\u586b\u5145\u3002\u8981\u751f\u6210\u4e00\u4e2anan\u77e9\u9635\uff0c\u53ef\u4ee5\u5c06\u586b\u5145\u503c\u8bbe\u7f6e\u4e3a<code>np.nan<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a3x3\u7684nan\u77e9\u9635<\/strong><\/h2>\n<p>nan_matrix = np.full((3, 3), np.nan)<\/p>\n<p>print(nan_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0<\/strong>\uff1a<code>np.full((3, 3), np.nan)<\/code>\u4e2d\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5143\u7ec4\uff0c\u6307\u5b9a\u4e86\u77e9\u9635\u7684\u5f62\u72b6\uff083\u884c3\u5217\uff09\uff1b\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u586b\u5145\u503c\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u4e3a<code>np.nan<\/code>\uff0c\u8868\u793a\u77e9\u9635\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f<code>nan<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u521b\u5efa\u7684\u77e9\u9635\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f<code>nan<\/code>\uff0c\u4e14\u5f62\u72b6\u53ef\u4ee5\u6839\u636e\u9700\u8981\u7075\u6d3b\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528np.empty<\/h3>\n<\/p>\n<p><p><code>np.empty<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u77e9\u9635\uff0c\u7136\u540e\u5c06\u6240\u6709\u5143\u7d20\u8bbe\u7f6e\u4e3a<code>np.nan<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a3x3\u7684\u672a\u521d\u59cb\u5316\u77e9\u9635<\/strong><\/h2>\n<p>nan_matrix = np.empty((3, 3))<\/p>\n<p>nan_matrix[:] = np.nan<\/p>\n<p>print(nan_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5148\u4f7f\u7528<code>np.empty<\/code>\u521b\u5efa\u4e00\u4e2a\u672a\u521d\u59cb\u5316\u7684\u77e9\u9635\uff0c\u7136\u540e\u901a\u8fc7<code>nan_matrix[:] = np.nan<\/code>\u5c06\u6240\u6709\u5143\u7d20\u8bbe\u7f6e\u4e3a<code>nan<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u7075\u6d3b\uff0c\u4f46\u9700\u8981\u4e24\u6b65\u5b8c\u6210\u3002<\/p>\n<\/p>\n<p><h3>3. \u4f7f\u7528np.nan<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u4f7f\u7528<code>np.nan<\/code>\u751f\u6210\u4e00\u4e2anan\u77e9\u9635\u3002\u53ef\u4ee5\u7ed3\u5408<code>np.tile<\/code>\u51fd\u6570\uff0c\u5c06\u4e00\u4e2a<code>nan<\/code>\u6269\u5c55\u5230\u6574\u4e2a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a3x3\u7684nan\u77e9\u9635<\/strong><\/h2>\n<p>nan_matrix = np.tile(np.nan, (3, 3))<\/p>\n<p>print(nan_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>np.tile(np.nan, (3, 3))<\/code>\u5c06\u4e00\u4e2a<code>nan<\/code>\u503c\u6269\u5c55\u52303&#215;3\u7684\u77e9\u9635\u4e2d\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8f83\u4e3a\u7b80\u6d01\uff0c\u4f46\u53ef\u80fd\u4e0d\u5982\u524d\u4e24\u79cd\u65b9\u6cd5\u5e38\u7528\u3002<\/p>\n<\/p>\n<p><h3>4. \u4f7f\u7528np.full_like<\/h3>\n<\/p>\n<p><p><code>np.full_like<\/code>\u51fd\u6570\u53ef\u4ee5\u6839\u636e\u73b0\u6709\u6570\u7ec4\u7684\u5f62\u72b6\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u5e76\u7528\u6307\u5b9a\u7684\u503c\u586b\u5145\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u53c2\u8003\u77e9\u9635<\/strong><\/h2>\n<p>reference_matrix = np.zeros((3, 3))<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u4e0e\u53c2\u8003\u77e9\u9635\u5f62\u72b6\u76f8\u540c\u7684nan\u77e9\u9635<\/strong><\/h2>\n<p>nan_matrix = np.full_like(reference_matrix, np.nan)<\/p>\n<p>print(nan_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>np.full_like(reference_matrix, np.nan)<\/code>\u6839\u636e<code>reference_matrix<\/code>\u7684\u5f62\u72b6\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\uff0c\u5e76\u7528<code>np.nan<\/code>\u586b\u5145\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u5df2\u7ecf\u6709\u4e00\u4e2a\u53c2\u8003\u77e9\u9635\uff0c\u5e76\u5e0c\u671b\u751f\u6210\u4e00\u4e2a\u5f62\u72b6\u76f8\u540c\u7684nan\u77e9\u9635\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Pandas\u5e93\u751f\u6210nan\u77e9\u9635<\/p>\n<\/p>\n<p><p>Pandas\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u751f\u6210nan\u77e9\u9635\uff0c\u4e3b\u8981\u65b9\u6cd5\u6709\u4f7f\u7528<code>pd.DataFrame<\/code>\u548c<code>pd.Series<\/code>\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528pd.DataFrame<\/h3>\n<\/p>\n<p><p><code>pd.DataFrame<\/code>\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aDataFrame\uff0c\u5e76\u7528<code>np.nan<\/code>\u586b\u5145\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>\u751f\u6210\u4e00\u4e2a3x3\u7684nan\u77e9\u9635<\/strong><\/h2>\n<p>nan_matrix = pd.DataFrame(np.nan, index=range(3), columns=range(3))<\/p>\n<p>print(nan_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pd.DataFrame(np.nan, index=range(3), columns=range(3))<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684DataFrame\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f<code>np.nan<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528pd.Series<\/h3>\n<\/p>\n<p><p><code>pd.Series<\/code>\u51fd\u6570\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aSeries\uff0c\u5e76\u7528<code>np.nan<\/code>\u586b\u5145\uff0c\u7136\u540e\u901a\u8fc7\u91cd\u5851\u751f\u6210\u4e00\u4e2a\u77e9\u9635\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>\u751f\u6210\u4e00\u4e2a3x3\u7684nan\u77e9\u9635<\/strong><\/h2>\n<p>nan_matrix = pd.Series([np.nan]*9).values.reshape(3, 3)<\/p>\n<p>print(nan_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pd.Series([np.nan]*9).values.reshape(3, 3)<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b9\u4e2a<code>np.nan<\/code>\u7684Series\uff0c\u7136\u540e\u901a\u8fc7<code>reshape<\/code>\u5c06\u5176\u91cd\u5851\u4e3a3&#215;3\u7684\u77e9\u9635\u3002\u8fd9\u4e2a\u65b9\u6cd5\u540c\u6837\u9002\u7528\u4e8e\u9700\u8981\u4f7f\u7528Pandas\u7684\u60c5\u51b5\uff0c\u4f46\u7a0d\u663e\u590d\u6742\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u624b\u52a8\u521b\u5efanan\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u8fd8\u53ef\u4ee5\u624b\u52a8\u521b\u5efa\u4e00\u4e2a\u5305\u542b<code>np.nan<\/code>\u7684\u77e9\u9635\uff0c\u867d\u7136\u4e0d\u5982\u524d\u9762\u7684\u65b9\u6cd5\u7b80\u6d01\uff0c\u4f46\u53ef\u4ee5\u7075\u6d3b\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a3x3\u7684nan\u77e9\u9635<\/strong><\/h2>\n<p>nan_matrix = [[np.nan for _ in range(3)] for _ in range(3)]<\/p>\n<p>print(nan_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f<code>np.nan<\/code>\u3002\u867d\u7136\u7075\u6d3b\u6027\u9ad8\uff0c\u4f46\u4ee3\u7801\u8f83\u4e3a\u5197\u957f\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p><strong>\u751f\u6210\u4e00\u4e2anan\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6700\u5e38\u7528\u548c\u4fbf\u6377\u7684\u662f\u4f7f\u7528NumPy\u5e93\uff0c\u5c24\u5176\u662f<code>np.full<\/code>\u51fd\u6570\u3002<\/strong>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u548c\u624b\u52a8\u521b\u5efa\u7684\u65b9\u6cd5\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u6240\u9700\u7684nan\u77e9\u9635\uff0c\u8fdb\u800c\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u53d1\u6325\u4f5c\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5305\u542bNaN\u503c\u7684\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u751f\u6210\u4e00\u4e2a\u5305\u542bNaN\u503c\u7684\u77e9\u9635\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip install numpy<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>numpy.empty<\/code>\u6216<code>numpy.full<\/code>\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff0c\u5e76\u4f7f\u7528<code>numpy.nan<\/code>\u586b\u5145\u5176\u4e2d\u7684\u5143\u7d20\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a3x3\u7684NaN\u77e9\u9635\nnan_matrix = np.full((3, 3), np.nan)\nprint(nan_matrix)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff0c\u6240\u6709\u5143\u7d20\u5747\u4e3aNaN\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u6765\u66ff\u6362\u77e9\u9635\u4e2d\u7684NaN\u503c\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542bNaN\u503c\u7684\u77e9\u9635\u65f6\uff0c\u901a\u5e38\u9700\u8981\u7528\u7279\u5b9a\u7684\u503c\u66ff\u6362\u8fd9\u4e9bNaN\u3002NumPy\u63d0\u4f9b\u4e86<code>numpy.nan_to_num()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06NaN\u66ff\u6362\u4e3a0\u6216\u5176\u4ed6\u6307\u5b9a\u503c\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>numpy.where()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u66ff\u6362\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\"># \u5c06NaN\u503c\u66ff\u6362\u4e3a0\nreplaced_matrix = np.nan_to_num(nan_matrix, nan=0)\nprint(replaced_matrix)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u6240\u6709NaN\u503c\u66ff\u6362\u4e3a0\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u77e9\u9635\u4e2d\u662f\u5426\u5b58\u5728NaN\u503c\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u77e9\u9635\u4e2d\u662f\u5426\u5b58\u5728NaN\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>numpy.isnan()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u6570\u7ec4\uff0c\u6307\u793a\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4e3aNaN\u3002\u7ed3\u5408<code>numpy.any()<\/code>\u53ef\u4ee5\u5feb\u901f\u5224\u65ad\u6574\u4e2a\u77e9\u9635\u4e2d\u662f\u5426\u5b58\u5728NaN\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\"># \u68c0\u67e5\u77e9\u9635\u4e2d\u662f\u5426\u5b58\u5728NaN\u503c\nhas_nan = np.any(np.isnan(nan_matrix))\nprint(has_nan)  # \u8f93\u51faTrue\u6216False\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u4f1a\u544a\u8bc9\u4f60\u77e9\u9635\u4e2d\u662f\u5426\u5305\u542b\u4efb\u4f55NaN\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u751f\u6210\u4e00\u4e2anan\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u5229\u7528Pandas\u5e93\u3001\u4ee5\u53ca\u624b\u52a8\u521b\u5efa\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1075616,"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\/1075605"}],"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=1075605"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075605\/revisions"}],"predecessor-version":[{"id":1075617,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075605\/revisions\/1075617"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1075616"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1075605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1075605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1075605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}