{"id":994542,"date":"2024-12-27T08:56:23","date_gmt":"2024-12-27T00:56:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994542.html"},"modified":"2024-12-27T08:56:27","modified_gmt":"2024-12-27T00:56:27","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e7%bc%ba%e5%a4%b1%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994542.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\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\/25071452\/21855f62-8c5b-4483-baad-70652f73f8f6.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u7f3a\u5931\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5220\u9664\u7f3a\u5931\u503c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>dropna()<\/code>\u51fd\u6570\u3001\u6307\u5b9a\u8f74\u548c\u5220\u9664\u7b56\u7565\u3001\u7ed3\u5408\u6570\u636e\u7c7b\u578b\u5904\u7406\u3002<\/strong>\u5176\u4e2d\uff0c<code>dropna()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u65b9\u4fbf\u5730\u5220\u9664DataFrame\u6216Series\u4e2d\u7684\u7f3a\u5931\u503c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u63d0\u4f9b\u4e00\u4e9b\u4ee3\u7801\u793a\u4f8b\u548c\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Pandas\u5e93\u5904\u7406\u7f3a\u5931\u503c<\/p>\n<\/p>\n<p><p><code>pandas<\/code>\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8bc6\u522b\u548c\u5220\u9664\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>dropna()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>dropna()<\/code>\u662fpandas\u5e93\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u7528\u4e8e\u5220\u9664DataFrame\u6216Series\u4e2d\u7684\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, None, 4],<\/p>\n<p>        &#39;B&#39;: [None, 2, 3, 4],<\/p>\n<p>        &#39;C&#39;: [1, None, None, 4]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528dropna()\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c<\/strong><\/h2>\n<p>df_cleaned = df.dropna()<\/p>\n<p>print(df_cleaned)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> <code>dropna()<\/code>\u51fd\u6570\u9ed8\u8ba4\u5220\u9664\u6240\u6709\u5305\u542b\u81f3\u5c11\u4e00\u4e2a\u7f3a\u5931\u503c\u7684\u884c\u3002\u5982\u679c\u60f3\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u5217\uff0c\u53ef\u4ee5\u5c06\u53c2\u6570<code>axis<\/code>\u8bbe\u4e3a1\uff08\u5373<code>df.dropna(axis=1)<\/code>\uff09\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>how<\/code>\u53c2\u6570\u6307\u5b9a\u5220\u9664\u7b56\u7565\uff1a<code>how=&#39;any&#39;<\/code>\u8868\u793a\u53ea\u8981\u6709\u4e00\u4e2a\u7f3a\u5931\u503c\u5c31\u5220\u9664\uff08\u9ed8\u8ba4\uff09\uff0c<code>how=&#39;all&#39;<\/code>\u8868\u793a\u53ea\u6709\u5f53\u6240\u6709\u503c\u90fd\u7f3a\u5931\u65f6\u624d\u5220\u9664\u3002<\/p>\n<\/p>\n<p><h3>\u5220\u9664\u7279\u5b9a\u5217\u4e2d\u7684\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ea\u5e0c\u671b\u5220\u9664\u67d0\u4e00\u5217\u4e2d\u7684\u7f3a\u5931\u503c\uff0c\u800c\u4fdd\u7559\u5176\u4ed6\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u7279\u5b9a\u5217&#39;A&#39;\u4e2d\u7684\u7f3a\u5931\u503c<\/p>\n<p>df_cleaned_A = df.dropna(subset=[&#39;A&#39;])<\/p>\n<p>print(df_cleaned_A)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u901a\u8fc7<code>subset<\/code>\u53c2\u6570\uff0c<code>dropna()<\/code>\u5141\u8bb8\u6211\u4eec\u6307\u5b9a\u9700\u8981\u68c0\u67e5\u7f3a\u5931\u503c\u7684\u5217\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u7ed3\u5408\u6570\u636e\u7c7b\u578b\u5904\u7406\u7f3a\u5931\u503c<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>fillna()<\/code>\u66ff\u6362\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5220\u9664\u7f3a\u5931\u503c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u91cf\u4e0d\u8db3\uff0c\u6b64\u65f6\u53ef\u4ee5\u9009\u62e9\u7528\u67d0\u4e2a\u503c\u66ff\u6362\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u75280\u66ff\u6362\u6240\u6709\u7f3a\u5931\u503c<\/p>\n<p>df_filled = df.fillna(0)<\/p>\n<p>print(df_filled)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> <code>fillna()<\/code>\u53ef\u4ee5\u7528\u6307\u5b9a\u7684\u503c\u66ff\u6362DataFrame\u6216Series\u4e2d\u7684\u7f3a\u5931\u503c\u3002\u9664\u4e86\u5e38\u7528\u7684\u6570\u503c\u66ff\u6362\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u65b9\u6cd5\u53c2\u6570\uff08\u5982<code>method=&#39;ffill&#39;<\/code>\u6216<code>method=&#39;bfill&#39;<\/code>\uff09\u8fdb\u884c\u524d\u5411\u586b\u5145\u6216\u540e\u5411\u586b\u5145\u3002<\/p>\n<\/p>\n<p><h3>\u6839\u636e\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u66ff\u6362<\/h3>\n<\/p>\n<p><p>\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u7f3a\u5931\u503c\u5904\u7406\u65b9\u6cd5\u53ef\u80fd\u4e0d\u540c\uff0c\u5982\u6570\u503c\u578b\u6570\u636e\u53ef\u4ee5\u4f7f\u7528\u5747\u503c\u6216\u4e2d\u4f4d\u6570\u586b\u8865\uff0c\u800c\u5b57\u7b26\u4e32\u578b\u6570\u636e\u53ef\u4ee5\u4f7f\u7528\u7a7a\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u5360\u4f4d\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u5747\u503c\u5e76\u66ff\u6362\u6570\u503c\u578b\u5217\u7684\u7f3a\u5931\u503c<\/p>\n<p>df[&#39;A&#39;] = df[&#39;A&#39;].fillna(df[&#39;A&#39;].mean())<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u5bf9\u4e8e\u6570\u503c\u578b\u6570\u636e\uff0c\u901a\u5e38\u4f7f\u7528\u5747\u503c\u6216\u4e2d\u4f4d\u6570\u66ff\u6362\u7f3a\u5931\u503c\uff0c\u4ee5\u5c3d\u91cf\u4fdd\u6301\u6570\u636e\u5206\u5e03\u4e0d\u53d8\u3002\u5bf9\u4e8e\u5206\u7c7b\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4f17\u6570\uff08\u51fa\u73b0\u9891\u7387\u6700\u9ad8\u7684\u503c\uff09\u8fdb\u884c\u586b\u5145\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528NumPy\u5e93\u8bc6\u522b\u7f3a\u5931\u503c<\/p>\n<\/p>\n<p><p>\u867d\u7136<code>pandas<\/code>\u662f\u5904\u7406\u7f3a\u5931\u503c\u7684\u9996\u9009\u5e93\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u4e5f\u9700\u8981\u501f\u52a9<code>NumPy<\/code>\u6765\u8bc6\u522b\u548c\u5904\u7406\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<code>numpy<\/code>\u8bc6\u522b\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u4e2d\u7684<code>nan<\/code>\u8868\u793a\u7f3a\u5931\u503c\uff0c\u53ef\u4ee5\u901a\u8fc7<code>numpy<\/code>\u63d0\u4f9b\u7684\u51fd\u6570\u8fdb\u884c\u8bc6\u522b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u7f3a\u5931\u503c\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, np.nan, 3, 4, np.nan])<\/p>\n<h2><strong>\u4f7f\u7528numpy\u7684isnan()\u51fd\u6570\u8bc6\u522b\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>mask = np.isnan(arr)<\/p>\n<p>print(mask)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> <code>np.isnan()<\/code>\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u578b\u6570\u7ec4\uff0c\u6807\u8bc6\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4e3a\u7f3a\u5931\u503c\u3002\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u5e03\u5c14\u63a9\u7801\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5904\u7406\uff0c\u5982\u5220\u9664\u6216\u66ff\u6362\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5904\u7406\u6570\u636e\u6846\u4e2d\u7684\u590d\u6742\u7f3a\u5931\u503c\u95ee\u9898<\/p>\n<\/p>\n<p><h3>\u5220\u9664\u9ad8\u5ea6\u7f3a\u5931\u7684\u5217<\/h3>\n<\/p>\n<p><p>\u5f53\u67d0\u4e9b\u5217\u7684\u7f3a\u5931\u503c\u6bd4\u4f8b\u8f83\u9ad8\u65f6\uff0c\u5220\u9664\u8fd9\u4e9b\u5217\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u7f3a\u5931\u503c\u6bd4\u4f8b\u5927\u4e8e50%\u7684\u5217<\/p>\n<p>threshold = len(df) * 0.5<\/p>\n<p>df_reduced = df.dropna(thresh=threshold, axis=1)<\/p>\n<p>print(df_reduced)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> <code>dropna()<\/code>\u7684<code>thresh<\/code>\u53c2\u6570\u5141\u8bb8\u6211\u4eec\u6307\u5b9a\u81f3\u5c11\u9700\u8981\u591a\u5c11\u4e2a\u975e\u7f3a\u5931\u503c\u7684\u6570\u636e\u624d\u80fd\u4fdd\u7559\u4e0b\u6765\u3002\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u6570\u636e\u96c6\u548c\u5206\u6790\u76ee\u6807\u8c03\u6574\u8fd9\u4e2a\u9608\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u5229\u7528\u6570\u636e\u5206\u6790\u5de5\u5177\u8fdb\u884c\u7f3a\u5931\u503c\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u53ef\u89c6\u5316\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u7f3a\u5931\u503c\u7684\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u4f7f\u7528seaborn\u7684heatmap\u53ef\u89c6\u5316\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>sns.heatmap(df.isnull(), cbar=False, cmap=&#39;viridis&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u901a\u8fc7\u4f7f\u7528<code>seaborn<\/code>\u5e93\u7684<code>heatmap()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u89c2\u5730\u770b\u5230\u7f3a\u5931\u503c\u7684\u5206\u5e03\u60c5\u51b5\u3002\u8fd9\u6709\u52a9\u4e8e\u6211\u4eec\u66f4\u597d\u5730\u51b3\u5b9a\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u7f3a\u5931\u503c\u7684\u9ad8\u7ea7\u6280\u5de7<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u63d2\u503c\u65b9\u6cd5\u586b\u8865\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u63d2\u503c\u662f\u4e00\u79cd\u5e38\u7528\u7684\u586b\u8865\u7f3a\u5931\u503c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u65f6\u95f4\u5e8f\u5217DataFrame<\/p>\n<p>dates = pd.date_range(&#39;20230101&#39;, periods=6)<\/p>\n<p>ts_df = pd.DataFrame({&#39;Value&#39;: [1, np.nan, 3, np.nan, 5, 6]}, index=dates)<\/p>\n<h2><strong>\u4f7f\u7528\u7ebf\u6027\u63d2\u503c\u586b\u8865\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>ts_df_interpolated = ts_df.interpolate(method=&#39;linear&#39;)<\/p>\n<p>print(ts_df_interpolated)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u63d2\u503c\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765<a href=\"https:\/\/docs.pingcode.com\/agile\/project-management\/estimation\" target=\"_blank\">\u4f30\u7b97<\/a>\u548c\u586b\u8865\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u4e2d\u7684\u7f3a\u5931\u503c\uff0c<code>interpolate()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u591a\u79cd\u63d2\u503c\u65b9\u6cd5\uff0c\u5982\u7ebf\u6027\u63d2\u503c\u3001\u65f6\u95f4\u63d2\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u9884\u6d4b\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><p>\u5f53\u6570\u636e\u7279\u5f81\u4e30\u5bcc\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u6a21\u578b\u9884\u6d4b\u5e76\u586b\u8865\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.ensemble import RandomForestRegressor<\/p>\n<p>from sklearn.model_selection import tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/p>\n<h2><strong>\u5047\u8bbedf\u4e2d\u6709\u4e00\u4e2a\u76ee\u6807\u5217&#39;y&#39;\u6709\u7f3a\u5931\u503c\uff0c\u6211\u4eec\u7528\u5176\u4ed6\u5217\u9884\u6d4b&#39;y&#39;<\/strong><\/h2>\n<p>df_no_missing_y = df.dropna(subset=[&#39;y&#39;])<\/p>\n<p>X = df_no_missing_y.drop(&#39;y&#39;, axis=1)<\/p>\n<p>y = df_no_missing_y[&#39;y&#39;]<\/p>\n<h2><strong>\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u5212\u5206<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u4f7f\u7528\u968f\u673a\u68ee\u6797\u56de\u5f52\u6a21\u578b\u9884\u6d4b\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>model = RandomForestRegressor()<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<h2><strong>\u9884\u6d4b\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>missing_y = df[df[&#39;y&#39;].isnull()].drop(&#39;y&#39;, axis=1)<\/p>\n<p>predicted_y = model.predict(missing_y)<\/p>\n<p>df.loc[df[&#39;y&#39;].isnull(), &#39;y&#39;] = predicted_y<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u673a\u5668\u5b66\u4e60\u6a21\u578b\u53ef\u4ee5\u5229\u7528\u6570\u636e\u96c6\u4e2d\u5176\u4ed6\u7279\u5f81\u6765\u9884\u6d4b\u5e76\u586b\u8865\u7f3a\u5931\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6570\u636e\u7279\u5f81\u4e30\u5bcc\u4e14\u76f8\u4e92\u5173\u8054\u7684\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u7f3a\u5931\u503c\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><h3>\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u7f3a\u5931\u503c\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u4e1a\u52a1\u9700\u6c42\u548c\u6570\u636e\u5206\u6790\u76ee\u6807\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5220\u9664\u7f3a\u5931\u503c\u53ef\u80fd\u662f\u6700\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c\u5728\u5176\u4ed6\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u63d2\u503c\u6216\u9884\u6d4b\u65b9\u6cd5\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\u3002<\/p>\n<\/p>\n<p><h3>\u8bb0\u5f55\u7f3a\u5931\u503c\u5904\u7406\u8fc7\u7a0b<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u9884\u5904\u7406\u4e2d\uff0c\u8bb0\u5f55\u6bcf\u4e00\u6b65\u7684\u7f3a\u5931\u503c\u5904\u7406\u64cd\u4f5c\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u63d0\u9ad8\u5206\u6790\u7684\u900f\u660e\u6027\uff0c\u4e5f\u4fbf\u4e8e\u540e\u7eed\u7684\u6a21\u578b\u9a8c\u8bc1\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u9a8c\u8bc1\u7f3a\u5931\u503c\u5904\u7406\u6548\u679c<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u5904\u7406\u7f3a\u5931\u503c\uff0c\u6700\u7ec8\u90fd\u9700\u8981\u9a8c\u8bc1\u5904\u7406\u6548\u679c\u3002\u53ef\u4ee5\u901a\u8fc7\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u6216\u68c0\u67e5\u6570\u636e\u5206\u6790\u7ed3\u679c\u7684\u5408\u7406\u6027\u6765\u9a8c\u8bc1\u7f3a\u5931\u503c\u5904\u7406\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u5904\u7406\u7f3a\u5931\u503c\uff0c\u63d0\u9ad8\u6570\u636e\u5206\u6790\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5220\u9664\u7f3a\u5931\u503c\u7684\u5e38\u7528\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5904\u7406\u7f3a\u5931\u503c\u7684\u4e3b\u8981\u5e93\u662fPandas\u3002\u4f7f\u7528Pandas\u7684<code>dropna()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5220\u9664\u7f3a\u5931\u503c\u3002\u60a8\u53ef\u4ee5\u9009\u62e9\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u6216\u5217\u3002\u4f8b\u5982\uff0c<code>df.dropna(axis=0)<\/code>\u4f1a\u5220\u9664\u6240\u6709\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\uff0c\u800c<code>df.dropna(axis=1)<\/code>\u5219\u4f1a\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u5217\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>thresh<\/code>\u53c2\u6570\u6765\u4fdd\u7559\u5177\u6709\u81f3\u5c11\u4e00\u5b9a\u6570\u91cf\u975e\u7f3a\u5931\u503c\u7684\u884c\u6216\u5217\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u6570\u636e\u96c6\u4e2d\u7f3a\u5931\u503c\u7684\u5206\u5e03\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u5220\u9664\u7f3a\u5931\u503c\u4e4b\u524d\uff0c\u4e86\u89e3\u6570\u636e\u96c6\u4e2d\u7f3a\u5931\u503c\u7684\u5206\u5e03\u662f\u5f88\u91cd\u8981\u7684\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>isnull().sum()<\/code>\u65b9\u6cd5\u6765\u67e5\u770b\u6bcf\u4e00\u5217\u7684\u7f3a\u5931\u503c\u6570\u91cf\u3002<code>df.isnull().sum()<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6bcf\u5217\u7f3a\u5931\u503c\u6570\u91cf\u7684Series\uff0c\u5e2e\u52a9\u60a8\u8bc6\u522b\u7f3a\u5931\u503c\u7684\u4e25\u91cd\u7a0b\u5ea6\u3002\u6b64\u5916\uff0c\u5229\u7528\u53ef\u89c6\u5316\u5e93\u5982Matplotlib\u6216Seaborn\uff0c\u60a8\u53ef\u4ee5\u7ed8\u5236\u70ed\u56fe\u6765\u76f4\u89c2\u5c55\u793a\u7f3a\u5931\u503c\u7684\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5220\u9664\u7f3a\u5931\u503c\u540e\uff0c\u5982\u4f55\u9a8c\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\uff1f<\/strong><br \/>\u5728\u5220\u9664\u7f3a\u5931\u503c\u540e\uff0c\u68c0\u67e5\u6570\u636e\u7684\u5b8c\u6574\u6027\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u4f7f\u7528<code>df.info()<\/code>\u65b9\u6cd5\u67e5\u770b\u6570\u636e\u7684\u7ef4\u5ea6\u3001\u6570\u636e\u7c7b\u578b\u4ee5\u53ca\u975e\u7f3a\u5931\u503c\u7684\u6570\u91cf\u3002\u540c\u65f6\uff0c\u60a8\u8fd8\u53ef\u4ee5\u91cd\u65b0\u8c03\u7528<code>isnull().sum()<\/code>\u6765\u786e\u8ba4\u7f3a\u5931\u503c\u662f\u5426\u5df2\u88ab\u6210\u529f\u5220\u9664\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u96c6\u7684\u8d28\u91cf\uff0c\u5e76\u5728\u540e\u7eed\u5206\u6790\u4e2d\u83b7\u5f97\u66f4\u51c6\u786e\u7684\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u7f3a\u5931\u503c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u7684dropna()\u51fd\u6570\u3001\u6307\u5b9a\u8f74\u548c\u5220\u9664\u7b56\u7565\u3001\u7ed3\u5408\u6570\u636e\u7c7b\u578b\u5904\u7406\u3002\u5176 [&hellip;]","protected":false},"author":3,"featured_media":994554,"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\/994542"}],"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=994542"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994542\/revisions"}],"predecessor-version":[{"id":994555,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994542\/revisions\/994555"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994554"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}