{"id":1067080,"date":"2024-12-31T16:31:49","date_gmt":"2024-12-31T08:31:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1067080.html"},"modified":"2024-12-31T16:31:51","modified_gmt":"2024-12-31T08:31:51","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%88%9b%e9%80%a0%e4%b8%80%e4%b8%aa%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1067080.html","title":{"rendered":"\u5982\u4f55\u7528python\u521b\u9020\u4e00\u4e2a\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/eeca3960-af40-462a-a7e7-8189f65baf5d.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u521b\u9020\u4e00\u4e2a\u77e9\u9635\" \/><\/p>\n<p><p> <strong>\u7528Python\u521b\u9020\u4e00\u4e2a\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u57fa\u7840\u5217\u8868\u3001NumPy\u5e93\u548cPandas\u5e93\u7b49\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4efb\u52a1\uff0cNumPy\u5e93\u662f\u6700\u5e38\u7528\u7684\u5de5\u5177\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u7528Python\u521b\u9020\u4e00\u4e2a\u77e9\u9635\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u548c\u5d4c\u5957\u5217\u8868\u7684\u65b9\u6cd5\u3001NumPy\u5e93\u4e2d\u7684array\u51fd\u6570\u3001Pandas\u5e93\u4e2d\u7684DataFrame\u51fd\u6570\u3002<strong>\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u662f\u4f7f\u7528NumPy\u5e93<\/strong>\uff0c\u56e0\u4e3aNumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u4f8b\u5982\u77e9\u9635\u4e58\u6cd5\u3001\u8f6c\u7f6e\u3001\u6c42\u9006\u7b49\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e14\u89e3\u91ca\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u6765\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5217\u8868\u548c\u5d4c\u5957\u5217\u8868\u521b\u9020\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u5217\u8868\uff08List\uff09\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u901a\u8fc7\u5d4c\u5957\u5217\u8868\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a2x3\u7684\u77e9\u9635<\/p>\n<p>matrix = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u4f46\u5728\u5904\u7406\u5927\u578b\u77e9\u9635\u6216\u8fdb\u884c\u590d\u6742\u77e9\u9635\u64cd\u4f5c\u65f6\u663e\u5f97\u6548\u7387\u4f4e\u4e0b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u521b\u9020\u77e9\u9635<\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u7684\u5f3a\u5927\u5e93\u3002\u5b83\u4e0d\u4ec5\u63d0\u4f9b\u4e86\u521b\u5efa\u77e9\u9635\u7684\u529f\u80fd\uff0c\u8fd8\u63d0\u4f9b\u4e86\u5927\u91cf\u77e9\u9635\u64cd\u4f5c\u7684\u5185\u5efa\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5NumPy<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u521b\u5efa\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u77e9\u9635\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8c03\u7528<code>numpy.array<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2x3\u7684\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u65b9\u5f0f\u6765\u521b\u5efa\u7279\u6b8a\u77e9\u9635\uff0c\u5982\u96f6\u77e9\u9635\u3001\u5355\u4f4d\u77e9\u9635\u3001\u968f\u673a\u77e9\u9635\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3x3\u7684\u96f6\u77e9\u9635<\/p>\n<p>zero_matrix = np.zeros((3, 3))<\/p>\n<p>print(zero_matrix)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u5355\u4f4d\u77e9\u9635<\/strong><\/h2>\n<p>identity_matrix = np.eye(3)<\/p>\n<p>print(identity_matrix)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u968f\u673a\u77e9\u9635<\/strong><\/h2>\n<p>random_matrix = np.random.random((3, 3))<\/p>\n<p>print(random_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u77e9\u9635\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u77e9\u9635\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u3001\u8f6c\u7f6e\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u52a0\u6cd5<\/p>\n<p>matrix1 = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6]<\/p>\n<p>])<\/p>\n<p>matrix2 = np.array([<\/p>\n<p>    [7, 8, 9],<\/p>\n<p>    [10, 11, 12]<\/p>\n<p>])<\/p>\n<p>sum_matrix = matrix1 + matrix2<\/p>\n<p>print(sum_matrix)<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>product_matrix = np.dot(matrix1, matrix2.T)<\/p>\n<p>print(product_matrix)<\/p>\n<h2><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/h2>\n<p>transpose_matrix = matrix1.T<\/p>\n<p>print(transpose_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Pandas\u5e93\u521b\u9020\u77e9\u9635<\/p>\n<\/p>\n<p><p>Pandas\u5e93\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u3002Pandas\u4e2d\u7684DataFrame\u53ef\u4ee5\u770b\u4f5c\u662f\u5e26\u6709\u6807\u7b7e\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Pandas<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86Pandas\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u521b\u5efa\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u5e93\u521b\u5efa\u77e9\u9635\uff08DataFrame\uff09\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a2x3\u7684\u77e9\u9635<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 4],<\/p>\n<p>    &#39;B&#39;: [2, 5],<\/p>\n<p>    &#39;C&#39;: [3, 6]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u77e9\u9635\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u52a0\u6cd5<\/p>\n<p>df1 = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [1, 4],<\/p>\n<p>    &#39;B&#39;: [2, 5],<\/p>\n<p>    &#39;C&#39;: [3, 6]<\/p>\n<p>})<\/p>\n<p>df2 = pd.DataFrame({<\/p>\n<p>    &#39;A&#39;: [7, 10],<\/p>\n<p>    &#39;B&#39;: [8, 11],<\/p>\n<p>    &#39;C&#39;: [9, 12]<\/p>\n<p>})<\/p>\n<p>sum_df = df1 + df2<\/p>\n<p>print(sum_df)<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>product_df = df1.dot(df2.T)<\/p>\n<p>print(product_df)<\/p>\n<h2><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/h2>\n<p>transpose_df = df1.T<\/p>\n<p>print(transpose_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\uff0c\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5217\u8868\u548c\u5d4c\u5957\u5217\u8868<\/strong>\uff1a\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5c0f\u578b\u77e9\u9635\u548c\u57fa\u672c\u64cd\u4f5c\u3002<\/li>\n<li><strong>NumPy<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u5904\u7406\u5927\u578b\u77e9\u9635\u548c\u8fdb\u884c\u590d\u6742\u77e9\u9635\u64cd\u4f5c\u7684\u573a\u666f\uff0c\u662f\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7684\u9996\u9009\u3002<\/li>\n<li><strong>Pandas<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c\u5c24\u5176\u662f\u5e26\u6709\u6807\u7b7e\u7684\u6570\u636e\u77e9\u9635\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><p><strong>\u603b\u7ed3\uff1a<\/strong>\u7528Python\u521b\u9020\u4e00\u4e2a\u77e9\u9635\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u57fa\u7840\u5217\u8868\u3001NumPy\u5e93\u548cPandas\u5e93\u3002<strong>\u5176\u4e2d\uff0cNumPy\u5e93\u662f\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u5de5\u5177<\/strong>\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0cNumPy\u548cPandas\u662f\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6765\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u4e8c\u7ef4\u77e9\u9635\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5217\u8868\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u521b\u5efa\u4e86\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\u3002\u4e3a\u4e86\u4fbf\u4e8e\u64cd\u4f5c\uff0c\u60a8\u4e5f\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<p><strong>\u4f7f\u7528NumPy\u521b\u5efa\u77e9\u9635\u6709\u4ec0\u4e48\u597d\u5904\uff1f<\/strong><br \/>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\u7684\u5185\u7f6e\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy.array()<\/code>\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5d4c\u5957\u5217\u8868\u8f6c\u5316\u4e3a\u6570\u7ec4\u3002\u901a\u8fc7NumPy\uff0c\u60a8\u80fd\u591f\u4eab\u53d7\u5230\u66f4\u5feb\u7684\u8ba1\u7b97\u901f\u5ea6\u548c\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u77e9\u9635\u8fd0\u7b97\u3001\u7ebf\u6027\u4ee3\u6570\u7b49\u3002<\/p>\n<p><strong>\u5982\u4f55\u521d\u59cb\u5316\u4e00\u4e2a\u7279\u5b9a\u5927\u5c0f\u7684\u77e9\u9635\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u521b\u5efa\u4e00\u4e2a\u7279\u5b9a\u5927\u5c0f\u7684\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>numpy.zeros()<\/code>\u3001<code>numpy.ones()<\/code>\u6216<code>numpy.empty()<\/code>\u51fd\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u5206\u522b\u7528\u4e8e\u521b\u5efa\u5168\u4e3a0\u3001\u5168\u4e3a1\u6216\u672a\u521d\u59cb\u5316\u7684\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u8981\u521b\u5efa\u4e00\u4e2a3&#215;4\u7684\u5168\u96f6\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmatrix = np.zeros((3, 4))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b3\u884c4\u5217\u7684\u77e9\u9635\uff0c\u6240\u6709\u5143\u7d20\u5747\u4e3a0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u521b\u9020\u4e00\u4e2a\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u57fa\u7840\u5217\u8868\u3001NumPy\u5e93\u548cPandas\u5e93\u7b49\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u6570\u636e\u79d1\u5b66 [&hellip;]","protected":false},"author":3,"featured_media":1067086,"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\/1067080"}],"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=1067080"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1067080\/revisions"}],"predecessor-version":[{"id":1067088,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1067080\/revisions\/1067088"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1067086"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1067080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1067080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1067080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}