{"id":1105204,"date":"2025-01-08T16:28:44","date_gmt":"2025-01-08T08:28:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1105204.html"},"modified":"2025-01-08T16:28:46","modified_gmt":"2025-01-08T08:28:46","slug":"python%e5%a6%82%e4%bd%95%e6%94%b9%e5%8f%98%e7%9f%a9%e9%98%b5%e4%b8%ad%e6%8c%87%e5%ae%9a%e5%85%83%e7%b4%a0%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1105204.html","title":{"rendered":"python\u5982\u4f55\u6539\u53d8\u77e9\u9635\u4e2d\u6307\u5b9a\u5143\u7d20\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070211\/bfb55fd5-6a1b-446b-8beb-4730a1c80b6b.webp\" alt=\"python\u5982\u4f55\u6539\u53d8\u77e9\u9635\u4e2d\u6307\u5b9a\u5143\u7d20\u7684\u503c\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u6539\u53d8\u77e9\u9635\u4e2d\u6307\u5b9a\u5143\u7d20\u7684\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u3002<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u5d4c\u5957\u3001\u4ee5\u53caPandas\u5e93<\/strong>\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u5217\u8868\u5d4c\u5957\u662fPython\u5185\u7f6e\u7684\u7b80\u5355\u77e9\u9635\u8868\u793a\u65b9\u6cd5\uff0c\u800cPandas\u5e93\u5219\u9002\u7528\u4e8e\u5904\u7406\u5305\u542b\u6807\u7b7e\u7684\u6570\u636e\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528NumPy\u5e93\u3002<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u4e13\u4e3a\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u800c\u8bbe\u8ba1\u3002\u901a\u8fc7NumPy\uff0c\u4f60\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u3001\u8bbf\u95ee\u548c\u4fee\u6539\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u901a\u8fc7NumPy\u5e93\u6765\u6539\u53d8\u77e9\u9635\u4e2d\u6307\u5b9a\u5143\u7d20\u7684\u503c\uff1a<\/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>\u6539\u53d8\u77e9\u9635\u4e2d (1, 1) \u4f4d\u7f6e\u7684\u5143\u7d20\u503c\uff0c\u5c06\u5176\u6539\u4e3a99<\/strong><\/h2>\n<p>matrix[1, 1] = 99<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>np.array<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff0c\u5e76\u901a\u8fc7\u7d22\u5f15<code>[1, 1]<\/code>\u8bbf\u95ee\u77e9\u9635\u4e2d\u7684\u6307\u5b9a\u5143\u7d20\uff0c\u5c06\u5176\u503c\u6539\u4e3a\u4e8699\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u4e24\u79cd\u65b9\u6cd5\uff0c\u5e76\u5bf9\u6bd4\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868<\/h2>\n<\/p>\n<p><h3>1. \u521b\u5efa\u77e9\u9635\u548c\u4fee\u6539\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u8868\u793a\u77e9\u9635\uff0c\u5e76\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3x3\u7684\u5d4c\u5957\u5217\u8868\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>\u6539\u53d8\u77e9\u9635\u4e2d (1, 1) \u4f4d\u7f6e\u7684\u5143\u7d20\u503c\uff0c\u5c06\u5176\u6539\u4e3a99<\/strong><\/h2>\n<p>matrix[1][1] = 99<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\uff0c\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u7684\u77e9\u9635\u64cd\u4f5c\u3002\u7f3a\u70b9\u662f\u64cd\u4f5c\u6548\u7387\u8f83\u4f4e\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u65f6\uff0c\u6027\u80fd\u4e0d\u5982\u4e13\u7528\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/h2>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u548c\u5bfc\u5165NumPy<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5NumPy\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165NumPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u521b\u5efa\u77e9\u9635\u548c\u4fee\u6539\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u521b\u5efa\u77e9\u9635\u975e\u5e38\u7b80\u5355\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>np.array<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff0c\u5e76\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u77e9\u9635\u4e2d\u7684\u5143\u7d20\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>\u6539\u53d8\u77e9\u9635\u4e2d (1, 1) \u4f4d\u7f6e\u7684\u5143\u7d20\u503c\uff0c\u5c06\u5176\u6539\u4e3a99<\/strong><\/h2>\n<p>matrix[1, 1] = 99<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u7684\u4f18\u70b9\u662f\u64cd\u4f5c\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002\u7f3a\u70b9\u662f\u9700\u8981\u989d\u5916\u5b89\u88c5NumPy\u5e93\uff0c\u5e76\u5b66\u4e60\u5176\u7279\u6709\u7684\u8bed\u6cd5\u548c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/h2>\n<\/p>\n<p><h3>1. \u5b89\u88c5\u548c\u5bfc\u5165Pandas<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Pandas\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u521b\u5efa\u77e9\u9635\u548c\u4fee\u6539\u5143\u7d20<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u4e2d\u7684DataFrame\u5bf9\u8c61\u53ef\u4ee5\u7528\u6765\u8868\u793a\u77e9\u9635\uff0c\u901a\u8fc7\u884c\u5217\u7d22\u5f15\u8bbf\u95ee\u548c\u4fee\u6539\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684DataFrame\u77e9\u9635<\/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>\u6539\u53d8\u77e9\u9635\u4e2d (1, 1) \u4f4d\u7f6e\u7684\u5143\u7d20\u503c\uff0c\u5c06\u5176\u6539\u4e3a99<\/strong><\/h2>\n<p>matrix.iloc[1, 1] = 99<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u7684\u4f18\u70b9\u662f\u64cd\u4f5c\u7b80\u4fbf\uff0c\u9002\u7528\u4e8e\u5e26\u6709\u6807\u7b7e\u7684\u6570\u636e\u5904\u7406\u3002\u7f3a\u70b9\u662f\u9700\u8981\u989d\u5916\u5b89\u88c5Pandas\u5e93\uff0c\u5e76\u5b66\u4e60\u5176\u7279\u6709\u7684\u8bed\u6cd5\u548c\u7528\u6cd5\u3002Pandas\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u6027\u80fd\u8f83NumPy\u7a0d\u900a\u4e00\u7b79\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u6539\u53d8\u77e9\u9635\u4e2d\u6307\u5b9a\u5143\u7d20\u7684\u503c\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5217\u8868\u3001NumPy\u5e93\u3001Pandas\u5e93\u4e09\u79cd\u4e3b\u8981\u65b9\u6cd5\u5b9e\u73b0<\/strong>\u3002\u5d4c\u5957\u5217\u8868\u9002\u7528\u4e8e\u7b80\u5355\u5c0f\u89c4\u6a21\u7684\u77e9\u9635\u64cd\u4f5c\uff0cNumPy\u5e93\u9002\u7528\u4e8e\u9ad8\u6548\u7684\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\uff0cPandas\u5e93\u5219\u9002\u7528\u4e8e\u5e26\u6709\u6807\u7b7e\u7684\u6570\u636e\u5904\u7406\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u5728Python\u4e2d\u6539\u53d8\u77e9\u9635\u4e2d\u6307\u5b9a\u5143\u7d20\u7684\u503c\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u5d4c\u5957\u5217\u8868\u64cd\u4f5c\uff0c\u8fd8\u662f\u9ad8\u6548\u7684NumPy\u5e93\u548cPandas\u5e93\u64cd\u4f5c\uff0c\u90fd\u80fd\u591f\u5e2e\u52a9\u4f60\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u5728\u6570\u636e\u5904\u7406\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\u53d6\u5f97\u66f4\u5927\u7684\u8fdb\u6b65\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbf\u95ee\u548c\u4fee\u6539\u77e9\u9635\u7684\u7279\u5b9a\u5143\u7d20\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5904\u7406\u77e9\u9635\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u3002\u901a\u8fc7<code>import numpy as np<\/code>\u5bfc\u5165\u5e93\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u7279\u5b9a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a2D\u6570\u7ec4<code>matrix<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matrix[row_index, column_index]<\/code>\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u6216\u4fee\u6539\u5143\u7d20\u7684\u503c\u3002\u5c06\u8be5\u4f4d\u7f6e\u7684\u503c\u8d4b\u4e88\u65b0\u7684\u6570\u503c\u5373\u53ef\u5b9e\u73b0\u4fee\u6539\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u6539\u53d8\u77e9\u9635\u4e2d\u5143\u7d20\u7684\u6548\u7387\u5982\u4f55\uff1f<\/strong><br \/>\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u77e9\u9635\u8fd0\u7b97\u3002NumPy\u5bf9\u5e95\u5c42\u7684\u4f18\u5316\u4f7f\u5f97\u77e9\u9635\u64cd\u4f5c\u901f\u5ea6\u8fdc\u8d85\u7eafPython\u7684\u5217\u8868\u64cd\u4f5c\u3002\u5f53\u4f60\u9700\u8981\u8fdb\u884c\u5927\u91cf\u7684\u77e9\u9635\u8fd0\u7b97\u6216\u4fee\u6539\u65f6\uff0cNumPy\u7684\u5411\u91cf\u5316\u64cd\u4f5c\u80fd\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u907f\u514d\u4f7f\u7528\u5faa\u73af\u76f4\u63a5\u4fee\u6539\u5143\u7d20\uff0c\u901a\u8fc7\u5207\u7247\u6216\u5e03\u5c14\u7d22\u5f15\u6765\u5904\u7406\u4f1a\u66f4\u6709\u6548\u7387\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u7528Python\u5982\u4f55\u6761\u4ef6\u6027\u5730\u4fee\u6539\u77e9\u9635\u5143\u7d20\uff1f<\/strong><br \/>Python\u5141\u8bb8\u4f7f\u7528\u5e03\u5c14\u7d22\u5f15\u6765\u6761\u4ef6\u6027\u4fee\u6539\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u6240\u6709\u5c0f\u4e8e0\u7684\u503c\u66ff\u6362\u4e3a0\uff0c\u53ef\u4ee5\u4f7f\u7528<code>matrix[matrix &lt; 0] = 0<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u8fd8\u80fd\u6709\u6548\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\uff0c\u907f\u514d\u624b\u52a8\u8fed\u4ee3\u6bcf\u4e2a\u5143\u7d20\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u53ef\u8bfb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6539\u53d8\u77e9\u9635\u4e2d\u6307\u5b9a\u5143\u7d20\u7684\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u5217\u8868\u5d4c\u5957\u3001\u4ee5\u53ca [&hellip;]","protected":false},"author":3,"featured_media":1105209,"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\/1105204"}],"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=1105204"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105204\/revisions"}],"predecessor-version":[{"id":1105210,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1105204\/revisions\/1105210"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1105209"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1105204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1105204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1105204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}