{"id":1108106,"date":"2025-01-08T16:56:47","date_gmt":"2025-01-08T08:56:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108106.html"},"modified":"2025-01-08T16:56:53","modified_gmt":"2025-01-08T08:56:53","slug":"python%e5%a6%82%e4%bd%95%e5%88%a0%e9%99%a4%e7%9f%a9%e9%98%b5%e7%9a%84%e6%9f%90%e4%b8%80%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108106.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071908\/a9780b9b-7d27-4c8d-9f16-af758dab2e40.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u89e3\u6790\u3001Pandas\u5e93\u7b49\u65b9\u6cd5\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u4f7f\u7528NumPy\u7684delete\u51fd\u6570\u3001\u5217\u8868\u5207\u7247\u3001Pandas\u7684drop\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528NumPy\u5e93\u7684delete\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u5b66\u5e93\uff0c\u7279\u522b\u9002\u5408\u8fdb\u884c\u77e9\u9635\u548c\u6570\u7ec4\u7684\u64cd\u4f5c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u6765\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\uff0c\u5e76\u63d0\u4f9b\u5176\u4ed6\u65b9\u6cd5\u7684\u7b80\u8981\u8bf4\u660e\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528NumPy\u5e93<\/h2>\n<\/p>\n<p><h3>1. NumPy\u7684delete\u51fd\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u4fbf\u7684\u51fd\u6570<code>np.delete<\/code>\uff0c\u53ef\u4ee5\u7528\u6765\u5220\u9664\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u5177\u4f53\u5230\u5220\u9664\u77e9\u9635\u7684\u4e00\u5217\uff0c\u53ef\u4ee5\u6307\u5b9a\u8981\u5220\u9664\u7684\u5217\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]])<\/p>\n<h2><strong>\u5220\u9664\u7b2c\u4e8c\u5217\uff08\u7d22\u5f15\u4e3a1\uff09<\/strong><\/h2>\n<p>new_matrix = np.delete(matrix, 1, axis=1)<\/p>\n<p>print(new_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86NumPy\u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\u3002\u901a\u8fc7<code>np.delete<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u6307\u5b9a\u5220\u9664\u7d22\u5f15\u4e3a1\u7684\u5217\uff0c\u5e76\u901a\u8fc7<code>axis=1<\/code>\u53c2\u6570\u8868\u660e\u6211\u4eec\u8981\u5220\u9664\u7684\u662f\u5217\u800c\u4e0d\u662f\u884c\u3002<\/p>\n<\/p>\n<p><h3>2. NumPy\u7684\u5207\u7247\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>np.delete<\/code>\u51fd\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7NumPy\u7684\u5207\u7247\u64cd\u4f5c\u6765\u9009\u62e9\u9700\u8981\u7684\u5217\uff0c\u4ece\u800c\u95f4\u63a5\u5b9e\u73b0\u5220\u9664\u67d0\u4e00\u5217\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]])<\/p>\n<h2><strong>\u901a\u8fc7\u5207\u7247\u9009\u62e9\u9700\u8981\u7684\u5217<\/strong><\/h2>\n<p>new_matrix = matrix[:, [0, 2]]<\/p>\n<p>print(new_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u9009\u62e9\u4e86\u7d22\u5f15\u4e3a0\u548c2\u7684\u5217\uff0c\u4ece\u800c\u95f4\u63a5\u5220\u9664\u4e86\u7d22\u5f15\u4e3a1\u7684\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u77e9\u9635\u662f\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8868\u793a\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u5220\u9664\u67d0\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3x3\u7684\u77e9\u9635<\/p>\n<p>matrix = [[1, 2, 3],<\/p>\n<p>          [4, 5, 6],<\/p>\n<p>          [7, 8, 9]]<\/p>\n<h2><strong>\u5220\u9664\u7b2c\u4e8c\u5217\uff08\u7d22\u5f15\u4e3a1\uff09<\/strong><\/h2>\n<p>new_matrix = [row[:1] + row[2:] for row in matrix]<\/p>\n<p>print(new_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5217\u8868\u89e3\u6790\u751f\u6210\u4e86\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\uff0c\u5176\u4e2d\u6bcf\u4e00\u884c\u90fd\u6392\u9664\u4e86\u7d22\u5f15\u4e3a1\u7684\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/h2>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u6570\u636e\u6846\uff08DataFrame\uff09\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Pandas\u7684<code>drop<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]], columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;])<\/p>\n<h2><strong>\u5220\u9664\u5217&#39;B&#39;<\/strong><\/h2>\n<p>new_df = df.drop(&#39;B&#39;, axis=1)<\/p>\n<p>print(new_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2aDataFrame\uff0c\u7136\u540e\u901a\u8fc7<code>drop<\/code>\u65b9\u6cd5\u5220\u9664\u4e86\u5217&#39;B&#39;\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001NumPy\u9ad8\u7ea7\u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>1. \u4f7f\u7528\u5e03\u5c14\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u5e03\u5c14\u7d22\u5f15\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u9009\u62e9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5e03\u5c14\u7d22\u5f15\uff0c\u8868\u793a\u9700\u8981\u4fdd\u7559\u7684\u5217<\/strong><\/h2>\n<p>mask = np.array([True, False, True])<\/p>\n<h2><strong>\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u9009\u62e9\u9700\u8981\u7684\u5217<\/strong><\/h2>\n<p>new_matrix = matrix[:, mask]<\/p>\n<p>print(new_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5e03\u5c14\u7d22\u5f15<code>mask<\/code>\uff0c\u8868\u793a\u9700\u8981\u4fdd\u7559\u7684\u5217\uff0c\u7136\u540e\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u9009\u62e9\u77e9\u9635\u4e2d\u7684\u76f8\u5e94\u5217\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u9ad8\u7ea7\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>\u9ad8\u7ea7\u7d22\u5f15\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u6307\u5b9a\u7d22\u5f15\u6570\u7ec4\u6765\u9009\u62e9\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002\u901a\u8fc7\u9ad8\u7ea7\u7d22\u5f15\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\u6570\u7ec4\uff0c\u8868\u793a\u9700\u8981\u4fdd\u7559\u7684\u5217<\/strong><\/h2>\n<p>indices = [0, 2]<\/p>\n<h2><strong>\u4f7f\u7528\u9ad8\u7ea7\u7d22\u5f15\u9009\u62e9\u9700\u8981\u7684\u5217<\/strong><\/h2>\n<p>new_matrix = matrix[:, indices]<\/p>\n<p>print(new_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7d22\u5f15\u6570\u7ec4<code>indices<\/code>\uff0c\u8868\u793a\u9700\u8981\u4fdd\u7559\u7684\u5217\uff0c\u7136\u540e\u901a\u8fc7\u9ad8\u7ea7\u7d22\u5f15\u9009\u62e9\u77e9\u9635\u4e2d\u7684\u76f8\u5e94\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001Pandas\u9ad8\u7ea7\u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>1. \u4f7f\u7528iloc\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>Pandas\u7684<code>iloc<\/code>\u7d22\u5f15\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u4f4d\u7f6e\u7d22\u5f15\u6765\u9009\u62e9\u6570\u636e\u3002\u901a\u8fc7<code>iloc<\/code>\u7d22\u5f15\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]], columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;])<\/p>\n<h2><strong>\u4f7f\u7528iloc\u7d22\u5f15\u9009\u62e9\u9700\u8981\u7684\u5217<\/strong><\/h2>\n<p>new_df = df.iloc[:, [0, 2]]<\/p>\n<p>print(new_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>iloc<\/code>\u7d22\u5f15\u9009\u62e9\u4e86\u7d22\u5f15\u4e3a0\u548c2\u7684\u5217\uff0c\u4ece\u800c\u95f4\u63a5\u5220\u9664\u4e86\u7d22\u5f15\u4e3a1\u7684\u5217\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u5e03\u5c14\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>Pandas\u7684\u5e03\u5c14\u7d22\u5f15\u4e5f\u53ef\u4ee5\u7528\u6765\u9009\u62e9\u6570\u636e\u3002\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]], columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5e03\u5c14\u7d22\u5f15\uff0c\u8868\u793a\u9700\u8981\u4fdd\u7559\u7684\u5217<\/strong><\/h2>\n<p>mask = [True, False, True]<\/p>\n<h2><strong>\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u9009\u62e9\u9700\u8981\u7684\u5217<\/strong><\/h2>\n<p>new_df = df.loc[:, mask]<\/p>\n<p>print(new_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5e03\u5c14\u7d22\u5f15<code>mask<\/code>\uff0c\u8868\u793a\u9700\u8981\u4fdd\u7559\u7684\u5217\uff0c\u7136\u540e\u901a\u8fc7\u5e03\u5c14\u7d22\u5f15\u9009\u62e9DataFrame\u4e2d\u7684\u76f8\u5e94\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u6027\u80fd\u6bd4\u8f83<\/h2>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\u7684\u65b9\u6cd5\u65f6\uff0c\u6027\u80fd\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u8003\u8651\u56e0\u7d20\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u5bf9\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\u7684\u6027\u80fd\u8fdb\u884c\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<p>import time<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a1000x1000\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.random.rand(1000, 1000)<\/p>\n<h2><strong>NumPy\u7684delete\u51fd\u6570<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>new_matrix = np.delete(matrix, 1, axis=1)<\/p>\n<p>print(&quot;NumPy delete time:&quot;, time.time() - start_time)<\/p>\n<h2><strong>NumPy\u7684\u5207\u7247\u64cd\u4f5c<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>new_matrix = matrix[:, [i for i in range(matrix.shape[1]) if i != 1]]<\/p>\n<p>print(&quot;NumPy slicing time:&quot;, time.time() - start_time)<\/p>\n<h2><strong>\u5217\u8868\u89e3\u6790<\/strong><\/h2>\n<p>matrix_list = matrix.tolist()<\/p>\n<p>start_time = time.time()<\/p>\n<p>new_matrix_list = [row[:1] + row[2:] for row in matrix_list]<\/p>\n<p>print(&quot;List comprehension time:&quot;, time.time() - start_time)<\/p>\n<h2><strong>Pandas\u7684drop\u65b9\u6cd5<\/strong><\/h2>\n<p>df = pd.DataFrame(matrix)<\/p>\n<p>start_time = time.time()<\/p>\n<p>new_df = df.drop(1, axis=1)<\/p>\n<p>print(&quot;Pandas drop time:&quot;, time.time() - start_time)<\/p>\n<h2><strong>Pandas\u7684iloc\u7d22\u5f15<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>new_df = df.iloc[:, [i for i in range(df.shape[1]) if i != 1]]<\/p>\n<p>print(&quot;Pandas iloc time:&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u6027\u80fd\u6bd4\u8f83\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a1000&#215;1000\u7684\u77e9\u9635\uff0c\u5e76\u5206\u522b\u4f7f\u7528NumPy\u7684delete\u51fd\u6570\u3001NumPy\u7684\u5207\u7247\u64cd\u4f5c\u3001\u5217\u8868\u89e3\u6790\u3001Pandas\u7684drop\u65b9\u6cd5\u548cPandas\u7684iloc\u7d22\u5f15\u6765\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\uff0c\u6700\u540e\u8f93\u51fa\u6bcf\u79cd\u65b9\u6cd5\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<strong>\u4f7f\u7528NumPy\u5e93\u7684delete\u51fd\u6570\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u77e9\u9635\u3002<strong>\u5217\u8868\u89e3\u6790\u65b9\u6cd5\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u77e9\u9635<\/strong>\uff0c\u5e76\u4e14\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u5e93\u3002<strong>Pandas\u5e93\u7684\u65b9\u6cd5\u9002\u5408\u5904\u7406\u6570\u636e\u6846<\/strong>\uff0c\u7279\u522b\u662f\u5728\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u8fc7\u7a0b\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5904\u7406\u77e9\u9635\u3002\u5982\u679c\u4f60\u60f3\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>np.delete()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>np.delete(array, obj, axis)<\/code>\uff0c\u5176\u4e2d<code>array<\/code>\u662f\u4f60\u7684\u77e9\u9635\uff0c<code>obj<\/code>\u662f\u8981\u5220\u9664\u7684\u5217\u7d22\u5f15\uff0c<code>axis<\/code>\u8bbe\u7f6e\u4e3a1\u8868\u793a\u6cbf\u7740\u5217\u65b9\u5411\u64cd\u4f5c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u77e9\u9635\nmatrix = np.array([[1, 2, 3],\n                   [4, 5, 6],\n                   [7, 8, 9]])\n\n# \u5220\u9664\u7b2c1\u5217\uff08\u7d22\u5f15\u4e3a0\uff09\nnew_matrix = np.delete(matrix, 0, axis=1)\nprint(new_matrix)  # \u8f93\u51fa: [[2 3], [5 6], [8 9]]\n<\/code><\/pre>\n<p><strong>\u5728\u4f7f\u7528Pandas\u65f6\uff0c\u5982\u4f55\u5220\u9664DataFrame\u7684\u67d0\u4e00\u5217\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fPandas\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>drop()<\/code>\u65b9\u6cd5\u6765\u5220\u9664DataFrame\u4e2d\u7684\u67d0\u4e00\u5217\u3002\u4f7f\u7528\u65f6\uff0c\u6307\u5b9a\u8981\u5220\u9664\u7684\u5217\u540d\u4ee5\u53ca<code>axis=1<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame\ndf = pd.DataFrame({&#39;A&#39;: [1, 2, 3],\n                   &#39;B&#39;: [4, 5, 6],\n                   &#39;C&#39;: [7, 8, 9]})\n\n# \u5220\u9664\u5217&#39;B&#39;\ndf_new = df.drop(&#39;B&#39;, axis=1)\nprint(df_new)\n<\/code><\/pre>\n<p><strong>\u5220\u9664\u77e9\u9635\u5217\u540e\uff0c\u5982\u4f55\u68c0\u67e5\u7ed3\u679c\u662f\u5426\u6b63\u786e\uff1f<\/strong><br \/>\u5728\u5220\u9664\u77e9\u9635\u6216DataFrame\u7684\u67d0\u4e00\u5217\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6253\u5370\u8f93\u51fa\u65b0\u77e9\u9635\u6216DataFrame\u6765\u68c0\u67e5\u5220\u9664\u64cd\u4f5c\u662f\u5426\u6210\u529f\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>.shape<\/code>\u5c5e\u6027\u6765\u67e5\u770b\u77e9\u9635\u7684\u7ef4\u5ea6\u53d8\u5316\uff0c\u786e\u4fdd\u884c\u6570\u4e0d\u53d8\u800c\u5217\u6570\u51cf\u5c11\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">print(new_matrix.shape)  # \u8f93\u51fa\u5e94\u4e3a(3, 2)\uff0c\u8868\u793a3\u884c2\u5217\n<\/code><\/pre>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528NumPy\u7684<code>np.delete()<\/code>\u548cPandas\u7684<code>drop()<\/code>\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5c0f\u578b\u77e9\u9635\u65f6\u7279\u522b\u6709\u6548\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\"># \u4f7f\u7528\u5207\u7247\u5220\u9664\u7b2c1\u5217\nnew_matrix_slice = matrix[:, 1:]  # \u9009\u62e9\u4ece\u7b2c2\u5217\u5f00\u59cb\u7684\u6240\u6709\u5217\nprint(new_matrix_slice)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u76f4\u63a5\u901a\u8fc7\u5217\u5207\u7247\u6765\u83b7\u5f97\u65b0\u77e9\u9635\uff0c\u7b80\u5355\u800c\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u5217\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u4f8b\u5982\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u89e3\u6790\u3001Pandas\u5e93\u7b49\u65b9\u6cd5\u3002\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1108119,"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\/1108106"}],"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=1108106"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108106\/revisions"}],"predecessor-version":[{"id":1108124,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108106\/revisions\/1108124"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108119"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}