{"id":1172638,"date":"2025-01-15T16:52:14","date_gmt":"2025-01-15T08:52:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172638.html"},"modified":"2025-01-15T16:52:16","modified_gmt":"2025-01-15T08:52:16","slug":"python%e5%a6%82%e4%bd%95%e5%a1%ab%e8%a1%a5%e7%bc%ba%e5%a4%b1%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1172638.html","title":{"rendered":"python\u5982\u4f55\u586b\u8865\u7f3a\u5931\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074636\/033374b3-0084-42d6-8c09-44899bb656e3.webp\" alt=\"python\u5982\u4f55\u586b\u8865\u7f3a\u5931\u503c\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u586b\u8865\u7f3a\u5931\u503c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ec<strong>\u4f7f\u7528\u5747\u503c\u586b\u8865\u3001\u4f7f\u7528\u4e2d\u4f4d\u6570\u586b\u8865\u3001\u4f7f\u7528\u4f17\u6570\u586b\u8865\u3001\u4f7f\u7528\u524d\u540e\u503c\u586b\u8865\u3001\u63d2\u503c\u6cd5\u3001\u4f7f\u7528<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u9884\u6d4b\u586b\u8865<\/strong>\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u5747\u503c\u586b\u8865\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5747\u503c\u586b\u8865<\/strong>\u662f\u4e00\u79cd\u7b80\u5355\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u5747\u503c\u586b\u8865\u662f\u6307\u7528\u6570\u636e\u96c6\u4e2d\u7684\u5747\u503c\u6765\u66ff\u4ee3\u7f3a\u5931\u503c\uff0c\u9002\u7528\u4e8e\u8fde\u7eed\u578b\u53d8\u91cf\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5982\u679c\u67d0\u4e2a\u5217\u4e2d\u6709\u7f3a\u5931\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u8be5\u5217\u7684\u5747\u503c\uff0c\u7136\u540e\u5c06\u6240\u6709\u7684\u7f3a\u5931\u503c\u66ff\u6362\u6210\u8fd9\u4e2a\u5747\u503c\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u4fdd\u6301\u4e86\u6570\u636e\u7684\u6574\u4f53\u8d8b\u52bf\uff0c\u4f46\u7f3a\u70b9\u662f\u5982\u679c\u7f3a\u5931\u503c\u8f83\u591a\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6570\u636e\u7684\u5206\u5e03\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u548cPandas\u5e93\u6765\u586b\u8865\u7f3a\u5931\u503c\u7684\u5177\u4f53\u793a\u4f8b\uff1a<\/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\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, np.nan, 4, 5],<\/p>\n<p>        &#39;B&#39;: [np.nan, 2, 3, np.nan, 5],<\/p>\n<p>        &#39;C&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5217A\u7684\u5747\u503c<\/strong><\/h2>\n<p>mean_A = df[&#39;A&#39;].mean()<\/p>\n<h2><strong>\u586b\u8865\u5217A\u7684\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df[&#39;A&#39;].fillna(mean_A, inplace=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8ba1\u7b97\u4e86\u5217&#39;A&#39;\u7684\u5747\u503c\uff0c\u7136\u540e\u4f7f\u7528<code>fillna<\/code>\u65b9\u6cd5\u5c06\u5217&#39;A&#39;\u4e2d\u7684\u7f3a\u5931\u503c\u66ff\u6362\u4e3a\u5747\u503c\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5747\u503c\u586b\u8865<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u5747\u503c\u586b\u8865\u7f3a\u5931\u503c\u662f\u6700\u57fa\u7840\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5747\u503c\u586b\u8865\u662f\u4e00\u79cd\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u5c24\u5176\u5728\u6570\u636e\u91cf\u8f83\u5927\u4e14\u7f3a\u5931\u503c\u76f8\u5bf9\u8f83\u5c11\u65f6\uff0c\u6548\u679c\u8f83\u597d\u3002\u5747\u503c\u586b\u8865\u7684\u4e3b\u8981\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h3>1.1 \u8ba1\u7b97\u5747\u503c<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u8ba1\u7b97\u5305\u542b\u7f3a\u5931\u503c\u5217\u7684\u5747\u503c\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>mean<\/code>\u65b9\u6cd5\u6765\u8ba1\u7b97\u3002\u8ba1\u7b97\u5747\u503c\u7684\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mean_value = df[&#39;column_name&#39;].mean()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2 \u586b\u8865\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u8ba1\u7b97\u51fa\u7684\u5747\u503c\u586b\u8865\u7f3a\u5931\u503c\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>fillna<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;column_name&#39;].fillna(mean_value, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u80fd\u5feb\u901f\u586b\u8865\u7f3a\u5931\u503c\uff0c\u4e0d\u4f1a\u5f71\u54cd\u6570\u636e\u96c6\u7684\u6574\u4f53\u89c4\u6a21\u3002\u4f46\u7f3a\u70b9\u662f\u53ef\u80fd\u4f1a\u63a9\u76d6\u6570\u636e\u7684\u771f\u5b9e\u5206\u5e03\uff0c\u5c24\u5176\u662f\u5728\u7f3a\u5931\u503c\u8f83\u591a\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u4e2d\u4f4d\u6570\u586b\u8865<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u4e2d\u4f4d\u6570\u586b\u8865\u662f\u4e00\u79cd\u66f4\u52a0\u9c81\u68d2\u7684\u65b9\u6cd5\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5b58\u5728\u6781\u7aef\u503c\u7684\u6570\u636e\u96c6\u3002\u4e2d\u4f4d\u6570\u586b\u8865\u7684\u6b65\u9aa4\u4e0e\u5747\u503c\u586b\u8865\u7c7b\u4f3c\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u8ba1\u7b97\u4e2d\u4f4d\u6570<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u8ba1\u7b97\u5305\u542b\u7f3a\u5931\u503c\u5217\u7684\u4e2d\u4f4d\u6570\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>median<\/code>\u65b9\u6cd5\u6765\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">median_value = df[&#39;column_name&#39;].median()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2 \u586b\u8865\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u8ba1\u7b97\u51fa\u7684\u4e2d\u4f4d\u6570\u586b\u8865\u7f3a\u5931\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;column_name&#39;].fillna(median_value, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e2d\u4f4d\u6570\u586b\u8865\u7684\u4f18\u70b9\u662f\u5bf9\u6781\u7aef\u503c\u4e0d\u654f\u611f\uff0c\u80fd\u66f4\u597d\u5730\u53cd\u6620\u6570\u636e\u7684\u4e2d\u5fc3\u8d8b\u52bf\u3002\u7136\u800c\uff0c\u7f3a\u70b9\u662f\u53ef\u80fd\u65e0\u6cd5\u51c6\u786e\u53cd\u6620\u6570\u636e\u7684\u6574\u4f53\u5206\u5e03\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u4f17\u6570\u586b\u8865<\/h2>\n<\/p>\n<p><p>\u4f17\u6570\u586b\u8865\u9002\u7528\u4e8e\u7c7b\u522b\u578b\u53d8\u91cf\u3002\u4f17\u6570\u662f\u6307\u6570\u636e\u96c6\u4e2d\u51fa\u73b0\u9891\u7387\u6700\u9ad8\u7684\u503c\u3002\u4f17\u6570\u586b\u8865\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h3>3.1 \u8ba1\u7b97\u4f17\u6570<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u8ba1\u7b97\u5305\u542b\u7f3a\u5931\u503c\u5217\u7684\u4f17\u6570\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>mode<\/code>\u65b9\u6cd5\u6765\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mode_value = df[&#39;column_name&#39;].mode()[0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u586b\u8865\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u8ba1\u7b97\u51fa\u7684\u4f17\u6570\u586b\u8865\u7f3a\u5931\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;column_name&#39;].fillna(mode_value, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f17\u6570\u586b\u8865\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u7528\u4e8e\u7c7b\u522b\u578b\u53d8\u91cf\u3002\u4f46\u7f3a\u70b9\u662f\u53ef\u80fd\u4f1a\u5f15\u5165\u504f\u5dee\uff0c\u5c24\u5176\u662f\u5728\u4f17\u6570\u5360\u6bd4\u8f83\u5927\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u524d\u540e\u503c\u586b\u8865<\/h2>\n<\/p>\n<p><p>\u524d\u540e\u503c\u586b\u8865\u662f\u4e00\u79cd\u57fa\u4e8e\u90bb\u8fd1\u503c\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u5e38\u89c1\u7684\u524d\u540e\u503c\u586b\u8865\u65b9\u6cd5\u6709\u5411\u524d\u586b\u8865\u548c\u5411\u540e\u586b\u8865\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u5411\u524d\u586b\u8865<\/h3>\n<\/p>\n<p><p>\u5411\u524d\u586b\u8865\u662f\u7528\u7f3a\u5931\u503c\u524d\u4e00\u4e2a\u6709\u6548\u503c\u6765\u586b\u8865\u7f3a\u5931\u503c\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>fillna<\/code>\u65b9\u6cd5\u5e76\u6307\u5b9a<code>method=&#39;ffill&#39;<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;column_name&#39;].fillna(method=&#39;ffill&#39;, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u5411\u540e\u586b\u8865<\/h3>\n<\/p>\n<p><p>\u5411\u540e\u586b\u8865\u662f\u7528\u7f3a\u5931\u503c\u540e\u4e00\u4e2a\u6709\u6548\u503c\u6765\u586b\u8865\u7f3a\u5931\u503c\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>fillna<\/code>\u65b9\u6cd5\u5e76\u6307\u5b9a<code>method=&#39;bfill&#39;<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;column_name&#39;].fillna(method=&#39;bfill&#39;, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u524d\u540e\u503c\u586b\u8865\u7684\u4f18\u70b9\u662f\u80fd\u4fdd\u6301\u6570\u636e\u7684\u8fde\u7eed\u6027\uff0c\u9002\u7528\u4e8e\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u4f46\u7f3a\u70b9\u662f\u53ef\u80fd\u4f1a\u5f15\u5165\u6ede\u540e\u6548\u5e94\u6216\u63d0\u524d\u6548\u5e94\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528\u63d2\u503c\u6cd5<\/h2>\n<\/p>\n<p><p>\u63d2\u503c\u6cd5\u662f\u4e00\u79cd\u57fa\u4e8e\u6570\u5b66\u6a21\u578b\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u8fde\u7eed\u578b\u53d8\u91cf\u3002\u5e38\u89c1\u7684\u63d2\u503c\u65b9\u6cd5\u6709\u7ebf\u6027\u63d2\u503c\u548c\u591a\u9879\u5f0f\u63d2\u503c\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u7ebf\u6027\u63d2\u503c<\/h3>\n<\/p>\n<p><p>\u7ebf\u6027\u63d2\u503c\u662f\u7528\u7f3a\u5931\u503c\u524d\u540e\u4e24\u4e2a\u6709\u6548\u503c\u7684\u7ebf\u6027\u7ec4\u5408\u6765\u586b\u8865\u7f3a\u5931\u503c\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>interpolate<\/code>\u65b9\u6cd5\u5e76\u6307\u5b9a<code>method=&#39;linear&#39;<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;column_name&#39;].interpolate(method=&#39;linear&#39;, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2 \u591a\u9879\u5f0f\u63d2\u503c<\/h3>\n<\/p>\n<p><p>\u591a\u9879\u5f0f\u63d2\u503c\u662f\u7528\u591a\u9879\u5f0f\u51fd\u6570\u6765\u62df\u5408\u6570\u636e\u5e76\u586b\u8865\u7f3a\u5931\u503c\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>interpolate<\/code>\u65b9\u6cd5\u5e76\u6307\u5b9a<code>method=&#39;polynomial&#39;<\/code>\u548c\u591a\u9879\u5f0f\u7684\u9636\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;column_name&#39;].interpolate(method=&#39;polynomial&#39;, order=2, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63d2\u503c\u6cd5\u7684\u4f18\u70b9\u662f\u80fd\u8f83\u597d\u5730\u62df\u5408\u6570\u636e\u7684\u53d8\u5316\u8d8b\u52bf\uff0c\u9002\u7528\u4e8e\u8fde\u7eed\u578b\u53d8\u91cf\u3002\u4f46\u7f3a\u70b9\u662f\u8ba1\u7b97\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u4e14\u5bf9\u5f02\u5e38\u503c\u654f\u611f\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u6a21\u578b\u9884\u6d4b\u586b\u8865<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u6a21\u578b\u9884\u6d4b\u586b\u8865\u662f\u4e00\u79cd\u9ad8\u7ea7\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u96c6\u3002\u53ef\u4ee5\u4f7f\u7528\u56de\u5f52\u6a21\u578b\u3001\u5206\u7c7b\u6a21\u578b\u7b49\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u6765\u9884\u6d4b\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><h3>6.1 \u51c6\u5907\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u51c6\u5907\u8bad\u7ec3\u6570\u636e\u548c\u6d4b\u8bd5\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u7684<code>dropna<\/code>\u65b9\u6cd5\u6765\u53bb\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_data = df.dropna()<\/p>\n<p>test_data = df[df[&#39;column_name&#39;].isna()]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.2 \u8bad\u7ec3\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u9009\u62e9\u5408\u9002\u7684\u673a\u5668\u5b66\u4e60\u6a21\u578b\u8fdb\u884c\u8bad\u7ec3\u3002\u4ee5\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528Scikit-learn\u5e93\u7684<code>LinearRegression<\/code>\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import LinearRegression<\/p>\n<p>model = LinearRegression()<\/p>\n<p>X_train = train_data.drop(columns=[&#39;column_name&#39;])<\/p>\n<p>y_train = train_data[&#39;column_name&#39;]<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.3 \u9884\u6d4b\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u9884\u6d4b\u7f3a\u5931\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">X_test = test_data.drop(columns=[&#39;column_name&#39;])<\/p>\n<p>predicted_values = model.predict(X_test)<\/p>\n<p>df.loc[df[&#39;column_name&#39;].isna(), &#39;column_name&#39;] = predicted_values<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u6a21\u578b\u9884\u6d4b\u586b\u8865\u7684\u4f18\u70b9\u662f\u80fd\u5145\u5206\u5229\u7528\u6570\u636e\u7684\u590d\u6742\u5173\u7cfb\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u96c6\u3002\u4f46\u7f3a\u70b9\u662f\u9700\u8981\u8f83\u9ad8\u7684\u8ba1\u7b97\u8d44\u6e90\uff0c\u4e14\u6a21\u578b\u7684\u9009\u62e9\u548c\u8c03\u53c2\u8fc7\u7a0b\u8f83\u4e3a\u590d\u6742\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u7efc\u5408\u8fd0\u7528\u591a\u79cd\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7efc\u5408\u8fd0\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u586b\u8865\u7f3a\u5931\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528\u5747\u503c\u586b\u8865\u3001\u4e2d\u4f4d\u6570\u586b\u8865\u6216\u4f17\u6570\u586b\u8865\u6765\u5904\u7406\u90e8\u5206\u7f3a\u5931\u503c\uff0c\u7136\u540e\u4f7f\u7528\u63d2\u503c\u6cd5\u6216\u673a\u5668\u5b66\u4e60\u6a21\u578b\u9884\u6d4b\u586b\u8865\u5269\u4f59\u7684\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><h3>7.1 \u5206\u6b65\u9aa4\u586b\u8865<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528\u7b80\u5355\u7684\u65b9\u6cd5\u586b\u8865\u90e8\u5206\u7f3a\u5931\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;column_name&#39;].fillna(mean_value, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528\u9ad8\u7ea7\u7684\u65b9\u6cd5\u586b\u8865\u5269\u4f59\u7684\u7f3a\u5931\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;column_name&#39;].interpolate(method=&#39;linear&#39;, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7.2 \u6a21\u578b\u878d\u5408<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u673a\u5668\u5b66\u4e60\u6a21\u578b\u8fdb\u884c\u9884\u6d4b\uff0c\u5e76\u878d\u5408\u9884\u6d4b\u7ed3\u679c\u6765\u63d0\u9ad8\u586b\u8865\u7684\u51c6\u786e\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u548c\u968f\u673a\u68ee\u6797\u6a21\u578b\u8fdb\u884c\u9884\u6d4b\uff0c\u7136\u540e\u53d6\u9884\u6d4b\u7ed3\u679c\u7684\u5e73\u5747\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.ensemble import RandomForestRegressor<\/p>\n<h2><strong>\u8bad\u7ec3\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>lr_model = LinearRegression()<\/p>\n<p>lr_model.fit(X_train, y_train)<\/p>\n<h2><strong>\u8bad\u7ec3\u968f\u673a\u68ee\u6797\u6a21\u578b<\/strong><\/h2>\n<p>rf_model = RandomForestRegressor()<\/p>\n<p>rf_model.fit(X_train, y_train)<\/p>\n<h2><strong>\u9884\u6d4b\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>lr_predictions = lr_model.predict(X_test)<\/p>\n<p>rf_predictions = rf_model.predict(X_test)<\/p>\n<h2><strong>\u878d\u5408\u9884\u6d4b\u7ed3\u679c<\/strong><\/h2>\n<p>final_predictions = (lr_predictions + rf_predictions) \/ 2<\/p>\n<p>df.loc[df[&#39;column_name&#39;].isna(), &#39;column_name&#39;] = final_predictions<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u5408\u8fd0\u7528\u591a\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u80fd\u5145\u5206\u5229\u7528\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u52bf\uff0c\u63d0\u9ad8\u586b\u8865\u7684\u51c6\u786e\u6027\u548c\u9c81\u68d2\u6027\u3002\u4f46\u7f3a\u70b9\u662f\u65b9\u6cd5\u8f83\u4e3a\u590d\u6742\uff0c\u9700\u8981\u8f83\u9ad8\u7684\u8ba1\u7b97\u8d44\u6e90\u548c\u7ecf\u9a8c\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u603b\u7ed3\u4e0e\u5efa\u8bae<\/h2>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f3a\u5931\u503c\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u975e\u5e38\u91cd\u8981\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u548c\u5e94\u7528\u573a\u666f\u3002\u5728\u9009\u62e9\u586b\u8865\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<\/p>\n<p><h3>8.1 \u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8fde\u7eed\u578b\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f18\u5148\u8003\u8651\u5747\u503c\u586b\u8865\u3001\u4e2d\u4f4d\u6570\u586b\u8865\u3001\u63d2\u503c\u6cd5\u6216\u673a\u5668\u5b66\u4e60\u6a21\u578b\u9884\u6d4b\u586b\u8865\u3002\u5bf9\u4e8e\u7c7b\u522b\u578b\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f18\u5148\u8003\u8651\u4f17\u6570\u586b\u8865\u3002<\/p>\n<\/p>\n<p><h3>8.2 \u7f3a\u5931\u503c\u6bd4\u4f8b<\/h3>\n<\/p>\n<p><p>\u5f53\u7f3a\u5931\u503c\u6bd4\u4f8b\u8f83\u4f4e\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u7b80\u5355\u7684\u65b9\u6cd5\u5982\u5747\u503c\u586b\u8865\u3001\u4e2d\u4f4d\u6570\u586b\u8865\u6216\u4f17\u6570\u586b\u8865\u3002\u5f53\u7f3a\u5931\u503c\u6bd4\u4f8b\u8f83\u9ad8\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u590d\u6742\u7684\u65b9\u6cd5\u5982\u63d2\u503c\u6cd5\u6216\u673a\u5668\u5b66\u4e60\u6a21\u578b\u9884\u6d4b\u586b\u8865\u3002<\/p>\n<\/p>\n<p><h3>8.3 \u6570\u636e\u5206\u5e03<\/h3>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u586b\u8865\u65b9\u6cd5\u65f6\uff0c\u8981\u8003\u8651\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u3002\u5bf9\u4e8e\u5b58\u5728\u6781\u7aef\u503c\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u9009\u62e9\u4e2d\u4f4d\u6570\u586b\u8865\u6216\u63d2\u503c\u6cd5\u3002\u5bf9\u4e8e\u5206\u5e03\u8f83\u4e3a\u5747\u5300\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u9009\u62e9\u5747\u503c\u586b\u8865\u3002<\/p>\n<\/p>\n<p><h3>8.4 \u8ba1\u7b97\u8d44\u6e90<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u7684\u65b9\u6cd5\u5bf9\u8ba1\u7b97\u8d44\u6e90\u7684\u9700\u6c42\u4e0d\u540c\u3002\u7b80\u5355\u7684\u65b9\u6cd5\u5982\u5747\u503c\u586b\u8865\u3001\u4e2d\u4f4d\u6570\u586b\u8865\u6216\u4f17\u6570\u586b\u8865\u8ba1\u7b97\u8d44\u6e90\u9700\u6c42\u8f83\u4f4e\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u3002\u590d\u6742\u7684\u65b9\u6cd5\u5982\u63d2\u503c\u6cd5\u6216\u673a\u5668\u5b66\u4e60\u6a21\u578b\u9884\u6d4b\u586b\u8865\u8ba1\u7b97\u8d44\u6e90\u9700\u6c42\u8f83\u9ad8\uff0c\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u6216\u5c40\u90e8\u586b\u8865\u3002<\/p>\n<\/p>\n<p><h3>8.5 \u4e1a\u52a1\u9700\u6c42<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u5177\u4f53\u7684\u4e1a\u52a1\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u586b\u8865\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5728\u91d1\u878d\u6570\u636e\u5206\u6790\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u52a0\u51c6\u786e\u548c\u4fdd\u5b88\u7684\u586b\u8865\u65b9\u6cd5\uff1b\u5728\u7535\u5546\u63a8\u8350\u7cfb\u7edf\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u52a0\u5feb\u901f\u548c\u9ad8\u6548\u7684\u586b\u8865\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u7efc\u5408\u8fd0\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u9010\u6b65\u4f18\u5316\u586b\u8865\u7684\u6548\u679c\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u8fd0\u7528\u586b\u8865\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u7f3a\u5931\u503c\uff0c\u63d0\u5347\u6570\u636e\u8d28\u91cf\u548c\u5206\u6790\u7ed3\u679c\u7684\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8bc6\u522b\u7f3a\u5931\u503c\u901a\u5e38\u4f7f\u7528Pandas\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>isnull()<\/code>\u6216<code>isna()<\/code>\u51fd\u6570\u6765\u68c0\u67e5DataFrame\u6216Series\u4e2d\u7684\u7f3a\u5931\u503c\u3002\u8fd9\u4e9b\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u7684DataFrame\u6216Series\uff0c\u6307\u793a\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4e3a\u7f3a\u5931\u503c\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u8ba1\u7b97\u51fa\u6bcf\u4e00\u5217\u4e2d\u7f3a\u5931\u503c\u7684\u6570\u91cf\uff0c\u4ece\u800c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u6765\u586b\u8865\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u586b\u8865\u7f3a\u5931\u503c\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>fillna()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u7279\u5b9a\u7684\u503c\u3001\u5747\u503c\u3001\u4e2d\u4f4d\u6570\u6216\u4f17\u6570\u586b\u8865\u7f3a\u5931\u503c\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>interpolate()<\/code>\u65b9\u6cd5\u901a\u8fc7\u63d2\u503c\u6cd5\u586b\u8865\u7f3a\u5931\u503c\uff0c\u6216\u8005\u5229\u7528<code>dropna()<\/code>\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u6216\u5217\u3002\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u901a\u5e38\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u6027\u8d28\u548c\u5206\u6790\u7684\u76ee\u7684\u3002<\/p>\n<p><strong>\u586b\u8865\u7f3a\u5931\u503c\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u586b\u8865\u7f3a\u5931\u503c\u65f6\uff0c\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u5206\u5e03\u548c\u586b\u8865\u65b9\u6cd5\u5bf9\u5206\u6790\u7ed3\u679c\u7684\u5f71\u54cd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5747\u503c\u586b\u8865\u53ef\u80fd\u4f1a\u5f71\u54cd\u6570\u636e\u7684\u65b9\u5dee\uff0c\u5bfc\u81f4\u6a21\u578b\u7684\u504f\u5dee\u3002\u6b64\u5916\uff0c\u586b\u8865\u7f3a\u5931\u503c\u65f6\u5e94\u907f\u514d\u5f15\u5165\u8fc7\u591a\u7684\u5047\u8bbe\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u96c6\u8f83\u5c0f\u6216\u7f3a\u5931\u503c\u6bd4\u4f8b\u8f83\u9ad8\u7684\u60c5\u51b5\u4e0b\u3002\u4e86\u89e3\u6570\u636e\u7684\u80cc\u666f\u548c\u4e1a\u52a1\u903b\u8f91\uff0c\u5c06\u6709\u52a9\u4e8e\u9009\u62e9\u5408\u9002\u7684\u586b\u8865\u7b56\u7565\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u586b\u8865\u7f3a\u5931\u503c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ec\u4f7f\u7528\u5747\u503c\u586b\u8865\u3001\u4f7f\u7528\u4e2d\u4f4d\u6570\u586b\u8865\u3001\u4f7f\u7528\u4f17\u6570\u586b\u8865\u3001\u4f7f\u7528\u524d\u540e\u503c\u586b\u8865 [&hellip;]","protected":false},"author":3,"featured_media":1172648,"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\/1172638"}],"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=1172638"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172638\/revisions"}],"predecessor-version":[{"id":1172651,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172638\/revisions\/1172651"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172648"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}