{"id":1074158,"date":"2025-01-08T11:32:06","date_gmt":"2025-01-08T03:32:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1074158.html"},"modified":"2025-01-08T11:32:08","modified_gmt":"2025-01-08T03:32:08","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%8e%bb%e6%8e%89%e5%b8%a6%e8%b4%9f%e5%80%bc%e7%9a%84%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1074158.html","title":{"rendered":"\u5982\u4f55\u7528python\u53bb\u6389\u5e26\u8d1f\u503c\u7684\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103859\/35da3472-51f5-47a6-8257-3c4c91d598d6.webp\" alt=\"\u5982\u4f55\u7528python\u53bb\u6389\u5e26\u8d1f\u503c\u7684\u884c\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u53bb\u6389\u5e26\u8d1f\u503c\u7684\u884c<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u53bb\u6389\u5e26\u8d1f\u503c\u7684\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3002<strong>pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5de5\u5177<\/strong>\uff0c\u4f7f\u5f97\u5904\u7406\u6570\u636e\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u548c\u9ad8\u6548\u3002<strong>\u901a\u8fc7\u68c0\u67e5\u6bcf\u884c\u662f\u5426\u5305\u542b\u8d1f\u503c\u5e76\u5c06\u5176\u5220\u9664\uff0c\u60a8\u53ef\u4ee5\u6e05\u7406\u6570\u636e\u96c6<\/strong>\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u52a0\u8f7d\u6570\u636e<\/strong>\uff1a\u9996\u5148\uff0c\u60a8\u9700\u8981\u52a0\u8f7d\u6570\u636e\u96c6\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6CSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u68c0\u67e5\u8d1f\u503c<\/strong>\uff1a\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u68c0\u67e5\u6bcf\u4e00\u884c\u662f\u5426\u5305\u542b\u8d1f\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>apply<\/code>\u548c<code>lambda<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u884c\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/li>\n<li><strong>\u5220\u9664\u8d1f\u503c\u884c<\/strong>\uff1a\u6700\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u5220\u9664\u5305\u542b\u8d1f\u503c\u7684\u884c\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u52a0\u8f7d\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7d\u6570\u636e\u96c6\uff0c\u8fd9\u91cc\u5047\u8bbe\u6570\u636e\u5b58\u50a8\u5728\u4e00\u4e2aCSV\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6\u5230DataFrame<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u68c0\u67e5\u8d1f\u503c<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>apply<\/code>\u65b9\u6cd5\u548c<code>lambda<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u6bcf\u884c\u662f\u5426\u5305\u542b\u8d1f\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u6bcf\u884c\u662f\u5426\u5305\u542b\u8d1f\u503c<\/p>\n<p>has_negative = df.apply(lambda row: (row &lt; 0).any(), axis=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5220\u9664\u8d1f\u503c\u884c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u5220\u9664\u5305\u542b\u8d1f\u503c\u7684\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u5305\u542b\u8d1f\u503c\u7684\u884c<\/p>\n<p>df_cleaned = df[~has_negative]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528pandas\u53bb\u6389\u5e26\u8d1f\u503c\u7684\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u636e\u5b58\u50a8\u5728\u540d\u4e3a&#39;data.csv&#39;\u7684CSV\u6587\u4ef6\u4e2d<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u6253\u5370\u539f\u59cb\u6570\u636e<\/strong><\/h2>\n<p>print(&quot;\u539f\u59cb\u6570\u636e:&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u68c0\u67e5\u6bcf\u884c\u662f\u5426\u5305\u542b\u8d1f\u503c<\/strong><\/h2>\n<p>has_negative = df.apply(lambda row: (row &lt; 0).any(), axis=1)<\/p>\n<h2><strong>\u5220\u9664\u5305\u542b\u8d1f\u503c\u7684\u884c<\/strong><\/h2>\n<p>df_cleaned = df[~has_negative]<\/p>\n<h2><strong>\u6253\u5370\u6e05\u7406\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>print(&quot;\\n\u6e05\u7406\u540e\u7684\u6570\u636e:&quot;)<\/p>\n<p>print(df_cleaned)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u89e3\u91ca<\/h3>\n<\/p>\n<ol>\n<li><strong>\u52a0\u8f7d\u6570\u636e<\/strong>\uff1a\u4f7f\u7528<code>pd.read_csv<\/code>\u51fd\u6570\u4eceCSV\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u5b58\u50a8\u5230DataFrame\u4e2d\u3002<\/li>\n<li><strong>\u68c0\u67e5\u8d1f\u503c<\/strong>\uff1a\u4f7f\u7528<code>apply<\/code>\u65b9\u6cd5\u904d\u5386\u6bcf\u884c\uff0c\u5e76\u4f7f\u7528<code>lambda<\/code>\u51fd\u6570\u68c0\u67e5\u884c\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u5c0f\u4e8e0\u3002\u5982\u679c\u884c\u4e2d\u5b58\u5728\u8d1f\u503c\uff0c\u5219\u8fd4\u56de<code>True<\/code>\uff0c\u5426\u5219\u8fd4\u56de<code>False<\/code>\u3002<\/li>\n<li><strong>\u5220\u9664\u8d1f\u503c\u884c<\/strong>\uff1a\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15<code>~has_negative<\/code>\u6765\u9009\u62e9\u4e0d\u5305\u542b\u8d1f\u503c\u7684\u884c\uff0c\u4ece\u800c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684DataFrame <code>df_cleaned<\/code>\u3002<\/li>\n<\/ol>\n<p><h3>\u5904\u7406\u5176\u4ed6\u6570\u636e\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86CSV\u6587\u4ef6\uff0cpandas\u8fd8\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\uff0c\u5982Excel\u3001SQL\u6570\u636e\u5e93\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>\u8bfb\u53d6Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6Excel\u6587\u4ef6\u5230DataFrame<\/p>\n<p>df = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8bfb\u53d6SQL\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5\u5230SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;data.db&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6SQL\u67e5\u8be2\u7ed3\u679c\u5230DataFrame<\/strong><\/h2>\n<p>df = pd.read_sql_query(&#39;SELECT * FROM table_name&#39;, conn)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5904\u7406\u590d\u6742\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u53ef\u80fd\u66f4\u4e3a\u590d\u6742\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5904\u7406\u590d\u6742\u6570\u636e\u7684\u6280\u5de7\uff1a<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u591a\u5217<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6570\u636e\u96c6\u4e2d\u5305\u542b\u591a\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>any<\/code>\u6216<code>all<\/code>\u51fd\u6570\u6765\u68c0\u67e5\u591a\u5217\u4e2d\u7684\u8d1f\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u68c0\u67e5\u6bcf\u884c\u662f\u5426\u5305\u542b\u4efb\u4f55\u8d1f\u503c<\/p>\n<p>has_negative = df.apply(lambda row: row[[&#39;col1&#39;, &#39;col2&#39;, &#39;col3&#39;]].lt(0).any(), axis=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5904\u7406\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f3a\u5931\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>dropna<\/code>\u65b9\u6cd5\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c<\/p>\n<p>df = df.dropna()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528pandas\u5e93\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u53bb\u6389\u5e26\u8d1f\u503c\u7684\u884c\u3002\u901a\u8fc7\u52a0\u8f7d\u6570\u636e\u3001\u68c0\u67e5\u8d1f\u503c\u548c\u5220\u9664\u8d1f\u503c\u884c\uff0c\u53ef\u4ee5\u6e05\u7406\u6570\u636e\u96c6\u5e76\u4e3a\u540e\u7eed\u5206\u6790\u505a\u597d\u51c6\u5907\u3002<strong>\u65e0\u8bba\u662f\u5904\u7406\u7b80\u5355\u7684CSV\u6587\u4ef6\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\uff0cpandas\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u5e2e\u52a9\u60a8\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e<\/strong>\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u66f4\u597d\u5730\u8fdb\u884c\u6570\u636e\u6e05\u7406\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u548c\u5220\u9664\u542b\u6709\u8d1f\u503c\u7684\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u8f7b\u677e\u8bc6\u522b\u548c\u5220\u9664\u542b\u6709\u8d1f\u503c\u7684\u884c\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165Pandas\u5e93\u5e76\u8bfb\u53d6\u6570\u636e\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u8fc7\u6ee4\u6389\u542b\u6709\u8d1f\u503c\u7684\u884c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>df[df &gt;= 0].dropna()<\/code>\u6765\u83b7\u53d6\u53ea\u5305\u542b\u975e\u8d1f\u503c\u7684\u884c\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5904\u7406\u7f3a\u5931\u503c\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u4e0d\u5220\u9664\u6709\u6548\u6570\u636e\uff1f<\/strong><br \/>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u5220\u9664\u542b\u6709\u8d1f\u503c\u7684\u884c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e22\u5931\u4e00\u4e9b\u91cd\u8981\u7684\u4fe1\u606f\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u5efa\u8bae\u60a8\u5728\u5220\u9664\u524d\u5148\u8fdb\u884c\u6570\u636e\u5ba1\u67e5\u548c\u7edf\u8ba1\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>df.describe()<\/code>\u6765\u67e5\u770b\u6570\u636e\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u4ee5\u4fbf\u8bc4\u4f30\u54ea\u4e9b\u6570\u636e\u662f\u6709\u6548\u7684\uff0c\u54ea\u4e9b\u662f\u9700\u8981\u5220\u9664\u7684\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u6709\u6ca1\u6709\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u66ff\u6362\u8d1f\u503c\u800c\u4e0d\u662f\u5220\u9664\u6574\u884c\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u4e0d\u60f3\u5220\u9664\u542b\u6709\u8d1f\u503c\u7684\u884c\uff0c\u53ef\u4ee5\u8003\u8651\u66ff\u6362\u8d1f\u503c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>df[df &lt; 0] = 0<\/code>\u5c06\u6240\u6709\u8d1f\u503c\u66ff\u6362\u4e3a\u96f6\u3002\u8fd9\u6837\u53ef\u4ee5\u4fdd\u7559\u6240\u6709\u884c\uff0c\u540c\u65f6\u786e\u4fdd\u6570\u636e\u7684\u6709\u6548\u6027\u548c\u4e00\u81f4\u6027\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u66f4\u5408\u9002\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u5206\u6790\u6216<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5efa\u6a21\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u53bb\u6389\u5e26\u8d1f\u503c\u7684\u884c \u8981\u5728Python\u4e2d\u53bb\u6389\u5e26\u8d1f\u503c\u7684\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3002pandas\u5e93\u63d0\u4f9b\u4e86 [&hellip;]","protected":false},"author":3,"featured_media":1074163,"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\/1074158"}],"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=1074158"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074158\/revisions"}],"predecessor-version":[{"id":1074165,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1074158\/revisions\/1074165"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1074163"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1074158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1074158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1074158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}