{"id":1101680,"date":"2025-01-08T15:52:05","date_gmt":"2025-01-08T07:52:05","guid":{"rendered":""},"modified":"2025-01-08T15:52:08","modified_gmt":"2025-01-08T07:52:08","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%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1101680.html","title":{"rendered":"python\u5982\u4f55\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064331\/f62f322f-9a93-4952-8349-63b4dc620cef.webp\" alt=\"python\u5982\u4f55\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u884c\" \/><\/p>\n<p><p> <strong>\u5220\u9664Python\u77e9\u9635\u7684\u67d0\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u64cd\u4f5c\u3001\u4f7f\u7528pandas\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528NumPy\u5e93<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3aNumPy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u5904\u7406\u529f\u80fd\u3002\u8be6\u7ec6\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528NumPy\u5e93\u662f\u5904\u7406\u77e9\u9635\u7684\u6700\u5e38\u7528\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u4e30\u5bcc\u7684\u529f\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u7528\u4e8e\u652f\u6301\u5927\u89c4\u6a21\u7684\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\uff0c\u4ee5\u53ca\u8bb8\u591a\u6570\u5b66\u51fd\u6570\u5e93\u3002\u8981\u4f7f\u7528NumPy\u5e93\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u884c\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5NumPy\u5e93\u5e76\u5bfc\u5165\u5b83\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u77e9\u9635\uff0c\u5e76\u4e14\u6211\u4eec\u60f3\u8981\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u5e76\u5bfc\u5165NumPy\u5e93\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4\u4f5c\u4e3a\u77e9\u9635\u3002<\/li>\n<li>\u4f7f\u7528NumPy\u7684\u5220\u9664\u529f\u80fd\u5220\u9664\u6307\u5b9a\u7684\u884c\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528NumPy\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\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>\u6307\u5b9a\u8981\u5220\u9664\u7684\u884c\u7d22\u5f15<\/strong><\/h2>\n<p>row_to_delete = 1<\/p>\n<h2><strong>\u5220\u9664\u6307\u5b9a\u7684\u884c<\/strong><\/h2>\n<p>new_matrix = np.delete(matrix, row_to_delete, axis=0)<\/p>\n<p>print(&quot;\u539f\u59cb\u77e9\u9635\uff1a\\n&quot;, matrix)<\/p>\n<p>print(&quot;\u5220\u9664\u7b2c&quot;, row_to_delete, &quot;\u884c\u540e\u7684\u77e9\u9635\uff1a\\n&quot;, new_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>np.delete<\/code>\u51fd\u6570\u7528\u4e8e\u5220\u9664\u77e9\u9635\u7684\u6307\u5b9a\u884c\u3002<code>axis=0<\/code>\u53c2\u6570\u6307\u5b9a\u5220\u9664\u7684\u662f\u884c\uff0c\u800c\u4e0d\u662f\u5217\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u4eec\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u57fa\u672c\u64cd\u4f5c\uff0cNumPy\u5e93\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u65b9\u4fbf\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u66f4\u591a\u5173\u4e8eNumPy\u5220\u9664\u884c\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528\u5e03\u5c14\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u60f3\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u5220\u9664\u77e9\u9635\u7684\u884c\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u57fa\u4e8e\u884c\u7d22\u5f15\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u5220\u9664\u77e9\u9635\u4e2d\u6240\u6709\u5143\u7d20\u503c\u5927\u4e8e5\u7684\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u77e9\u9635<\/p>\n<p>matrix = np.array([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]])<\/p>\n<h2><strong>\u6761\u4ef6\uff1a\u5220\u9664\u6240\u6709\u5143\u7d20\u503c\u5927\u4e8e5\u7684\u884c<\/strong><\/h2>\n<p>condition = np.all(matrix &lt;= 5, axis=1)<\/p>\n<h2><strong>\u5220\u9664\u6ee1\u8db3\u6761\u4ef6\u7684\u884c<\/strong><\/h2>\n<p>new_matrix = matrix[condition]<\/p>\n<p>print(&quot;\u539f\u59cb\u77e9\u9635\uff1a\\n&quot;, matrix)<\/p>\n<p>print(&quot;\u5220\u9664\u6240\u6709\u5143\u7d20\u503c\u5927\u4e8e5\u884c\u540e\u7684\u77e9\u9635\uff1a\\n&quot;, new_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>condition<\/code>\u662f\u4e00\u4e2a\u5e03\u5c14\u6570\u7ec4\uff0c\u5b83\u6307\u793a\u6bcf\u4e00\u884c\u662f\u5426\u6ee1\u8db3\u6761\u4ef6\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u9009\u62e9\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528\u5207\u7247\u548c\u8fde\u63a5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5207\u7247\u548c\u8fde\u63a5\u6765\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u884c\u3002\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u4e0d\u5982<code>np.delete<\/code>\u51fd\u6570\u76f4\u63a5\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5220\u9664\u77e9\u9635\u7684\u7b2c\u4e8c\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u77e9\u9635<\/p>\n<p>matrix = np.array([[1, 2, 3],<\/p>\n<p>                   [4, 5, 6],<\/p>\n<p>                   [7, 8, 9]])<\/p>\n<h2><strong>\u6307\u5b9a\u8981\u5220\u9664\u7684\u884c\u7d22\u5f15<\/strong><\/h2>\n<p>row_to_delete = 1<\/p>\n<h2><strong>\u4f7f\u7528\u5207\u7247\u548c\u8fde\u63a5\u5220\u9664\u6307\u5b9a\u7684\u884c<\/strong><\/h2>\n<p>new_matrix = np.vstack((matrix[:row_to_delete], matrix[row_to_delete+1:]))<\/p>\n<p>print(&quot;\u539f\u59cb\u77e9\u9635\uff1a\\n&quot;, matrix)<\/p>\n<p>print(&quot;\u5220\u9664\u7b2c&quot;, row_to_delete, &quot;\u884c\u540e\u7684\u77e9\u9635\uff1a\\n&quot;, new_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6570\u7ec4\u5207\u7247\u5c06\u77e9\u9635\u5206\u6210\u4e24\u90e8\u5206\uff0c\u7136\u540e\u4f7f\u7528<code>np.vstack<\/code>\u51fd\u6570\u5c06\u5b83\u4eec\u5782\u76f4\u8fde\u63a5\u8d77\u6765\uff0c\u4ece\u800c\u5220\u9664\u4e86\u6307\u5b9a\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u4e0d\u60f3\u4f7f\u7528NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u7eafPython\u5217\u8868\u64cd\u4f5c\u6765\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u884c\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4e0d\u5982NumPy\u9ad8\u6548\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4ecd\u7136\u662f\u4e00\u4e2a\u53ef\u884c\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684Python\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u4ee5\u7b80\u6d01\u7684\u65b9\u5f0f\u521b\u5efa\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5220\u9664\u77e9\u9635\u7684\u7b2c\u4e8c\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff08\u5217\u8868\u7684\u5217\u8868\uff09<\/p>\n<p>matrix = [[1, 2, 3],<\/p>\n<p>          [4, 5, 6],<\/p>\n<p>          [7, 8, 9]]<\/p>\n<h2><strong>\u6307\u5b9a\u8981\u5220\u9664\u7684\u884c\u7d22\u5f15<\/strong><\/h2>\n<p>row_to_delete = 1<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5220\u9664\u6307\u5b9a\u7684\u884c<\/strong><\/h2>\n<p>new_matrix = [row for i, row in enumerate(matrix) if i != row_to_delete]<\/p>\n<p>print(&quot;\u539f\u59cb\u77e9\u9635\uff1a&quot;)<\/p>\n<p>for row in matrix:<\/p>\n<p>    print(row)<\/p>\n<p>print(&quot;\u5220\u9664\u7b2c&quot;, row_to_delete, &quot;\u884c\u540e\u7684\u77e9\u9635\uff1a&quot;)<\/p>\n<p>for row in new_matrix:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5176\u4e2d\u4e0d\u5305\u62ec\u8981\u5220\u9664\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528\u5207\u7247\u548c\u62fc\u63a5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u5207\u7247\u548c\u62fc\u63a5\u6765\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff08\u5217\u8868\u7684\u5217\u8868\uff09<\/p>\n<p>matrix = [[1, 2, 3],<\/p>\n<p>          [4, 5, 6],<\/p>\n<p>          [7, 8, 9]]<\/p>\n<h2><strong>\u6307\u5b9a\u8981\u5220\u9664\u7684\u884c\u7d22\u5f15<\/strong><\/h2>\n<p>row_to_delete = 1<\/p>\n<h2><strong>\u4f7f\u7528\u5207\u7247\u548c\u62fc\u63a5\u5220\u9664\u6307\u5b9a\u7684\u884c<\/strong><\/h2>\n<p>new_matrix = matrix[:row_to_delete] + matrix[row_to_delete+1:]<\/p>\n<p>print(&quot;\u539f\u59cb\u77e9\u9635\uff1a&quot;)<\/p>\n<p>for row in matrix:<\/p>\n<p>    print(row)<\/p>\n<p>print(&quot;\u5220\u9664\u7b2c&quot;, row_to_delete, &quot;\u884c\u540e\u7684\u77e9\u9635\uff1a&quot;)<\/p>\n<p>for row in new_matrix:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u5207\u7247\u5c06\u77e9\u9635\u5206\u6210\u4e24\u90e8\u5206\uff0c\u7136\u540e\u4f7f\u7528\u62fc\u63a5\u64cd\u4f5c\u5c06\u5b83\u4eec\u8fde\u63a5\u8d77\u6765\uff0c\u4ece\u800c\u5220\u9664\u4e86\u6307\u5b9a\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u867d\u7136\u5b83\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u6570\u636e\u5e27\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u77e9\u9635\u3002\u4f7f\u7528pandas\u5e93\u5220\u9664\u77e9\u9635\u7684\u67d0\u4e00\u884c\u4e5f\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528<code>drop<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p>pandas\u5e93\u7684<code>drop<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5220\u9664\u6570\u636e\u5e27\u4e2d\u7684\u67d0\u4e00\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff08\u6570\u636e\u5e27\uff09<\/strong><\/h2>\n<p>matrix = pd.DataFrame([[1, 2, 3],<\/p>\n<p>                       [4, 5, 6],<\/p>\n<p>                       [7, 8, 9]])<\/p>\n<h2><strong>\u6307\u5b9a\u8981\u5220\u9664\u7684\u884c\u7d22\u5f15<\/strong><\/h2>\n<p>row_to_delete = 1<\/p>\n<h2><strong>\u4f7f\u7528drop\u51fd\u6570\u5220\u9664\u6307\u5b9a\u7684\u884c<\/strong><\/h2>\n<p>new_matrix = matrix.drop(row_to_delete)<\/p>\n<p>print(&quot;\u539f\u59cb\u77e9\u9635\uff1a\\n&quot;, matrix)<\/p>\n<p>print(&quot;\u5220\u9664\u7b2c&quot;, row_to_delete, &quot;\u884c\u540e\u7684\u77e9\u9635\uff1a\\n&quot;, new_matrix)<\/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\u4e2apandas\u6570\u636e\u5e27\uff0c\u7136\u540e\u4f7f\u7528<code>drop<\/code>\u51fd\u6570\u5220\u9664\u4e86\u6307\u5b9a\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u91cd\u7f6e\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>\u5220\u9664\u884c\u540e\uff0c\u53ef\u80fd\u9700\u8981\u91cd\u7f6e\u6570\u636e\u5e27\u7684\u7d22\u5f15\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff08\u6570\u636e\u5e27\uff09<\/p>\n<p>matrix = pd.DataFrame([[1, 2, 3],<\/p>\n<p>                       [4, 5, 6],<\/p>\n<p>                       [7, 8, 9]])<\/p>\n<h2><strong>\u6307\u5b9a\u8981\u5220\u9664\u7684\u884c\u7d22\u5f15<\/strong><\/h2>\n<p>row_to_delete = 1<\/p>\n<h2><strong>\u4f7f\u7528drop\u51fd\u6570\u5220\u9664\u6307\u5b9a\u7684\u884c<\/strong><\/h2>\n<p>new_matrix = matrix.drop(row_to_delete).reset_index(drop=True)<\/p>\n<p>print(&quot;\u539f\u59cb\u77e9\u9635\uff1a\\n&quot;, matrix)<\/p>\n<p>print(&quot;\u5220\u9664\u7b2c&quot;, row_to_delete, &quot;\u884c\u540e\u7684\u77e9\u9635\uff1a\\n&quot;, new_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u5220\u9664\u884c\u540e\u4f7f\u7528<code>reset_index<\/code>\u51fd\u6570\u91cd\u7f6e\u4e86\u6570\u636e\u5e27\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5220\u9664Python\u77e9\u9635\u7684\u67d0\u4e00\u884c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<strong>\u4f7f\u7528NumPy\u5e93<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u4e30\u5bcc\u7684\u529f\u80fd\u3002<strong>\u4f7f\u7528\u5217\u8868\u64cd\u4f5c<\/strong>\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u77e9\u9635\u548c\u7b80\u5355\u64cd\u4f5c\uff0c<strong>\u4f7f\u7528pandas\u5e93<\/strong>\u5219\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u5176\u539f\u7406\u548c\u9002\u7528\u573a\u666f\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u77e9\u9635\u884c\u5220\u9664\u7684\u5404\u79cd\u65b9\u6cd5\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\u7279\u5b9a\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5904\u7406\u77e9\u9635\u64cd\u4f5c\u3002\u8981\u5220\u9664\u7279\u5b9a\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.delete()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u77e9\u9635\u3001\u8981\u5220\u9664\u7684\u884c\u7d22\u5f15\u548c\u8f74\u53c2\u6570\uff0c\u8f74\u53c2\u6570\u8bbe\u7f6e\u4e3a0\u8868\u793a\u6309\u884c\u5220\u9664\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nmatrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\nnew_matrix = np.delete(matrix, 1, axis=0)  # \u5220\u9664\u7b2c\u4e8c\u884c\nprint(new_matrix)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u5c06\u662f\uff1a  <\/p>\n<pre><code>[[1 2 3]\n [7 8 9]]\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5220\u9664\u77e9\u9635\u7684\u884c\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u5220\u9664\u77e9\u9635\u7684\u884c\u3002\u901a\u8fc7\u904d\u5386\u77e9\u9635\u5e76\u9009\u62e9\u4e0d\u9700\u8981\u5220\u9664\u7684\u884c\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nrow_to_delete = 1\nnew_matrix = [row for i, row in enumerate(matrix) if i != row_to_delete]\nprint(new_matrix)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\uff1a  <\/p>\n<pre><code>[[1, 2, 3], [7, 8, 9]]\n<\/code><\/pre>\n<p><strong>\u5220\u9664\u77e9\u9635\u884c\u540e\uff0c\u5982\u4f55\u5904\u7406\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u7d22\u5f15\uff1f<\/strong><br \/>\u5728\u5220\u9664\u77e9\u9635\u7684\u884c\u540e\uff0c\u786e\u4fdd\u540e\u7eed\u64cd\u4f5c\u80fd\u591f\u6b63\u786e\u5904\u7406\u6570\u636e\u5b8c\u6574\u6027\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Pandas\u5e93\u6765\u5904\u7406\u6570\u636e\u6846\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u6301\u7d22\u5f15\u7684\u5b8c\u6574\u6027\u3002\u4f7f\u7528<code>drop()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5220\u9664\u884c\u5e76\u4fdd\u6301\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u5b8c\u6574\u6027\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndf = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\ndf.drop(index=1, inplace=True)  # \u5220\u9664\u7b2c\u4e8c\u884c\nprint(df)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\u4f1a\u4fdd\u7559\u7d22\u5f15\u7684\u5b8c\u6574\u6027\uff0c\u4fbf\u4e8e\u540e\u7eed\u6570\u636e\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5220\u9664Python\u77e9\u9635\u7684\u67d0\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u64cd\u4f5c\u3001\u4f7f\u7528pandas\u5e93\u3002 \u5176\u4e2d\uff0c\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1101689,"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\/1101680"}],"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=1101680"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101680\/revisions"}],"predecessor-version":[{"id":1101690,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101680\/revisions\/1101690"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101689"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}