{"id":1076406,"date":"2025-01-08T11:53:03","date_gmt":"2025-01-08T03:53:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076406.html"},"modified":"2025-01-08T11:53:06","modified_gmt":"2025-01-08T03:53:06","slug":"%e5%a6%82%e4%bd%95%e6%8a%8a%e6%95%b0%e7%bb%84%e8%bd%ac%e6%8d%a2%e6%88%90%e7%9f%a9%e9%98%b5python-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076406.html","title":{"rendered":"\u5982\u4f55\u628a\u6570\u7ec4\u8f6c\u6362\u6210\u77e9\u9635python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181012\/1fbb9431-043e-4f45-b505-468980eaef93.webp\" alt=\"\u5982\u4f55\u628a\u6570\u7ec4\u8f6c\u6362\u6210\u77e9\u9635python\" \/><\/p>\n<p><p> <strong>\u5c06\u6570\u7ec4\u8f6c\u6362\u6210\u77e9\u9635\u7684\u6838\u5fc3\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528Pandas\u5e93\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u662f\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u548c\u77e9\u9635\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6570\u7ec4\u4e0e\u77e9\u9635\u7684\u76f8\u4e92\u8f6c\u6362\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u5c06\u6570\u7ec4\u8f6c\u6362\u6210\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u652f\u6301\u5927\u591a\u6570\u7ef4\u5ea6\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u8981\u5c06\u6570\u7ec4\u8f6c\u6362\u6210\u77e9\u9635\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5e76\u5bfc\u5165NumPy\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528NumPy\u7684 <code>array<\/code> \u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5c06\u6570\u7ec4\u8f6c\u6362\u6210\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u7684 <code>reshape<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u662f\uff0c\u77e9\u9635\u7684\u603b\u5143\u7d20\u4e2a\u6570\u5fc5\u987b\u4e0e\u539f\u6570\u7ec4\u76f8\u540c\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u62102\u884c3\u5217\u7684\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = array.reshape(2, 3)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u4f1a\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1 2 3]<\/p>\n<p> [4 5 6]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>reshape<\/code> \u65b9\u6cd5\u4f7f\u7528\u4e24\u4e2a\u53c2\u6570\uff0c\u8fd9\u4e24\u4e2a\u53c2\u6570\u5206\u522b\u4ee3\u8868\u77e9\u9635\u7684\u884c\u6570\u548c\u5217\u6570\u3002<code>reshape(2, 3)<\/code>\u8868\u793a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a2\u884c3\u5217\u7684\u77e9\u9635\u3002\u6ce8\u610f\uff0c\u6570\u7ec4\u5143\u7d20\u7684\u6570\u91cf\u5fc5\u987b\u4e0e\u77e9\u9635\u7684\u5143\u7d20\u6570\u91cf\u76f8\u5339\u914d\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u867d\u7136NumPy\u5e93\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u4f46\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7eafPython\u7684\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u6570\u7ec4\u5230\u77e9\u9635\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">array = [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b9a\u4e49\u77e9\u9635\u7684\u7ef4\u5ea6<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u60f3\u8981\u5c06\u5176\u8f6c\u6362\u4e3a2\u884c3\u5217\u7684\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows, cols = 2, 3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u8fdb\u884c\u8f6c\u6362<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [array[i * cols:(i + 1) * cols] for i in range(rows)]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u4f1a\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1, 2, 3], [4, 5, 6]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u4e5f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u867d\u7136\u5b83\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6570\u7ec4\u548c\u77e9\u9635\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u5e76\u5bfc\u5165Pandas\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">array = [1, 2, 3, 4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aPandas\u7684DataFrame<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.DataFrame(np.array(array).reshape(2, 3))<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u5c06\u4f1a\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>   0  1  2<\/p>\n<p>0  1  2  3<\/p>\n<p>1  4  5  6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>pd.DataFrame<\/code> \u65b9\u6cd5\u5c06NumPy\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61\uff0c\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u6570\u7ec4\u5230\u77e9\u9635\u7684\u8f6c\u6362\u3002\u867d\u7136DataFrame\u4e0e\u4f20\u7edf\u7684\u77e9\u9635\u4e0d\u540c\uff0c\u4f46\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\u53ef\u4ee5\u89c6\u4e3a\u7b49\u4ef7\u7684\u64cd\u4f5c\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5c06\u6570\u7ec4\u8f6c\u6362\u6210\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u548c\u9ad8\u6548\u7684\u662f\u4f7f\u7528NumPy\u5e93\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u548c\u77e9\u9635\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6570\u7ec4\u4e0e\u77e9\u9635\u7684\u76f8\u4e92\u8f6c\u6362\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548cPandas\u5e93\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u4f46NumPy\u662f\u6700\u63a8\u8350\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>\u4f18\u7f3a\u70b9\u6bd4\u8f83<\/h4>\n<\/p>\n<ul>\n<li><strong>NumPy\u5e93<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u6548\u7387\u9ad8\uff0c\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u548c\u590d\u6742\u77e9\u9635\u8fd0\u7b97\u3002<\/li>\n<li><strong>\u5217\u8868\u89e3\u6790<\/strong>\uff1a\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u5408\u5c0f\u89c4\u6a21\u6570\u636e\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u6027\u80fd\u8f83\u4f4e\u3002<\/li>\n<li><strong>Pandas\u5e93<\/strong>\uff1a\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u9002\u5408\u5904\u7406\u5e26\u6807\u7b7e\u7684\u6570\u636e\uff0c\u4f46\u5728\u7eaf\u77e9\u9635\u8fd0\u7b97\u4e2d\u4e0d\u5982NumPy\u9ad8\u6548\u3002<\/li>\n<\/ul>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u89c4\u6a21\u6765\u51b3\u5b9a\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u4efb\u52a1\uff0cNumPy\u5e93\u662f\u6700\u4f18\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u77e9\u9635\u3002\u901a\u8fc7\u8c03\u7528<code>numpy.reshape()<\/code>\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9a\u76ee\u6807\u77e9\u9635\u7684\u5f62\u72b6\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<code>arr = [1, 2, 3, 4, 5, 6]<\/code>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>arr.reshape(2, 3)<\/code>\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a2\u884c3\u5217\u7684\u77e9\u9635\u3002<\/p>\n<p><strong>\u4f7f\u7528Pandas\u5e93\u8f6c\u6362\u6570\u7ec4\u4e3a\u77e9\u9635\u6709\u4ec0\u4e48\u597d\u5904\uff1f<\/strong><br \/>Pandas\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982DataFrame\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u77e9\u9635\u3002\u4f7f\u7528<code>pd.DataFrame()<\/code>\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff0c\u540c\u65f6\u63d0\u4f9b\u66f4\u591a\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u4f8b\u5982\u6570\u636e\u7b5b\u9009\u548c\u5206\u7ec4\uff0c\u9002\u5408\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u6570\u7ec4\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8f6c\u6362\u6570\u7ec4\u65f6\uff0c\u786e\u4fdd\u6570\u7ec4\u7684\u5143\u7d20\u603b\u6570\u4e0e\u76ee\u6807\u77e9\u9635\u7684\u5f62\u72b6\u5339\u914d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u4e00\u4e2a\u957f\u5ea6\u4e3a6\u7684\u6570\u7ec4\u8f6c\u6362\u4e3a2\u884c3\u5217\u7684\u77e9\u9635\uff0c\u90a3\u4e48\u4f60\u9700\u8981\u786e\u4fdd\u76ee\u6807\u7ef4\u5ea6\u7684\u4e58\u79ef\u7b49\u4e8e\u6570\u7ec4\u7684\u957f\u5ea6\u3002\u6b64\u5916\uff0c\u6ce8\u610f\u6570\u636e\u7c7b\u578b\u7684\u517c\u5bb9\u6027\uff0c\u786e\u4fdd\u6240\u6709\u5143\u7d20\u90fd\u662f\u76f8\u540c\u7c7b\u578b\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\u6216\u6570\u636e\u4e22\u5931\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06\u6570\u7ec4\u8f6c\u6362\u6210\u77e9\u9635\u7684\u6838\u5fc3\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u3001\u4f7f\u7528Pandas\u5e93\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u662f\u6700 [&hellip;]","protected":false},"author":3,"featured_media":1076414,"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\/1076406"}],"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=1076406"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076406\/revisions"}],"predecessor-version":[{"id":1076417,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076406\/revisions\/1076417"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076414"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}