{"id":1016541,"date":"2024-12-27T12:17:21","date_gmt":"2024-12-27T04:17:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1016541.html"},"modified":"2024-12-27T12:17:24","modified_gmt":"2024-12-27T04:17:24","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8anan%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1016541.html","title":{"rendered":"python\u5982\u4f55\u628anan\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100040\/b96a7811-46a9-4ca4-a0d5-2a33129430cc.webp\" alt=\"python\u5982\u4f55\u628anan\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5904\u7406NaN\u503c\uff0c\u5305\u62ec\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>fillna()<\/code>\u65b9\u6cd5\u586b\u5145NaN\u503c\u3001\u4f7f\u7528<code>dropna()<\/code>\u65b9\u6cd5\u5220\u9664\u5305\u542bNaN\u7684\u884c\u6216\u5217\u3001\u4ee5\u53ca\u4f7f\u7528<code>numpy<\/code>\u5e93\u7684<code>nan_to_num()<\/code>\u65b9\u6cd5\u5c06NaN\u66ff\u6362\u4e3a\u96f6\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>fillna()<\/code>\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u586b\u5145\u9009\u9879\uff0c\u6bd4\u5982\u6307\u5b9a\u586b\u5145\u503c\u3001\u4f7f\u7528\u524d\u4e00\u4e2a\u6216\u540e\u4e00\u4e2a\u6709\u6548\u503c\u586b\u5145\u7b49\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93\u5904\u7406NaN\u503c<\/p>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u5bf9\u4e8eNaN\u503c\uff0c<code>pandas<\/code>\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u586b\u5145NaN\u503c<\/h3>\n<\/p>\n<p><p><code>fillna()<\/code>\u662f<code>pandas<\/code>\u5e93\u4e2d\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u5141\u8bb8\u4f60\u7528\u6307\u5b9a\u7684\u503c\u6216\u65b9\u6cd5\u6765\u66ff\u6362NaN\u503c\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\u4e00\u4e2a\u5305\u542bNaN\u503c\u7684DataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, 2, np.nan, 4],<\/p>\n<p>    &#39;B&#39;: [np.nan, 2, 3, 4],<\/p>\n<p>    &#39;C&#39;: [1, 2, 3, np.nan]<\/p>\n<p>})<\/p>\n<h2><strong>\u4f7f\u75280\u586b\u5145NaN\u503c<\/strong><\/h2>\n<p>df_filled = df.fillna(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6240\u6709\u7684NaN\u503c\u90fd\u88ab\u66ff\u6362\u4e3a0\u3002<code>fillna()<\/code>\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u63a5\u53d7\u5176\u4ed6\u53c2\u6570\uff0c\u6bd4\u5982\u7528\u4e00\u4e2a\u5b57\u5178\u6765\u6307\u5b9a\u4e0d\u540c\u5217\u7684\u4e0d\u540c\u586b\u5145\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u4e0d\u540c\u7684\u503c\u586b\u5145\u4e0d\u540c\u5217\u7684NaN\u503c<\/p>\n<p>df_filled_different = df.fillna({&#39;A&#39;: 0, &#39;B&#39;: 1, &#39;C&#39;: 2})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2 \u4f7f\u7528<code>dropna()<\/code>\u65b9\u6cd5\u5220\u9664NaN\u503c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5e0c\u671b\u5220\u9664\u5305\u542bNaN\u503c\u7684\u884c\u6216\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>dropna()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u5305\u542bNaN\u503c\u7684\u884c<\/p>\n<p>df_dropped_rows = df.dropna()<\/p>\n<h2><strong>\u5220\u9664\u5305\u542bNaN\u503c\u7684\u5217<\/strong><\/h2>\n<p>df_dropped_cols = df.dropna(axis=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>dropna()<\/code>\u65b9\u6cd5\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u53ef\u4ee5\u63a7\u5236\u5220\u9664\u7684\u884c\u4e3a\uff0c\u6bd4\u5982<code>how=&#39;all&#39;<\/code>\u8868\u793a\u53ea\u5220\u9664\u6240\u6709\u503c\u5747\u4e3aNaN\u7684\u884c\u6216\u5217\uff0c<code>thresh<\/code>\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u975eNaN\u503c\u7684\u6700\u5c11\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93\u5904\u7406NaN\u503c<\/p>\n<\/p>\n<p><h3>2.1 \u4f7f\u7528<code>nan_to_num()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86<code>nan_to_num()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u6570\u7ec4\u4e2d\u7684NaN\u503c\u66ff\u6362\u4e3a\u96f6\uff0c\u6216\u5176\u4ed6\u6307\u5b9a\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542bNaN\u503c\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, np.nan, 4])<\/p>\n<h2><strong>\u5c06NaN\u503c\u66ff\u6362\u4e3a0<\/strong><\/h2>\n<p>arr_filled = np.nan_to_num(arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>nan_to_num()<\/code>\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u63a5\u53d7<code>nan<\/code>\u53c2\u6570\uff0c\u6307\u5b9a\u7528\u6765\u66ff\u6362NaN\u503c\u7684\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06NaN\u503c\u66ff\u6362\u4e3a-1<\/p>\n<p>arr_filled_with_minus_one = np.nan_to_num(arr, nan=-1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>scipy<\/code>\u5e93\u7684\u63d2\u503c\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7528\u63d2\u503c\u6cd5\u6765\u586b\u5145NaN\u503c\u53ef\u80fd\u66f4\u4e3a\u5408\u7406\u3002<code>scipy<\/code>\u5e93\u63d0\u4f9b\u4e86\u63d2\u503c\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765<a href=\"https:\/\/docs.pingcode.com\/agile\/project-management\/estimation\" target=\"_blank\">\u4f30\u7b97<\/a>\u548c\u586b\u5145\u6570\u636e\u4e2d\u7684NaN\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import interpolate<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u65f6\u95f4\u5e8f\u5217\uff0c\u5176\u4e2d\u4e00\u4e9b\u503c\u4e22\u5931\uff08\u5373NaN\uff09<\/strong><\/h2>\n<p>x = np.array([0, 1, 2, 3, 4, 5])<\/p>\n<p>y = np.array([1, np.nan, 3, np.nan, 5, 6])<\/p>\n<h2><strong>\u4f7f\u7528\u7ebf\u6027\u63d2\u503c\u65b9\u6cd5<\/strong><\/h2>\n<p>linear_interpolator = interpolate.interp1d(x[~np.isnan(y)], y[~np.isnan(y)], kind=&#39;linear&#39;, fill_value=&quot;extrapolate&quot;)<\/p>\n<h2><strong>\u586b\u5145NaN\u503c<\/strong><\/h2>\n<p>y_filled = linear_interpolator(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>interp1d<\/code>\u51fd\u6570\u8fdb\u884c\u7ebf\u6027\u63d2\u503c\uff0c<code>kind<\/code>\u53c2\u6570\u53ef\u4ee5\u6307\u5b9a\u63d2\u503c\u7684\u7c7b\u578b\uff0c\u6bd4\u5982\u7ebf\u6027\u3001\u4e8c\u6b21\u3001\u4e09\u6b21\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u65b9\u6cd5\u9884\u6d4bNaN\u503c<\/p>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u5904\u7406\u7f3a\u5931\u503c\u901a\u5e38\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u53ef\u4ee5\u5229\u7528\u673a\u5668\u5b66\u4e60\u6a21\u578b\u6765\u9884\u6d4b\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u4f7f\u7528<code>sklearn<\/code>\u7684<code>KNNImputer<\/code><\/h3>\n<\/p>\n<p><p><code>KNNImputer<\/code>\u662f<code>sklearn<\/code>\u5e93\u4e2d\u4e00\u4e2a\u7528\u4e8e\u586b\u5145\u7f3a\u5931\u503c\u7684\u5de5\u5177\u3002\u5b83\u4f7f\u7528k-\u6700\u8fd1\u90bb\u7b97\u6cd5\uff0c\u6839\u636e\u6570\u636e\u96c6\u4e2d\u5176\u4ed6\u76f8\u4f3c\u7684\u6837\u672c\u6765\u9884\u6d4b\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.impute import KNNImputer<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542bNaN\u503c\u7684DataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, 2, np.nan, 4],<\/p>\n<p>    &#39;B&#39;: [np.nan, 2, 3, 4],<\/p>\n<p>    &#39;C&#39;: [1, 2, 3, np.nan]<\/p>\n<p>})<\/p>\n<h2><strong>\u521d\u59cb\u5316KNNImputer<\/strong><\/h2>\n<p>imputer = KNNImputer(n_neighbors=2)<\/p>\n<h2><strong>\u586b\u5145NaN\u503c<\/strong><\/h2>\n<p>df_imputed = imputer.fit_transform(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>KNNImputer<\/code>\u53ef\u4ee5\u6709\u6548\u5229\u7528\u6570\u636e\u7684\u5185\u5728\u7ed3\u6784\u6765\u9884\u6d4b\u7f3a\u5931\u503c\uff0c\u5c24\u5176\u5728\u6570\u636e\u6a21\u5f0f\u8f83\u4e3a\u590d\u6742\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u5904\u7406NaN\u503c<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528\u7279\u5b9a\u7684\u903b\u8f91\u6216\u89c4\u5219\u6765\u5904\u7406NaN\u503c\u3002\u4f60\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u4e8e\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def custom_fillna(series):<\/p>\n<p>    # \u81ea\u5b9a\u4e49\u89c4\u5219\uff0c\u6bd4\u5982\u7528\u5217\u7684\u5747\u503c\u586b\u5145<\/p>\n<p>    mean_value = series.mean()<\/p>\n<p>    return series.fillna(mean_value)<\/p>\n<h2><strong>\u5e94\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>df_custom_filled = df.apply(custom_fillna)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u6700\u5927\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u9700\u6c42\u5b9a\u5236\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5904\u7406NaN\u503c\u662f\u6570\u636e\u6e05\u6d17\u4e2d\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u6839\u636e\u5177\u4f53\u7684\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u5347\u6570\u636e\u5206\u6790\u548c\u6a21\u578b\u8bad\u7ec3\u7684\u6548\u679c\u3002<code>pandas<\/code>\u548c<code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u51fd\u6570\u6765\u586b\u5145\u548c\u5220\u9664NaN\u503c\uff0c\u800c<code>scipy<\/code>\u548c<code>sklearn<\/code>\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u65b9\u6cd5\u8fdb\u884c\u63d2\u503c\u548c\u9884\u6d4b\u3002\u5728\u5904\u7406NaN\u503c\u65f6\uff0c\u7406\u89e3\u6570\u636e\u7684\u80cc\u666f\u548c\u7279\u5f81\u662f\u9009\u62e9\u5408\u9002\u65b9\u6cd5\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522bNaN\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8bc6\u522bNaN\u503c\u901a\u5e38\u4f7f\u7528NumPy\u5e93\u7684<code>np.isnan()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u68c0\u67e5\u6570\u7ec4\u6216\u6570\u636e\u6846\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4e3aNaN\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5c06\u6570\u636e\u6846\u7684\u67d0\u4e00\u5217\u4f20\u9012\u7ed9\u8be5\u51fd\u6570\uff0c\u4ee5\u83b7\u53d6\u4e00\u4e2a\u5e03\u5c14\u6570\u7ec4\uff0c\u6307\u793a\u54ea\u4e9b\u5143\u7d20\u662fNaN\u3002\u8fd9\u5bf9\u4e8e\u6570\u636e\u6e05\u7406\u548c\u9884\u5904\u7406\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u66ff\u6362NaN\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>fillna()<\/code>\u65b9\u6cd5\u6765\u66ff\u6362NaN\u503c\u3002\u60a8\u53ef\u4ee5\u9009\u62e9\u7528\u7279\u5b9a\u7684\u503c\u66ff\u6362NaN\uff0c\u6216\u8005\u4f7f\u7528\u5747\u503c\u3001\u4e2d\u4f4d\u6570\u7b49\u7edf\u8ba1\u91cf\u8fdb\u884c\u66ff\u6362\u3002\u4f8b\u5982\uff0c<code>df[&#39;column_name&#39;].fillna(value=0)<\/code>\u4f1a\u628a\u6307\u5b9a\u5217\u4e2d\u7684\u6240\u6709NaN\u503c\u66ff\u6362\u4e3a0\u3002\u6b64\u65b9\u6cd5\u5728\u5904\u7406\u7f3a\u5931\u6570\u636e\u65f6\u975e\u5e38\u7075\u6d3b\u3002<\/p>\n<p><strong>\u5982\u4f55\u5220\u9664\u5305\u542bNaN\u503c\u7684\u884c\u6216\u5217\uff1f<\/strong><br \/>\u4f7f\u7528Pandas\u7684<code>dropna()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5220\u9664\u5305\u542bNaN\u503c\u7684\u884c\u6216\u5217\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8c03\u7528<code>df.dropna()<\/code>\u4f1a\u5220\u9664\u4efb\u4f55\u5305\u542bNaN\u7684\u884c\u3002\u5982\u679c\u60a8\u5e0c\u671b\u5220\u9664\u5305\u542bNaN\u7684\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>df.dropna(axis=1)<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u6570\u636e\u6e05\u7406\u4e2d\u975e\u5e38\u6709\u6548\uff0c\u786e\u4fdd\u6570\u636e\u96c6\u7684\u5b8c\u6574\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5904\u7406NaN\u503c\uff0c\u5305\u62ec\u4f7f\u7528pandas\u5e93\u7684fillna()\u65b9\u6cd5\u586b\u5145NaN\u503c\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1016546,"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\/1016541"}],"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=1016541"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016541\/revisions"}],"predecessor-version":[{"id":1016547,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1016541\/revisions\/1016547"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1016546"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1016541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1016541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1016541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}