{"id":1143424,"date":"2025-01-08T22:50:08","date_gmt":"2025-01-08T14:50:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1143424.html"},"modified":"2025-01-08T22:50:11","modified_gmt":"2025-01-08T14:50:11","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e4%b8%80%e7%bb%b4%e6%95%b0%e6%8d%ae%e5%8f%98%e6%88%90%e4%ba%8c%e7%bb%b4%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1143424.html","title":{"rendered":"python\u5982\u4f55\u5c06\u4e00\u7ef4\u6570\u636e\u53d8\u6210\u4e8c\u7ef4\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181039\/efc30a38-a13d-40f8-af6e-ca2972f329c3.webp\" alt=\"python\u5982\u4f55\u5c06\u4e00\u7ef4\u6570\u636e\u53d8\u6210\u4e8c\u7ef4\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5c06\u4e00\u7ef4\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528numpy\u3001pandas\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c<strong>numpy<\/strong> \u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u4ee5\u53ca\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Numpy\u8fdb\u884c\u8f6c\u6362<\/p>\n<\/p>\n<p><p>Numpy\u662fPython\u4e2d\u5904\u7406\u6570\u503c\u6570\u636e\u7684\u6838\u5fc3\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u51fd\u6570\u3002\u4f7f\u7528Numpy\u7684reshape\u65b9\u6cd5\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>1. Numpy\u5e93\u7684\u5b89\u88c5\u4e0e\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Numpy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\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><p>\u7136\u540e\u5728Python\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. \u4f7f\u7528reshape\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u7ef4\u6570\u7ec4<\/p>\n<p>one_d_array = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<h2><strong>\u4f7f\u7528reshape\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>two_d_array = one_d_array.reshape(2, 3)<\/p>\n<p>print(two_d_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>one_d_array<\/code>\u662f\u4e00\u4e2a\u5305\u542b6\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4\u3002\u901a\u8fc7<code>reshape(2, 3)<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a2\u884c3\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>3. \u81ea\u52a8\u8ba1\u7b97\u67d0\u4e00\u7ef4\u5ea6<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u4e0d\u786e\u5b9a\u67d0\u4e00\u4e2a\u7ef4\u5ea6\u7684\u5927\u5c0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>-1<\/code>\u8ba9Numpy\u81ea\u52a8\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">two_d_array_auto = one_d_array.reshape(2, -1)<\/p>\n<p>print(two_d_array_auto)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>-1<\/code>\u8868\u793a\u8be5\u7ef4\u5ea6\u7531Numpy\u81ea\u52a8\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>4. \u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528reshape\u65b9\u6cd5\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u539f\u4e00\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\u6570\u91cf\u4e0e\u76ee\u6807\u4e8c\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\u6570\u91cf\u76f8\u540c\uff0c\u5426\u5219\u4f1a\u629b\u51fa\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fd9\u4f1a\u629b\u51fa\u9519\u8bef\uff0c\u56e0\u4e3a\u5143\u7d20\u6570\u91cf\u4e0d\u5339\u914d<\/p>\n<p>incorrect_array = one_d_array.reshape(3, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Pandas\u8fdb\u884c\u8f6c\u6362<\/p>\n<\/p>\n<p><p>Pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u64cd\u4f5c\u3002\u5b83\u63d0\u4f9b\u4e86DataFrame\u7ed3\u6784\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06\u4e00\u7ef4\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>1. Pandas\u5e93\u7684\u5b89\u88c5\u4e0e\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Pandas\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\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><p>\u7136\u540e\u5728Python\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. \u4f7f\u7528DataFrame\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868\u6570\u636e\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u7ef4\u5217\u8868<\/p>\n<p>one_d_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u4f7f\u7528Pandas\u7684DataFrame\u8fdb\u884c\u8f6c\u6362<\/strong><\/h2>\n<p>two_d_dataframe = pd.DataFrame(np.array(one_d_list).reshape(2, 3))<\/p>\n<p>print(two_d_dataframe)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>one_d_list<\/code>\u662f\u4e00\u4e2a\u5305\u542b6\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u5217\u8868\u3002\u901a\u8fc7\u5c06\u5176\u8f6c\u6362\u4e3aNumpy\u6570\u7ec4\u5e76\u4f7f\u7528<code>reshape<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a2\u884c3\u5217\u7684DataFrame\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u8f6c\u6362<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684Python\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5c\u5217\u8868\u3002\u867d\u7136\u4e0d\u5982Numpy\u548cPandas\u9ad8\u6548\uff0c\u4f46\u5728\u67d0\u4e9b\u7b80\u5355\u573a\u666f\u4e0b\u4e5f\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>1. \u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868\u6570\u636e\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u7ef4\u5217\u8868<\/p>\n<p>one_d_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u8f6c\u6362<\/strong><\/h2>\n<p>two_d_list = [one_d_list[i:i+3] for i in range(0, len(one_d_list), 3)]<\/p>\n<p>print(two_d_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>one_d_list<\/code>\u662f\u4e00\u4e2a\u5305\u542b6\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u5217\u8868\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b2\u4e2a\u5b50\u5217\u8868\u7684\u4e8c\u7ef4\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u5305\u542b3\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528zip\u51fd\u6570\u8fdb\u884c\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684<code>zip<\/code>\u51fd\u6570\uff0c\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u7ef4\u5217\u8868<\/p>\n<p>one_d_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u4f7f\u7528zip\u51fd\u6570\u8fdb\u884c\u8f6c\u6362<\/strong><\/h2>\n<p>two_d_list_zip = list(zip(*[iter(one_d_list)]*3))<\/p>\n<p>print(two_d_list_zip)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>one_d_list<\/code>\u662f\u4e00\u4e2a\u5305\u542b6\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u5217\u8868\u3002\u901a\u8fc7<code>zip<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b2\u4e2a\u5b50\u5217\u8868\u7684\u4e8c\u7ef4\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u5305\u542b3\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528itertools\u8fdb\u884c\u8f6c\u6362<\/p>\n<\/p>\n<p><p>Python\u7684\u6807\u51c6\u5e93\u4e2d\u5305\u542b\u4e00\u4e2a\u540d\u4e3a<code>itertools<\/code>\u7684\u6a21\u5757\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u8fed\u4ee3\u5668\u64cd\u4f5c\u7684\u51fd\u6570\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>itertools<\/code>\u4e2d\u7684<code>islice<\/code>\u51fd\u6570\u6765\u5c06\u4e00\u7ef4\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>1. itertools\u5e93\u7684\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165<code>itertools<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528islice\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868\u6570\u636e\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u7ef4\u5217\u8868<\/p>\n<p>one_d_list = [1, 2, 3, 4, 5, 6]<\/p>\n<h2><strong>\u4f7f\u7528itertools\u7684islice\u51fd\u6570\u8fdb\u884c\u8f6c\u6362<\/strong><\/h2>\n<p>two_d_list_itertools = [list(itertools.islice(one_d_list, i, i+3)) for i in range(0, len(one_d_list), 3)]<\/p>\n<p>print(two_d_list_itertools)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>one_d_list<\/code>\u662f\u4e00\u4e2a\u5305\u542b6\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u5217\u8868\u3002\u901a\u8fc7<code>islice<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b2\u4e2a\u5b50\u5217\u8868\u7684\u4e8c\u7ef4\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u5305\u542b3\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5c06\u4e00\u7ef4\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u6709\u5f88\u591a\u573a\u666f\uff0c\u4f8b\u5982\u56fe\u50cf\u5904\u7406\u3001\u6570\u636e\u5206\u6790\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u3002\u4e0b\u9762\u6211\u4eec\u4ee5\u673a\u5668\u5b66\u4e60\u4e2d\u7684\u6570\u636e\u9884\u5904\u7406\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u5982\u4f55\u5c06\u4e00\u7ef4\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>1. \u56fe\u50cf\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u901a\u5e38\u4ee5\u4e8c\u7ef4\u6570\u7ec4\u7684\u5f62\u5f0f\u8868\u793a\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u8868\u793a\u7684\u7070\u5ea6\u56fe\u50cf\u6570\u636e\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u4ee5\u4fbf\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u7ef4\u56fe\u50cf\u6570\u636e<\/strong><\/h2>\n<p>one_d_image = np.array([0, 255, 0, 255, 0, 255, 0, 255, 0])<\/p>\n<h2><strong>\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u56fe\u50cf\u6570\u636e<\/strong><\/h2>\n<p>two_d_image = one_d_image.reshape(3, 3)<\/p>\n<p>print(two_d_image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>one_d_image<\/code>\u662f\u4e00\u4e2a\u5305\u542b9\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4\uff0c\u8868\u793a3&#215;3\u7684\u7070\u5ea6\u56fe\u50cf\u6570\u636e\u3002\u901a\u8fc7<code>reshape<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a3\u884c3\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h3>2. \u6570\u636e\u5206\u6790\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u9886\u57df\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5904\u7406\u5927\u91cf\u7684\u6570\u636e\uff0c\u5c06\u4e00\u7ef4\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u65b9\u4fbf\u5730\u5206\u6790\u548c\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u7ef4\u6570\u636e<\/strong><\/h2>\n<p>one_d_data = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u4f7f\u7528Pandas\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e<\/strong><\/h2>\n<p>two_d_data = pd.DataFrame(np.array(one_d_data).reshape(3, 3))<\/p>\n<p>print(two_d_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>one_d_data<\/code>\u662f\u4e00\u4e2a\u5305\u542b9\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u5217\u8868\u3002\u901a\u8fc7\u5c06\u5176\u8f6c\u6362\u4e3aNumpy\u6570\u7ec4\u5e76\u4f7f\u7528<code>reshape<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a3\u884c3\u5217\u7684DataFrame\u3002<\/p>\n<\/p>\n<p><h3>3. \u673a\u5668\u5b66\u4e60\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u9886\u57df\uff0c\u8bad\u7ec3\u6570\u636e\u901a\u5e38\u4ee5\u4e8c\u7ef4\u6570\u7ec4\u7684\u5f62\u5f0f\u8868\u793a\uff0c\u6bcf\u884c\u8868\u793a\u4e00\u4e2a\u6837\u672c\uff0c\u6bcf\u5217\u8868\u793a\u4e00\u4e2a\u7279\u5f81\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6837\u672c\u7684\u4e00\u7ef4\u6570\u636e\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u4ee5\u4fbf\u8fdb\u884c\u8bad\u7ec3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u7ef4\u8bad\u7ec3\u6570\u636e<\/strong><\/h2>\n<p>one_d_tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])<\/p>\n<h2><strong>\u5c06\u5176\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u8bad\u7ec3\u6570\u636e<\/strong><\/h2>\n<p>two_d_train_data = one_d_train_data.reshape(4, 3)<\/p>\n<p>print(two_d_train_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>one_d_train_data<\/code>\u662f\u4e00\u4e2a\u5305\u542b12\u4e2a\u5143\u7d20\u7684\u4e00\u7ef4\u6570\u7ec4\uff0c\u8868\u793a4\u4e2a\u6837\u672c\uff0c\u6bcf\u4e2a\u6837\u672c\u5305\u542b3\u4e2a\u7279\u5f81\u3002\u901a\u8fc7<code>reshape<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a4\u884c3\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u5c06\u4e00\u7ef4\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528Numpy\u3001Pandas\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001itertools\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>Numpy<\/strong>\u662f\u5904\u7406\u6570\u503c\u6570\u636e\u7684\u9996\u9009\u65b9\u6cd5\uff0c<strong>Pandas<\/strong>\u5728\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u65b9\u4fbf\uff0c<strong>\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong>\u9002\u7528\u4e8e\u7b80\u5355\u573a\u666f\uff0c<strong>itertools<\/strong>\u5219\u5728\u5904\u7406\u8fed\u4ee3\u5668\u65f6\u975e\u5e38\u6709\u7528\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u4e2d\u7684<code>reshape<\/code>\u51fd\u6570\u6765\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\uff0c\u7136\u540e\u5bfc\u5165\u5b83\u5e76\u4f7f\u7528<code>reshape<\/code>\u65b9\u6cd5\u6307\u5b9a\u65b0\u7684\u7ef4\u5ea6\u3002\u4f8b\u5982\uff0c<code>array.reshape(\u884c\u6570, \u5217\u6570)<\/code>\u53ef\u4ee5\u5c06\u4e00\u7ef4\u6570\u636e\u8f6c\u6362\u4e3a\u6307\u5b9a\u5f62\u72b6\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<p><strong>\u5728\u8f6c\u6362\u4e00\u7ef4\u6570\u636e\u65f6\uff0c\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u4e00\u7ef4\u5230\u4e8c\u7ef4\u7684\u8f6c\u6362\u65f6\uff0c\u786e\u4fdd\u4e00\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\u603b\u6570\u80fd\u591f\u88ab\u65b0\u5f62\u72b6\u7684\u884c\u6570\u548c\u5217\u6570\u6574\u9664\u3002\u5982\u679c\u4e0d\u6ee1\u8db3\u8fd9\u4e00\u6761\u4ef6\uff0c\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002\u6b64\u5916\uff0c\u6ce8\u610f\u4f7f\u7528\u7684\u7ef4\u5ea6\u5e94\u8be5\u9002\u5408\u6570\u636e\u7684\u5b9e\u9645\u610f\u4e49\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u548c\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u5217\u8868\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528NumPy\u5916\uff0cPython\u7684\u5185\u7f6e\u5217\u8868\u529f\u80fd\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u4e00\u7ef4\u5217\u8868\u5230\u4e8c\u7ef4\u5217\u8868\u7684\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u5c06\u4e00\u7ef4\u5217\u8868\u6309\u6307\u5b9a\u7684\u5217\u6570\u5207\u5206\u6210\u591a\u4e2a\u5b50\u5217\u8868\u3002\u4f7f\u7528<code>[data[i:i + cols] for i in range(0, len(data), cols)]<\/code>\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5c06\u4e00\u7ef4\u6570\u636e\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u636e\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4f7f\u7528numpy\u3001pandas\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u7b49\u3002 \u5176\u4e2d\uff0cnum [&hellip;]","protected":false},"author":3,"featured_media":1143433,"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\/1143424"}],"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=1143424"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143424\/revisions"}],"predecessor-version":[{"id":1143434,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143424\/revisions\/1143434"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1143433"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1143424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1143424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1143424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}