{"id":986128,"date":"2024-12-27T07:42:18","date_gmt":"2024-12-26T23:42:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/986128.html"},"modified":"2024-12-27T07:42:21","modified_gmt":"2024-12-26T23:42:21","slug":"python-%e5%a6%82%e4%bd%95%e5%8f%96%e5%ad%90%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/986128.html","title":{"rendered":"python \u5982\u4f55\u53d6\u5b50\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062843\/4d3667a2-4f75-4874-966d-c7e9ddd6d23a.webp\" alt=\"python \u5982\u4f55\u53d6\u5b50\u77e9\u9635\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u63d0\u53d6\u5b50\u77e9\u9635\uff0c\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u89e3\u6790\u548cPandas\u5e93\u7b49\u3002NumPy\u5e93\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3001\u6548\u7387\u9ad8\u3001\u6613\u4e8e\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u63d0\u53d6\u5b50\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u652f\u6301\u5927\u591a\u6570\u5b66\u5e93\u64cd\u4f5c\u7684\u6570\u7ec4\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528NumPy\u63d0\u53d6\u5b50\u77e9\u9635\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\u3002\u5728NumPy\u4e2d\uff0c\u77e9\u9635\u901a\u5e38\u8868\u793a\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\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<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u63d0\u53d6\u5b50\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u7d22\u5f15\u6765\u63d0\u53d6\u5b50\u77e9\u9635\u3002NumPy\u5141\u8bb8\u4f7f\u7528\u5207\u7247\u8bed\u6cd5\u6765\u63d0\u53d6\u77e9\u9635\u7684\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u5b50\u77e9\u9635<\/p>\n<p>sub_matrix = matrix[0:2, 1:3]<\/p>\n<p>print(sub_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u63d0\u53d6\u77e9\u9635\u7684\u7b2c\u4e00\u884c\u548c\u7b2c\u4e8c\u884c\uff0c\u4ee5\u53ca\u7b2c\u4e8c\u5217\u548c\u7b2c\u4e09\u5217\uff0c\u5f62\u6210\u4e00\u4e2a2&#215;2\u7684\u5b50\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u5e03\u5c14\u7d22\u5f15<\/h4>\n<\/p>\n<p><p>NumPy\u8fd8\u652f\u6301\u5e03\u5c14\u7d22\u5f15\uff0c\u5141\u8bb8\u6211\u4eec\u57fa\u4e8e\u6761\u4ef6\u63d0\u53d6\u5b50\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u5927\u4e8e5\u7684\u5143\u7d20\u7ec4\u6210\u7684\u5b50\u77e9\u9635<\/p>\n<p>bool_index = matrix &gt; 5<\/p>\n<p>sub_matrix_bool = matrix[bool_index]<\/p>\n<p>print(sub_matrix_bool)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5c06\u63d0\u53d6\u6240\u6709\u5927\u4e8e5\u7684\u5143\u7d20\uff0c\u5e76\u5c06\u5176\u5c55\u5e73\u6210\u4e00\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662fPython\u7684\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u63d0\u53d6\u5b50\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3x3\u77e9\u9635<\/p>\n<p>matrix = [[1, 2, 3],<\/p>\n<p>          [4, 5, 6],<\/p>\n<p>          [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u63d0\u53d6\u5b50\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5217\u8868\u89e3\u6790\u63d0\u53d6\u5b50\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63d0\u53d6\u5b50\u77e9\u9635<\/p>\n<p>sub_matrix = [row[1:3] for row in matrix[0:2]]<\/p>\n<p>print(sub_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u4e0eNumPy\u7684\u5207\u7247\u8bed\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u9002\u7528\u4e8e\u7eafPython\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\u3002\u867d\u7136Pandas\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u77e9\u9635\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efaDataFrame<\/h4>\n<\/p>\n<p><p>\u5728Pandas\u4e2d\uff0c\u77e9\u9635\u53ef\u4ee5\u8868\u793a\u4e3aDataFrame\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684DataFrame<\/strong><\/h2>\n<p>matrix = pd.DataFrame([[1, 2, 3],<\/p>\n<p>                       [4, 5, 6],<\/p>\n<p>                       [7, 8, 9]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u63d0\u53d6\u5b50\u77e9\u9635<\/h4>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u63d0\u53d6\u5b50\u77e9\u9635\uff0c\u5305\u62ec\u4f7f\u7528<code>.iloc<\/code>\u548c<code>.loc<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528iloc\u63d0\u53d6\u5b50\u77e9\u9635<\/p>\n<p>sub_matrix = matrix.iloc[0:2, 1:3]<\/p>\n<p>print(sub_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>.iloc<\/code>\u57fa\u4e8e\u4f4d\u7f6e\u7d22\u5f15\u63d0\u53d6\u6570\u636e\uff0c\u800c<code>.loc<\/code>\u57fa\u4e8e\u6807\u7b7e\u7d22\u5f15\u63d0\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001NumPy\u4e0ePandas\u7684\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0cNumPy\u901a\u5e38\u6bd4Pandas\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3aNumPy\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u7684\u5e93\uff0c\u800cPandas\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u8fd9\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u7136\u800c\uff0cPandas\u5728\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u65f6\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u62bd\u8c61\uff0c\u53ef\u80fd\u66f4\u5bb9\u6613\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u6027\u80fd\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u5927\u89c4\u6a21\u77e9\u9635\u6765\u6bd4\u8f83\u4e24\u8005\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>NumPy\u6027\u80fd\u6d4b\u8bd5<\/strong><\/h2>\n<p>large_matrix = np.random.rand(1000, 1000)<\/p>\n<p>start_time = time.time()<\/p>\n<p>sub_matrix_np = large_matrix[100:200, 200:300]<\/p>\n<p>print(&quot;NumPy Time:&quot;, time.time() - start_time)<\/p>\n<h2><strong>Pandas\u6027\u80fd\u6d4b\u8bd5<\/strong><\/h2>\n<p>large_df = pd.DataFrame(np.random.rand(1000, 1000))<\/p>\n<p>start_time = time.time()<\/p>\n<p>sub_matrix_pd = large_df.iloc[100:200, 200:300]<\/p>\n<p>print(&quot;Pandas Time:&quot;, time.time() - start_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u6d4b\u8bd5\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0NumPy\u901a\u5e38\u66f4\u5feb\uff0c\u800cPandas\u5728\u529f\u80fd\u548c\u6613\u7528\u6027\u4e0a\u66f4\u6709\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u63d0\u53d6\u5b50\u77e9\u9635\u5728\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4ece\u5927\u578b\u6570\u636e\u96c6\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u884c\u548c\u5217\u8fdb\u884c\u5206\u6790\u3002\u4f7f\u7528NumPy\u6216Pandas\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h4>2. \u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u901a\u5e38\u8868\u793a\u4e3a\u4e8c\u7ef4\u77e9\u9635\uff0c\u63d0\u53d6\u5b50\u77e9\u9635\u53ef\u4ee5\u7528\u4e8e\u88c1\u526a\u56fe\u50cf\u3001\u7279\u5f81\u63d0\u53d6\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>3. <a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/h4>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u7279\u5f81\u9009\u62e9\u662f\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\u3002\u63d0\u53d6\u7279\u5b9a\u7279\u5f81\u7684\u5b50\u77e9\u9635\u53ef\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u63d0\u53d6\u5b50\u77e9\u9635\u662f\u4e00\u4e2a\u57fa\u672c\u4e14\u91cd\u8981\u7684\u64cd\u4f5c\u3002<strong>NumPy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u9002\u5408\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u5904\u7406\u3002\u5217\u8868\u89e3\u6790\u5219\u9002\u7528\u4e8e\u7b80\u5355\u7684Python\u6570\u636e\u7ed3\u6784\u5904\u7406\uff0c\u800cPandas\u5219\u5728\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u65f6\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u62bd\u8c61\u3002<\/strong>\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u63d0\u53d6\u5b50\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u63d0\u53d6\u5b50\u77e9\u9635\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5NumPy\u5e76\u5bfc\u5165\u5b83\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u884c\u548c\u5217\u7684\u8303\u56f4\u6765\u83b7\u53d6\u5b50\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u7b26\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<code>matrix<\/code>\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>sub_matrix = matrix[start_row:end_row, start_col:end_col]<\/code>\u6765\u63d0\u53d6\u5b50\u77e9\u9635\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u63d0\u53d6\u5b50\u77e9\u9635\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4f7f\u7528NumPy\u662f\u63d0\u53d6\u5b50\u77e9\u9635\u7684\u6700\u4f73\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u9ad8\u6548\uff0c\u8fd8\u652f\u6301\u591a\u79cd\u6570\u7ec4\u64cd\u4f5c\u3002NumPy\u7684\u5207\u7247\u529f\u80fd\u5141\u8bb8\u7528\u6237\u5feb\u901f\u9009\u62e9\u7279\u5b9a\u884c\u548c\u5217\uff0c\u521b\u5efa\u5b50\u77e9\u9635\u3002\u6b64\u5916\uff0cNumPy\u7684\u6570\u7ec4\u8fd0\u7b97\u80fd\u529b\u4f7f\u5f97\u540e\u7eed\u7684\u6570\u5b66\u8ba1\u7b97\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u4e0d\u89c4\u5219\u77e9\u9635\u7684\u5b50\u77e9\u9635\u63d0\u53d6\uff1f<\/strong><br \/>\u5bf9\u4e8e\u4e0d\u89c4\u5219\u77e9\u9635\uff0c\u5373\u4e0d\u540c\u957f\u5ea6\u7684\u884c\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u5176\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\u6216Pandas DataFrame\u3002\u4f7f\u7528Pandas\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>iloc<\/code>\u65b9\u6cd5\u6765\u9009\u62e9\u7279\u5b9a\u7684\u884c\u548c\u5217\u3002\u4f8b\u5982\uff0c<code>sub_matrix = df.iloc[start_row:end_row, start_col:end_col]<\/code>\u53ef\u4ee5\u8f7b\u677e\u63d0\u53d6\u5b50\u77e9\u9635\uff0c\u540c\u65f6\u4fdd\u7559\u6570\u636e\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u63d0\u53d6\u5b50\u77e9\u9635\uff0c\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u89e3\u6790\u548cPandas\u5e93\u7b49\u3002NumPy\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":986133,"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\/986128"}],"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=986128"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986128\/revisions"}],"predecessor-version":[{"id":986135,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986128\/revisions\/986135"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/986133"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=986128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=986128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=986128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}