{"id":1139966,"date":"2025-01-08T22:15:28","date_gmt":"2025-01-08T14:15:28","guid":{"rendered":""},"modified":"2025-01-08T22:15:31","modified_gmt":"2025-01-08T14:15:31","slug":"%e5%a6%82%e4%bd%95%e6%8a%8a%e4%b8%80%e8%a1%8c%e5%88%97%e8%a1%a8%e5%8f%98%e4%b8%ba%e5%a4%9a%e5%88%97python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139966.html","title":{"rendered":"\u5982\u4f55\u628a\u4e00\u884c\u5217\u8868\u53d8\u4e3a\u591a\u5217python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103109\/438a69fd-10f1-4435-b720-b78412c49889.webp\" alt=\"\u5982\u4f55\u628a\u4e00\u884c\u5217\u8868\u53d8\u4e3a\u591a\u5217python\" \/><\/p>\n<p><p> <strong>\u8981\u5c06\u4e00\u884c\u5217\u8868\u53d8\u4e3a\u591a\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ecNumpy\u5e93\u3001Pandas\u5e93\u6216\u8005\u6807\u51c6\u7684Python\u4ee3\u7801\u3002<\/strong> \u5176\u4e2d\uff0c<strong>Numpy\u5e93\u65b9\u6cd5\u6700\u4e3a\u5e38\u7528<\/strong>\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528Numpy\u5e93<\/h3>\n<\/p>\n<p><p>Numpy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u6838\u5fc3\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u7528\u4e8e\u5904\u7406\u8fd9\u4e9b\u6570\u7ec4\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5b89\u88c5Numpy<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5Numpy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\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><h4>1.2 \u521b\u5efa\u4e00\u7ef4\u6570\u7ec4\u5e76\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868\uff0c\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u591a\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Numpy\u7684<code>reshape<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>one_d_array = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5047\u8bbe\u6bcf\u884c\u67093\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>two_d_array = np.reshape(one_d_array, (-1, 3))<\/p>\n<p>print(two_d_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>np.reshape<\/code>\u51fd\u6570\u5c06\u4e00\u7ef4\u6570\u7ec4<code>one_d_array<\/code>\u8f6c\u6362\u4e3a\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<code>two_d_array<\/code>\u3002<code>-1<\/code>\u8868\u793a\u81ea\u52a8\u8ba1\u7b97\u884c\u6570\uff0c<code>3<\/code>\u8868\u793a\u6bcf\u884c\u67093\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u7684\u6d41\u884c\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b89\u88c5Pandas<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5Pandas\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\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><h4>2.2 \u4f7f\u7528DataFrame\u548creshape\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528Pandas\u7684DataFrame\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>one_d_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(one_d_list)<\/p>\n<h2><strong>\u4f7f\u7528reshape\u65b9\u6cd5\u5c06DataFrame\u8f6c\u6362\u4e3a\u591a\u5217<\/strong><\/h2>\n<p>df = df.values.reshape(-1, 3)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2aDataFrame\uff0c\u7136\u540e\u4f7f\u7528<code>reshape<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u591a\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6807\u51c6Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5373\u4f7f\u6ca1\u6709Numpy\u548cPandas\u5e93\uff0c\u4f60\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u7684Python\u4ee3\u7801\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c\u5207\u7247<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868<\/p>\n<p>one_d_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u5b9a\u4e49\u6bcf\u884c\u7684\u5143\u7d20\u6570\u91cf<\/strong><\/h2>\n<p>num_columns = 3<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c\u5207\u7247\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u591a\u5217<\/strong><\/h2>\n<p>two_d_list = [one_d_list[i:i + num_columns] for i in range(0, len(one_d_list), num_columns)]<\/p>\n<p>print(two_d_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u89e3\u6790\u548c\u5207\u7247\u5c06\u4e00\u7ef4\u5217\u8868<code>one_d_list<\/code>\u8f6c\u6362\u4e3a\u591a\u5217\u7684\u4e8c\u7ef4\u5217\u8868<code>two_d_list<\/code>\u3002<code>num_columns<\/code>\u5b9a\u4e49\u4e86\u6bcf\u884c\u7684\u5143\u7d20\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528itertools\u6a21\u5757<\/h3>\n<\/p>\n<p><p>itertools\u6a21\u5757\u63d0\u4f9b\u4e86\u5404\u79cd\u7528\u4e8e\u8fed\u4ee3\u5668\u64cd\u4f5c\u7684\u5de5\u5177\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>islice<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>one_d_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u5b9a\u4e49\u6bcf\u884c\u7684\u5143\u7d20\u6570\u91cf<\/strong><\/h2>\n<p>num_columns = 3<\/p>\n<h2><strong>\u4f7f\u7528itertools\u7684islice\u51fd\u6570\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u591a\u5217<\/strong><\/h2>\n<p>two_d_list = list(itertools.zip_longest(*[iter(one_d_list)] * num_columns))<\/p>\n<p>print(two_d_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>itertools.zip_longest<\/code>\u548c<code>iter<\/code>\u51fd\u6570\u5c06\u4e00\u7ef4\u5217\u8868<code>one_d_list<\/code>\u8f6c\u6362\u4e3a\u591a\u5217\u7684\u4e8c\u7ef4\u5217\u8868<code>two_d_list<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5217\u8868\u7684chunk\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u901a\u7528\u7684<code>chunk<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u5217\u8868<\/p>\n<p>one_d_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]<\/p>\n<h2><strong>\u5b9a\u4e49chunk\u51fd\u6570<\/strong><\/h2>\n<p>def chunk(lst, n):<\/p>\n<p>    for i in range(0, len(lst), n):<\/p>\n<p>        yield lst[i:i + n]<\/p>\n<h2><strong>\u4f7f\u7528chunk\u51fd\u6570\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u591a\u5217<\/strong><\/h2>\n<p>two_d_list = list(chunk(one_d_list, 3))<\/p>\n<p>print(two_d_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u901a\u7528\u7684<code>chunk<\/code>\u51fd\u6570\uff0c\u5e76\u4f7f\u7528\u5b83\u5c06\u4e00\u7ef4\u5217\u8868<code>one_d_list<\/code>\u8f6c\u6362\u4e3a\u591a\u5217\u7684\u4e8c\u7ef4\u5217\u8868<code>two_d_list<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u51e0\u79cd\u5c06\u4e00\u884c\u5217\u8868\u53d8\u4e3a\u591a\u5217\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528Numpy\u5e93\u3001Pandas\u5e93\u548c\u6807\u51c6\u7684Python\u4ee3\u7801\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u9879\u76ee\u73af\u5883\u3002<\/p>\n<\/p>\n<p><p><strong>Numpy\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6027\u80fd\u548c\u591a\u7ef4\u6570\u7ec4\u64cd\u4f5c\u7684\u573a\u666f\u3002<br \/><strong>Pandas\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u65f6\u3002<br \/><strong>\u6807\u51c6Python\u4ee3\u7801<\/strong>\uff1a\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u6216\u4e0d\u4f9d\u8d56\u5916\u90e8\u5e93\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u539f\u7406\u548c\u4f7f\u7528\u573a\u666f\u5c06\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5904\u7406\u6570\u636e\u8f6c\u6362\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6216<code>numpy<\/code>\u5e93\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3a\u4e8c\u7ef4\u5217\u8868\u3002\u901a\u8fc7\u8bbe\u7f6e\u6bcf\u4e00\u884c\u7684\u5143\u7d20\u6570\u91cf\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5217\u8868\u91cd\u7ec4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">one_d_list = [1, 2, 3, 4, 5, 6]\nnum_columns = 2\ntwo_d_list = [one_d_list[i:i + num_columns] for i in range(0, len(one_d_list), num_columns)]\nprint(two_d_list)  # \u8f93\u51fa\uff1a[[1, 2], [3, 4], [5, 6]]\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u600e\u6837\u4f7f\u7528<code>pandas<\/code>\u5e93\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\uff1f<\/strong><br \/><code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u4e00\u7ef4\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\u3002\u901a\u8fc7\u6307\u5b9a\u5217\u6570\u6216\u884c\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u6570\u636e\u7684\u6574\u7406\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\none_d_list = [1, 2, 3, 4, 5, 6]\nnum_columns = 2\ndf = pd.DataFrame([one_d_list[i:i + num_columns] for i in range(0, len(one_d_list), num_columns)])\nprint(df)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u8f6c\u6362\u5217\u8868\uff0c\u8fd8\u80fd\u4e3a\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u63d0\u4f9b\u4fbf\u5229\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u6765\u5b9e\u73b0\u4e00\u7ef4\u5217\u8868\u5230\u591a\u5217\u6570\u7ec4\u7684\u8f6c\u6362\uff1f<\/strong><br \/><code>numpy<\/code>\u5e93\u63d0\u4f9b\u4e86<code>reshape<\/code>\u529f\u80fd\uff0c\u53ef\u4ee5\u5feb\u901f\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u591a\u7ef4\u6570\u7ec4\u3002\u53ea\u9700\u6307\u5b9a\u65b0\u7684\u5f62\u72b6\u5373\u53ef\u5b9e\u73b0\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\none_d_array = np.array([1, 2, 3, 4, 5, 6])\nnum_columns = 2\ntwo_d_array = one_d_array.reshape(-1, num_columns)\nprint(two_d_array)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5c24\u5176\u9002\u5408\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u4e14\u6027\u80fd\u4f18\u8d8a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06\u4e00\u884c\u5217\u8868\u53d8\u4e3a\u591a\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ecNumpy\u5e93\u3001Pandas\u5e93\u6216\u8005\u6807\u51c6\u7684Python [&hellip;]","protected":false},"author":3,"featured_media":1139972,"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\/1139966"}],"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=1139966"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139966\/revisions"}],"predecessor-version":[{"id":1139974,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139966\/revisions\/1139974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139972"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}