{"id":997772,"date":"2024-12-27T09:26:56","date_gmt":"2024-12-27T01:26:56","guid":{"rendered":""},"modified":"2024-12-27T09:26:59","modified_gmt":"2024-12-27T01:26:59","slug":"python%e5%a6%82%e4%bd%95%e7%bb%93%e6%9e%9c%e6%a8%aa%e5%90%91%e5%b1%95%e5%bc%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/997772.html","title":{"rendered":"python\u5982\u4f55\u7ed3\u679c\u6a2a\u5411\u5c55\u5f00"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073446\/0aaa1885-5052-4c74-b3dc-28acd7750360.webp\" alt=\"python\u5982\u4f55\u7ed3\u679c\u6a2a\u5411\u5c55\u5f00\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u60f3\u8981\u5c06\u7ed3\u679c\u6a2a\u5411\u5c55\u5f00\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\u3001<code>zip<\/code>\u51fd\u6570\u3001<code>pandas<\/code>\u5e93\u3001<code>numpy<\/code>\u5e93\u7b49\u3002\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u7c7b\u578b\u548c\u4f60\u60f3\u8981\u7684\u6700\u7ec8\u683c\u5f0f\u3002<\/strong>\u4f8b\u5982\uff0c\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u591a\u4e2a\u5217\u8868\u7684\u6a2a\u5411\u5c55\u5f00\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u7b80\u5355\u7684\u5217\u8868\u7ec4\u5408\u64cd\u4f5c\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7ed3\u679c\u7684\u6a2a\u5411\u5c55\u5f00\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>zip<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>zip<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u8fed\u4ee3\u5668\u201c\u538b\u7f29\u201d\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u8fed\u4ee3\u5668\u3002\u901a\u8fc7\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u591a\u4e2a\u5217\u8868\u7684\u5143\u7d20\u8fdb\u884c\u6a2a\u5411\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>result = list(zip(list1, list2))<\/p>\n<p>print(result)  # \u8f93\u51fa: [(1, &#39;a&#39;), (2, &#39;b&#39;), (3, &#39;c&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>zip<\/code>\u51fd\u6570\u5c06<code>list1<\/code>\u548c<code>list2<\/code>\u7684\u5143\u7d20\u6210\u5bf9\u7ec4\u5408\uff0c\u5f62\u6210\u4e00\u4e2a\u5143\u7ec4\u7684\u5217\u8868\u3002<code>zip<\/code>\u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u4e8e\u591a\u4e2a\u5217\u8868\uff0c\u5c06\u5b83\u4eec\u7684\u5143\u7d20\u9010\u4e2a\u7ec4\u5408\u5728\u4e00\u8d77\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u52bf\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u7406\u89e3\u3002<\/li>\n<li>\u9002\u5408\u5904\u7406\u591a\u4e2a\u7b49\u957f\u5217\u8868\u7684\u7ec4\u5408\u3002<\/li>\n<\/ul>\n<p><p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><code>zip<\/code>\u51fd\u6570\u4f1a\u5728\u6700\u77ed\u7684\u8f93\u5165\u5217\u8868\u7ed3\u675f\u65f6\u505c\u6b62\uff0c\u56e0\u6b64\u5982\u679c\u8f93\u5165\u5217\u8868\u957f\u5ea6\u4e0d\u4e00\u81f4\uff0c\u90e8\u5206\u6570\u636e\u53ef\u80fd\u4f1a\u4e22\u5931\u3002<\/li>\n<\/ul>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u975e\u5e38Pythonic\u7684\u5199\u6cd5\uff0c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u5c06\u591a\u4e2a\u5217\u8868\u7684\u5143\u7d20\u6a2a\u5411\u7ec4\u5408\uff0c\u5e76\u751f\u6210\u65b0\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]<\/p>\n<p>result = [(x, y) for x, y in zip(list1, list2)]<\/p>\n<p>print(result)  # \u8f93\u51fa: [(1, &#39;a&#39;), (2, &#39;b&#39;), (3, &#39;c&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>zip<\/code>\u51fd\u6570\uff0c\u4e0d\u4ec5\u80fd\u591f\u5b9e\u73b0\u6a2a\u5411\u5c55\u5f00\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u7ec4\u5408\u540e\u7684\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u52bf\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\uff0c\u53ef\u4ee5\u5728\u7ec4\u5408\u7684\u540c\u65f6\u8fdb\u884c\u5143\u7d20\u7684\u53d8\u6362\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u9700\u8981\u5bf9\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5904\u7406\u7684\u6570\u636e\u662f\u8868\u683c\u6570\u636e\uff0c\u6216\u8005\u9700\u8981\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u64cd\u4f5c\uff0c<code>pandas<\/code>\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {&#39;Numbers&#39;: [1, 2, 3], &#39;Letters&#39;: [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   Numbers Letters<\/p>\n<p>0        1       a<\/p>\n<p>1        2       b<\/p>\n<p>2        3       c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u80fd\u591f\u8f7b\u677e\u5730\u5904\u7406\u6570\u636e\u7684\u6a2a\u5411\u7ec4\u5408\uff0c\u5e76\u63d0\u4f9b\u4e30\u5bcc\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u52bf\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u652f\u6301\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u5e93\uff0c\u80fd\u591f\u9ad8\u6548\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><code>pandas<\/code>\u5e93\u9700\u8981\u5355\u72ec\u5b89\u88c5\uff0c\u9002\u5408\u9700\u8981\u9891\u7e41\u8fdb\u884c\u6570\u636e\u5206\u6790\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><p>\u56db\u3001\u4f7f\u7528<code>numpy<\/code>\u5e93<\/p>\n<\/p>\n<p><p><code>numpy<\/code>\u5e93\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u6570\u503c\u578b\u6570\u636e\u3002\u5b83\u80fd\u591f\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\uff0c\u5305\u62ec\u6570\u636e\u7684\u6a2a\u5411\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array1 = np.array([1, 2, 3])<\/p>\n<p>array2 = np.array([4, 5, 6])<\/p>\n<p>result = np.column_stack((array1, array2))<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1 4]<\/p>\n<p> [2 5]<\/p>\n<p> [3 6]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>numpy.column_stack<\/code>\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u6570\u7ec4\u6a2a\u5411\u7ec4\u5408\u6210\u4e00\u4e2a\u65b0\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u52bf\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\u80fd\u529b\u3002<\/li>\n<li>\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u503c\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li><code>numpy<\/code>\u5e93\u9700\u8981\u5355\u72ec\u5b89\u88c5\uff0c\u9002\u5408\u5904\u7406\u6570\u503c\u8ba1\u7b97\u548c\u79d1\u5b66\u8ba1\u7b97\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f\u548c\u9009\u62e9\u5efa\u8bae<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7b80\u5355\u5217\u8868\u7ec4\u5408\uff1a<\/strong> \u5982\u679c\u53ea\u662f\u5bf9\u7b80\u5355\u5217\u8868\u8fdb\u884c\u6a2a\u5411\u7ec4\u5408\uff0c<code>zip<\/code>\u51fd\u6570\u6216\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff1a<\/strong> \u5982\u679c\u9700\u8981\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u503c\u8ba1\u7b97\uff1a<\/strong> \u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u503c\u8ba1\u7b97\u6216\u79d1\u5b66\u8ba1\u7b97\u4efb\u52a1\uff0c<code>numpy<\/code>\u5e93\u662f\u6700\u4f73\u9009\u62e9\uff0c\u80fd\u591f\u63d0\u4f9b\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u590d\u6742\u6027\u548c\u5177\u4f53\u7684\u4efb\u52a1\u9700\u6c42\u3002\u901a\u8fc7\u5408\u7406\u5730\u9009\u62e9\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u6570\u636e\u7684\u6a2a\u5411\u5c55\u5f00\uff0c\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u636e\u7ed3\u679c\u8fdb\u884c\u6a2a\u5411\u5c55\u5f00\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u7ed3\u679c\u6a2a\u5411\u5c55\u5f00\u3002\u5e38\u89c1\u7684\u5e93\u5982Pandas\u63d0\u4f9b\u4e86\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5de5\u5177\u3002\u901a\u8fc7\u4f7f\u7528<code>pivot<\/code>\u6216<code>pivot_table<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u957f\u683c\u5f0f\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5bbd\u683c\u5f0f\uff0c\u65b9\u4fbf\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>unstack()<\/code>\u65b9\u6cd5\u4e5f\u80fd\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\uff0c\u9002\u5408\u4e8e\u591a\u5c42\u7d22\u5f15\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u4f7f\u7528Pandas\u8fdb\u884c\u6a2a\u5411\u5c55\u5f00\u65f6\uff0c\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Pandas\u8fdb\u884c\u6a2a\u5411\u5c55\u5f00\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6570\u636e\u4e2d\u6709\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u4ee5\u907f\u514d\u6570\u636e\u7684\u91cd\u590d\u6216\u4e22\u5931\u3002\u5728\u4f7f\u7528<code>pivot<\/code>\u65b9\u6cd5\u65f6\uff0c\u786e\u4fdd\u6bcf\u4e2a\u7d22\u5f15\u548c\u5217\u7684\u7ec4\u5408\u90fd\u6709\u5bf9\u5e94\u7684\u503c\uff0c\u5426\u5219\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u9009\u62e9\u5408\u9002\u7684\u805a\u5408\u51fd\u6570\u4e5f\u5f88\u91cd\u8981\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5177\u6709\u91cd\u590d\u503c\u7684\u60c5\u51b5\u4e0b\uff0c<code>pivot_table<\/code>\u53ef\u4ee5\u901a\u8fc7<code>aggfunc<\/code>\u53c2\u6570\u81ea\u5b9a\u4e49\u805a\u5408\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u600e\u6837\u4f7f\u7528NumPy\u5b9e\u73b0\u7ed3\u679c\u7684\u6a2a\u5411\u5c55\u5f00\uff1f<\/strong><br \/>NumPy\u867d\u7136\u4e0d\u5982Pandas\u90a3\u4e48\u76f4\u89c2\uff0c\u4f46\u4ecd\u7136\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u7684reshape\u529f\u80fd\u5b9e\u73b0\u6a2a\u5411\u5c55\u5f00\u3002\u9996\u5148\u9700\u8981\u786e\u4fdd\u6570\u636e\u4ee5\u9002\u5408\u7684\u5f62\u72b6\u5b58\u5728\uff0c\u63a5\u7740\u4f7f\u7528<code>reshape()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u91cd\u5851\u4e3a\u6240\u9700\u7684\u5f62\u72b6\u3002\u9700\u6ce8\u610f\u7684\u662f\uff0cNumPy\u7684reshape\u64cd\u4f5c\u8981\u6c42\u65b0\u5f62\u72b6\u7684\u603b\u5143\u7d20\u6570\u91cf\u5fc5\u987b\u4e0e\u539f\u59cb\u6570\u7ec4\u76f8\u540c\uff0c\u56e0\u6b64\u5728\u8fdb\u884c\u8f6c\u6362\u65f6\u8981\u4ed4\u7ec6\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u60f3\u8981\u5c06\u7ed3\u679c\u6a2a\u5411\u5c55\u5f00\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\u5217\u8868\u63a8\u5bfc\u5f0f\u3001zip\u51fd\u6570\u3001pandas\u5e93\u3001numpy\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":997777,"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\/997772"}],"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=997772"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/997772\/revisions"}],"predecessor-version":[{"id":997788,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/997772\/revisions\/997788"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/997777"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=997772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=997772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=997772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}