{"id":1103285,"date":"2025-01-08T16:08:43","date_gmt":"2025-01-08T08:08:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1103285.html"},"modified":"2025-01-08T16:08:46","modified_gmt":"2025-01-08T08:08:46","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e6%8d%ae%e5%88%a0%e9%99%a4%e4%b8%80%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1103285.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6570\u636e\u5220\u9664\u4e00\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065204\/1010409a-ef66-4d0c-b378-11b5cef9b522.webp\" alt=\"python\u5982\u4f55\u5c06\u6570\u636e\u5220\u9664\u4e00\u5217\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5220\u9664\u6570\u636e\u6846\u7684\u4e00\u5217\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u7684drop\u65b9\u6cd5\u3001\u5220\u9664\u5217\u7684\u76f4\u63a5\u64cd\u4f5c\u7b49\uff0c\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Pandas\u5e93\u7684drop()\u65b9\u6cd5\u3001\u76f4\u63a5\u5220\u9664\u5217\u3001\u4f7f\u7528iloc\u6216\u5176\u4ed6\u65b9\u6cd5\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0cPandas\u5e93\u7684drop()\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u6700\u5e38\u7528\u4e14\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u5e93\u7684drop()\u65b9\u6cd5\u662fPython\u5220\u9664\u6570\u636e\u6846\u4e2d\u4e00\u5217\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c\u5206\u6790\u3002\u8981\u4f7f\u7528drop()\u65b9\u6cd5\u5220\u9664\u4e00\u5217\uff0c\u53ef\u4ee5\u6309\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5220\u9664\u5217 &#39;B&#39;<\/strong><\/h2>\n<p>df = df.drop(columns=[&#39;B&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528drop()\u65b9\u6cd5\u5e76\u4f20\u9012columns\u53c2\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u5220\u9664\u6307\u5b9a\u7684\u5217\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u89e3\u91ca\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528Pandas\u5e93\u7684drop()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>Pandas\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684drop()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u6570\u636e\u6846\u4e2d\u7684\u884c\u6216\u5217\u3002\u8981\u5220\u9664\u4e00\u5217\uff0c\u53ea\u9700\u8bbe\u7f6eaxis\u53c2\u6570\u4e3a1\u6216\u8005\u4f7f\u7528columns\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528axis\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528axis\u53c2\u6570\u65f6\uff0c\u5220\u9664\u4e00\u5217\u53ef\u4ee5\u6309\u5982\u4e0b\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5220\u9664\u5217 &#39;B&#39;<\/strong><\/h2>\n<p>df = df.drop(&#39;B&#39;, axis=1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cdrop()\u65b9\u6cd5\u4e2d\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e3a\u5217\u540d\uff0caxis=1\u8868\u793a\u5bf9\u5217\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528columns\u53c2\u6570<\/h3>\n<\/p>\n<p><p>columns\u53c2\u6570\u5141\u8bb8\u540c\u65f6\u5220\u9664\u591a\u4e2a\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5220\u9664\u5217 &#39;B&#39; \u548c &#39;C&#39;<\/strong><\/h2>\n<p>df = df.drop(columns=[&#39;B&#39;, &#39;C&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u76f4\u63a5\u5220\u9664\u5217<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528drop()\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u5220\u9664\u5217\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u6570\u636e\u6846\u3002\u8fd9\u79cd\u65b9\u6cd5\u76f4\u63a5\u5bf9\u6570\u636e\u6846\u64cd\u4f5c\uff0c\u5220\u9664\u6307\u5b9a\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u76f4\u63a5\u5220\u9664\u5217 &#39;B&#39;<\/strong><\/h2>\n<p>del df[&#39;B&#39;]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5220\u9664\u5355\u4e2a\u5217\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528iloc\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>iloc\u65b9\u6cd5\u4e3b\u8981\u7528\u4e8e\u57fa\u4e8e\u4f4d\u7f6e\u7d22\u5f15\u6765\u64cd\u4f5c\u6570\u636e\u6846\u3002\u867d\u7136iloc\u65b9\u6cd5\u4e0d\u76f4\u63a5\u7528\u4e8e\u5220\u9664\u5217\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u91cd\u65b0\u9009\u62e9\u9700\u8981\u7684\u5217\u6765\u95f4\u63a5\u5220\u9664\u67d0\u4e9b\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528iloc\u65b9\u6cd5\u91cd\u65b0\u9009\u62e9\u5217\uff0c\u5220\u9664\u5217 &#39;B&#39;<\/strong><\/h2>\n<p>df = df.iloc[:, [0, 2]]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u91cd\u65b0\u9009\u62e9\u9700\u8981\u7684\u5217\uff0c\u53ef\u4ee5\u95f4\u63a5\u5220\u9664\u4e0d\u9700\u8981\u7684\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5220\u9664\u6570\u636e\u6846\u4e2d\u7684\u5217\uff0c\u5982\u4f7f\u7528pop()\u65b9\u6cd5\u3002pop()\u65b9\u6cd5\u5220\u9664\u6307\u5b9a\u5217\u5e76\u8fd4\u56de\u8be5\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528pop()\u65b9\u6cd5\u5220\u9664\u5217 &#39;B&#39;<\/strong><\/h2>\n<p>b_column = df.pop(&#39;B&#39;)<\/p>\n<p>print(df)<\/p>\n<p>print(b_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pop()\u65b9\u6cd5\u4e0d\u4ec5\u5220\u9664\u4e86\u6307\u5b9a\u5217\uff0c\u8fd8\u5c06\u8be5\u5217\u7684\u6570\u636e\u8fd4\u56de\uff0c\u65b9\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u5220\u9664\u6570\u636e\u6846\u4e00\u5217\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u7684drop()\u65b9\u6cd5\u3001\u76f4\u63a5\u5220\u9664\u5217\u3001\u4f7f\u7528iloc\u65b9\u6cd5\u548c\u5176\u4ed6\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u5220\u9664\u591a\u4f59\u6216\u65e0\u7528\u7684\u5217\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u64cd\u4f5c\uff0c\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664DataFrame\u7684\u4e00\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u5904\u7406\u6570\u636e\u3002\u8981\u5220\u9664DataFrame\u7684\u4e00\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>drop()<\/code>\u51fd\u6570\u3002\u5177\u4f53\u65b9\u6cd5\u4e3a\uff1a<code>df.drop(&#39;\u5217\u540d&#39;, axis=1, inplace=True)<\/code>\u3002\u5176\u4e2d\uff0c<code>df<\/code>\u662f\u4f60\u7684DataFrame\uff0c<code>&#39;\u5217\u540d&#39;<\/code>\u662f\u4f60\u60f3\u8981\u5220\u9664\u7684\u5217\u7684\u540d\u79f0\uff0c<code>axis=1<\/code>\u8868\u793a\u5220\u9664\u5217\uff0c<code>inplace=True<\/code>\u8868\u793a\u76f4\u63a5\u5728\u539fDataFrame\u4e0a\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<p><strong>\u5220\u9664\u5217\u540e\u5982\u4f55\u9a8c\u8bc1\u64cd\u4f5c\u662f\u5426\u6210\u529f\uff1f<\/strong><br \/>\u5728\u5220\u9664\u6307\u5b9a\u5217\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6253\u5370DataFrame\u7684\u5934\u90e8\u6765\u9a8c\u8bc1\u64cd\u4f5c\u662f\u5426\u6210\u529f\u3002\u4f7f\u7528<code>print(df.head())<\/code>\u53ef\u4ee5\u67e5\u770bDataFrame\u7684\u524d\u51e0\u884c\u6570\u636e\uff0c\u786e\u4fdd\u5df2\u5220\u9664\u7684\u5217\u4e0d\u518d\u51fa\u73b0\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>df.columns<\/code>\u53ef\u4ee5\u67e5\u770b\u5f53\u524dDataFrame\u4e2d\u6240\u6709\u5217\u7684\u540d\u79f0\uff0c\u4ee5\u786e\u8ba4\u5220\u9664\u64cd\u4f5c\u662f\u5426\u751f\u6548\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u5220\u9664\u591a\u5217\uff0c\u5e94\u8be5\u600e\u4e48\u505a\uff1f<\/strong><br \/>\u8981\u5220\u9664\u591a\u5217\uff0c\u53ef\u4ee5\u5728<code>drop()<\/code>\u51fd\u6570\u4e2d\u4f20\u9012\u4e00\u4e2a\u5217\u540d\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>df.drop([&#39;\u5217\u540d1&#39;, &#39;\u5217\u540d2&#39;], axis=1, inplace=True)<\/code>\u5c06\u540c\u65f6\u5220\u9664\u540d\u4e3a&#39;\u5217\u540d1&#39;\u548c&#39;\u5217\u540d2&#39;\u7684\u4e24\u5217\u3002\u786e\u4fdd\u5728\u5220\u9664\u591a\u5217\u65f6\uff0c\u5217\u540d\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u540d\u79f0\u90fd\u6b63\u786e\u65e0\u8bef\uff0c\u4ee5\u907f\u514d\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u6570\u636e\u6846\u7684\u4e00\u5217\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u7684drop\u65b9\u6cd5\u3001\u5220\u9664\u5217\u7684\u76f4\u63a5\u64cd\u4f5c\u7b49\uff0c\u5177\u4f53\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1103295,"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\/1103285"}],"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=1103285"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103285\/revisions"}],"predecessor-version":[{"id":1103297,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103285\/revisions\/1103297"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1103295"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1103285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1103285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1103285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}