{"id":1128572,"date":"2025-01-08T20:21:51","date_gmt":"2025-01-08T12:21:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1128572.html"},"modified":"2025-01-08T20:21:54","modified_gmt":"2025-01-08T12:21:54","slug":"%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e7%a9%ba%e5%80%bcpython%e6%95%b0%e6%8d%ae%e5%88%86%e6%9e%90%e7%bb%9f%e8%ae%a1%e6%9c%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1128572.html","title":{"rendered":"\u5982\u4f55\u5220\u9664\u7a7a\u503cpython\u6570\u636e\u5206\u6790\u7edf\u8ba1\u670d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095242\/825192f3-e23f-437a-9a28-bfd15e2bba6c.webp\" alt=\"\u5982\u4f55\u5220\u9664\u7a7a\u503cpython\u6570\u636e\u5206\u6790\u7edf\u8ba1\u670d\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5220\u9664\u7a7a\u503cPython\u6570\u636e\u5206\u6790\u7edf\u8ba1<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5220\u9664\u7a7a\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 Pandas \u5e93\u3001\u5220\u9664\u7a7a\u503c\u884c\u3001\u5220\u9664\u7a7a\u503c\u5217\u3001\u586b\u5145\u7a7a\u503c\u3001\u4f7f\u7528\u63d2\u503c\u6cd5\u3002<\/strong> \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5904\u7406\u6570\u636e\u4e2d\u7684\u7a7a\u503c\uff0c\u5e76\u786e\u4fdd\u6211\u4eec\u7684\u6570\u636e\u5206\u6790\u7ed3\u679c\u66f4\u51c6\u786e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 Pandas \u5e93<\/h3>\n<\/p>\n<p><p>Pandas \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u9886\u57df\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u6570\u636e\u4e2d\u7684\u7a7a\u503c\u3002\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528 Pandas \u5904\u7406\u7a7a\u503c\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165 Pandas<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5 Pandas \u5e93\u3002\u5982\u679c\u60a8\u8fd8\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u60a8\u7684 Python \u811a\u672c\u4e2d\u5bfc\u5165 Pandas\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u8bfb\u53d6\u6570\u636e\u96c6\u3002Pandas \u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\uff0c\u4f8b\u5982 CSV\u3001Excel\u3001SQL \u6570\u636e\u5e93\u7b49\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u8bfb\u53d6 CSV \u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_csv(&#39;your_dataset.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5220\u9664\u7a7a\u503c\u884c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5220\u9664\u5305\u542b\u7a7a\u503c\u7684\u884c\u662f\u6700\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\u3002Pandas \u63d0\u4f9b\u4e86 <code>dropna<\/code> \u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h4>1. \u5220\u9664\u6240\u6709\u5305\u542b\u7a7a\u503c\u7684\u884c<\/h4>\n<\/p>\n<p><p>\u8981\u5220\u9664\u6240\u6709\u5305\u542b\u81f3\u5c11\u4e00\u4e2a\u7a7a\u503c\u7684\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_cleaned = df.dropna()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5220\u9664\u7279\u5b9a\u5217\u4e2d\u5305\u542b\u7a7a\u503c\u7684\u884c<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u53ea\u60f3\u5220\u9664\u67d0\u4e00\u5217\u5305\u542b\u7a7a\u503c\u7684\u884c\uff0c\u53ef\u4ee5\u6307\u5b9a <code>subset<\/code> \u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_cleaned = df.dropna(subset=[&#39;column_name&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5220\u9664\u7a7a\u503c\u5217<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u67d0\u4e9b\u5217\u53ef\u80fd\u5305\u542b\u5927\u91cf\u7a7a\u503c\uff0c\u8fd9\u65f6\u5019\u5220\u9664\u8fd9\u4e9b\u5217\u53ef\u80fd\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>dropna<\/code> \u65b9\u6cd5\u7684 <code>axis<\/code> \u53c2\u6570\u6765\u5220\u9664\u5305\u542b\u7a7a\u503c\u7684\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_cleaned = df.dropna(axis=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u586b\u5145\u7a7a\u503c<\/h3>\n<\/p>\n<p><p>\u5220\u9664\u7a7a\u503c\u867d\u7136\u7b80\u5355\uff0c\u4f46\u6709\u65f6\u53ef\u80fd\u4f1a\u4e22\u5931\u91cd\u8981\u4fe1\u606f\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u586b\u5145\u7a7a\u503c\u3002Pandas \u63d0\u4f9b\u4e86 <code>fillna<\/code> \u65b9\u6cd5\u6765\u586b\u5145\u7a7a\u503c\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u5e38\u6570\u586b\u5145<\/h4>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u7279\u5b9a\u7684\u5e38\u6570\u6765\u586b\u5145\u7a7a\u503c\uff0c\u4f8b\u5982 0 \u6216\u67d0\u4e2a\u5747\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_filled = df.fillna(0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5747\u503c\u586b\u5145<\/h4>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5217\u7684\u5747\u503c\u3001\u4f17\u6570\u6216\u4e2d\u4f4d\u6570\u6765\u586b\u5145\u7a7a\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_filled = df.fillna(df.mean())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u63d2\u503c\u6cd5<\/h3>\n<\/p>\n<p><p>\u63d2\u503c\u6cd5\u662f\u4e00\u79cd\u5229\u7528\u73b0\u6709\u6570\u636e\u70b9\u6765\u4f30\u8ba1\u672a\u77e5\u6570\u636e\u70b9\u7684\u65b9\u6cd5\u3002Pandas \u63d0\u4f9b\u4e86 <code>interpolate<\/code> \u65b9\u6cd5\u6765\u5b9e\u73b0\u63d2\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_interpolated = df.interpolate()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3\u4e0e\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u6570\u636e\u4e2d\u7684\u7a7a\u503c\u662f\u6570\u636e\u6e05\u6d17\u4e2d\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u56e0\u6b64\u4e86\u89e3\u5e76\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u4f7f\u7528 Pandas \u5e93\u6709\u6548\u5730\u5904\u7406\u6570\u636e\u4e2d\u7684\u7a7a\u503c\uff0c\u63d0\u5347\u6570\u636e\u5206\u6790\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p><strong>\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u6570\u636e\u6e05\u6d17\u662f\u4e00\u4e2a\u53cd\u590d\u8fed\u4ee3\u7684\u8fc7\u7a0b\uff0c\u9700\u8981\u4e0d\u65ad\u5730\u68c0\u67e5\u548c\u8c03\u6574\uff0c\u4ee5\u786e\u4fdd\u6700\u7ec8\u7684\u6570\u636e\u96c6\u8d28\u91cf\u6700\u4f73\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8bf7\u8bb0\u4f4f\uff0c\u5904\u7406\u6570\u636e\u4e2d\u7684\u7a7a\u503c\u53ea\u662f\u6570\u636e\u6e05\u6d17\u7684\u4e00\u90e8\u5206\uff0c\u5176\u4ed6\u6b65\u9aa4\u5305\u62ec\u5904\u7406\u91cd\u590d\u503c\u3001\u6807\u51c6\u5316\u6570\u636e\u683c\u5f0f\u7b49\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u4e3a\u60a8\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u6307\u5bfc\u548c\u53c2\u8003\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u6570\u636e\u6846\u4e2d\u7684\u7a7a\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u8bc6\u522b\u6570\u636e\u6846\u4e2d\u7684\u7a7a\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>isnull()<\/code>\u6216<code>isna()<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u54ea\u4e9b\u6570\u636e\u662f\u7a7a\u503c\u3002\u8fd9\u4e24\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u7684\u6570\u636e\u6846\uff0c\u6307\u793a\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4e3a\u7a7a\u3002\u7ed3\u5408<code>sum()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6bcf\u4e00\u5217\u7a7a\u503c\u7684\u6570\u91cf\uff0c\u5e2e\u52a9\u7528\u6237\u4e86\u89e3\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p><strong>\u5728\u5220\u9664\u7a7a\u503c\u4e4b\u524d\uff0c\u5982\u4f55\u5904\u7406\u7a7a\u503c\u6570\u636e\uff1f<\/strong><br \/>\u5728\u5220\u9664\u7a7a\u503c\u4e4b\u524d\uff0c\u7528\u6237\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u586b\u5145\u65b9\u6cd5\u6765\u5904\u7406\u7a7a\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>fillna()<\/code>\u51fd\u6570\u7528\u5e73\u5747\u503c\u3001\u4e2d\u4f4d\u6570\u6216\u7279\u5b9a\u503c\u66ff\u4ee3\u7a7a\u503c\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931\u5e26\u6765\u7684\u5f71\u54cd\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u9009\u62e9\u5220\u9664\u5305\u542b\u7a7a\u503c\u7684\u884c\u6216\u5217\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u6570\u636e\u5206\u6790\u7684\u9700\u6c42\u548c\u6570\u636e\u96c6\u7684\u5927\u5c0f\u3002<\/p>\n<p><strong>\u5220\u9664\u7a7a\u503c\u540e\uff0c\u5982\u4f55\u9a8c\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\uff1f<\/strong><br \/>\u5728\u5220\u9664\u7a7a\u503c\u540e\uff0c\u7528\u6237\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528<code>isnull()<\/code>\u6216<code>isna()<\/code>\u51fd\u6570\u68c0\u67e5\u6570\u636e\u6846\uff0c\u786e\u4fdd\u6240\u6709\u7a7a\u503c\u5df2\u88ab\u6210\u529f\u5220\u9664\u3002\u6b64\u5916\uff0c\u7ed3\u5408<code>info()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u67e5\u770b\u6570\u636e\u6846\u7684\u6574\u4f53\u4fe1\u606f\uff0c\u5305\u62ec\u6570\u636e\u7c7b\u578b\u548c\u975e\u7a7a\u503c\u7684\u6570\u91cf\uff0c\u4ece\u800c\u9a8c\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u51c6\u786e\u6027\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u540e\u7eed\u5206\u6790\u7684\u57fa\u7840\u6570\u636e\u662f\u53ef\u9760\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5220\u9664\u7a7a\u503cPython\u6570\u636e\u5206\u6790\u7edf\u8ba1 \u5220\u9664\u7a7a\u503c\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528 Pandas \u5e93\u3001\u5220\u9664\u7a7a\u503c\u884c\u3001\u5220\u9664\u7a7a\u503c\u5217\u3001\u586b\u5145\u7a7a [&hellip;]","protected":false},"author":3,"featured_media":1128580,"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\/1128572"}],"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=1128572"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128572\/revisions"}],"predecessor-version":[{"id":1128583,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128572\/revisions\/1128583"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1128580"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1128572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1128572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1128572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}