{"id":1054526,"date":"2024-12-31T14:43:04","date_gmt":"2024-12-31T06:43:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1054526.html"},"modified":"2024-12-31T14:43:07","modified_gmt":"2024-12-31T06:43:07","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e7%bb%93%e6%9e%9c%e5%88%97%e6%8d%a2%e6%88%90%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1054526.html","title":{"rendered":"Python\u5982\u4f55\u5c06\u7ed3\u679c\u5217\u6362\u6210\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5845723e-5084-4691-afbb-4b06d572a48a.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"Python\u5982\u4f55\u5c06\u7ed3\u679c\u5217\u6362\u6210\u884c\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u7ed3\u679c\u5217\u8868\u8f6c\u6362\u4e3a\u884c\u683c\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528zip\u3001numpy\u3001pandas\u7b49\u3002<\/strong> \u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528Python\u5185\u7f6e\u7684<code>zip<\/code>\u51fd\u6570\u5c06\u591a\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u884c\u683c\u5f0f\u3002\u901a\u8fc7\u5c06\u591a\u4e2a\u5217\u8868\u4f20\u9012\u7ed9<code>zip<\/code>\u51fd\u6570\uff0c\u7136\u540e\u4f7f\u7528<code>*<\/code>\u8fd0\u7b97\u7b26\u89e3\u5305\u8fd9\u4e9b\u5217\u8868\uff0c\u53ef\u4ee5\u5b9e\u73b0\u884c\u5217\u8f6c\u6362\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528<code>numpy<\/code>\u5e93\u7684<code>transpose<\/code>\u65b9\u6cd5\u6765\u8f6c\u6362\u6570\u7ec4\u7684\u7ef4\u5ea6\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>transpose<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u7684\u6b65\u9aa4\u53ca\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u5c06\u591a\u4e2a\u5217\u8868\u8f6c\u6362\u4e3a\u884c\u683c\u5f0f\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u591a\u4e2a\u5217\u8868\uff0c\u6bcf\u4e2a\u5217\u8868\u4ee3\u8868\u4e00\u5217\u6570\u636e\u3002<\/li>\n<li>\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u548c<code>*<\/code>\u8fd0\u7b97\u7b26\u5c06\u8fd9\u4e9b\u5217\u8868\u89e3\u5305\u5e76\u8f6c\u6362\u4e3a\u884c\u683c\u5f0f\u3002<\/li>\n<li>\u5c06\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u65b0\u7684\u5217\u8868\u4e2d\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u591a\u4e2a\u5217\u8868\uff0c\u6bcf\u4e2a\u5217\u8868\u4ee3\u8868\u4e00\u5217\u6570\u636e<\/p>\n<p>col1 = [1, 2, 3]<\/p>\n<p>col2 = [4, 5, 6]<\/p>\n<p>col3 = [7, 8, 9]<\/p>\n<h2><strong>\u4f7f\u7528zip\u51fd\u6570\u548c*\u8fd0\u7b97\u7b26\u5c06\u8fd9\u4e9b\u5217\u8868\u89e3\u5305\u5e76\u8f6c\u6362\u4e3a\u884c\u683c\u5f0f<\/strong><\/h2>\n<p>rows = list(zip(*[col1, col2, col3]))<\/p>\n<h2><strong>\u6253\u5370\u8f6c\u6362\u540e\u7684\u7ed3\u679c<\/strong><\/h2>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>(1, 4, 7)<\/p>\n<p>(2, 5, 8)<\/p>\n<p>(3, 6, 9)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528zip\u51fd\u6570\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><code>zip<\/code>\u51fd\u6570\u662fPython\u5185\u7f6e\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5408\u5e76\u4e3a\u4e00\u4e2a\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\u3002\u901a\u8fc7\u4f7f\u7528<code>*<\/code>\u8fd0\u7b97\u7b26\u89e3\u5305\u5217\u8868\uff0c\u53ef\u4ee5\u5c06\u5217\u8f6c\u6362\u4e3a\u884c\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u591a\u4e2a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u591a\u4e2a\u5217\u8868\uff0c\u6bcf\u4e2a\u5217\u8868\u4ee3\u8868\u4e00\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">col1 = [1, 2, 3]<\/p>\n<p>col2 = [4, 5, 6]<\/p>\n<p>col3 = [7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528zip\u51fd\u6570\u548c*\u8fd0\u7b97\u7b26\u89e3\u5305\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u548c<code>*<\/code>\u8fd0\u7b97\u7b26\u89e3\u5305\u8fd9\u4e9b\u5217\u8868\uff0c\u5c06\u5217\u8f6c\u6362\u4e3a\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = list(zip(*[col1, col2, col3]))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6253\u5370\u8f6c\u6362\u540e\u7684\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u5c06\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u65b0\u7684\u5217\u8868\u4e2d\uff0c\u5e76\u6253\u5370\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528numpy\u5e93\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u662fPython\u7684\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u901a\u8fc7\u4f7f\u7528<code>numpy<\/code>\u7684<code>transpose<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u884c\u5217\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165numpy\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165<code>numpy<\/code>\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5<code>numpy<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\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><h4>2. \u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy<\/code>\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6bcf\u4e00\u5217\u4ee3\u8868\u4e00\u4e2a\u5217\u8868\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([[1, 2, 3],<\/p>\n<p>                  [4, 5, 6],<\/p>\n<p>                  [7, 8, 9]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528transpose\u65b9\u6cd5\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>numpy<\/code>\u7684<code>transpose<\/code>\u65b9\u6cd5\u5c06\u6570\u7ec4\u7684\u7ef4\u5ea6\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transposed_array = np.transpose(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u6253\u5370\u8f6c\u6362\u540e\u7684\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u5c06\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u65b0\u7684\u6570\u7ec4\u4e2d\uff0c\u5e76\u6253\u5370\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in transposed_array:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas\u5e93\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u662fPython\u7684\u4e00\u4e2a\u6570\u636e\u5206\u6790\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u6570\u636e\u6846\u548c\u7cfb\u5217\u6570\u636e\u3002\u901a\u8fc7\u4f7f\u7528<code>pandas<\/code>\u7684<code>transpose<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u884c\u5217\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165<code>pandas<\/code>\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5<code>pandas<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\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><h4>2. \u521b\u5efa\u4e00\u4e2aDataFrame<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pandas<\/code>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846\uff0c\u6bcf\u4e00\u5217\u4ee3\u8868\u4e00\u4e2a\u5217\u8868\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;col1&#39;: [1, 2, 3],<\/p>\n<p>    &#39;col2&#39;: [4, 5, 6],<\/p>\n<p>    &#39;col3&#39;: [7, 8, 9]<\/p>\n<p>})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528transpose\u65b9\u6cd5\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>pandas<\/code>\u7684<code>transpose<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u6846\u7684\u7ef4\u5ea6\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transposed_df = df.transpose()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u6253\u5370\u8f6c\u6362\u540e\u7684\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u5c06\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u65b0\u7684\u6570\u636e\u6846\u4e2d\uff0c\u5e76\u6253\u5370\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(transposed_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u3001<code>numpy<\/code>\u548c<code>pandas<\/code>\u5e93\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u89e3\u6790\u7684\u65b9\u5f0f\u8fdb\u884c\u884c\u5217\u8f6c\u6362\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u8f83\u5c0f\u7684\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u591a\u4e2a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u591a\u4e2a\u5217\u8868\uff0c\u6bcf\u4e2a\u5217\u8868\u4ee3\u8868\u4e00\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">col1 = [1, 2, 3]<\/p>\n<p>col2 = [4, 5, 6]<\/p>\n<p>col3 = [7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5217\u8868\u89e3\u6790\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5217\u8868\u89e3\u6790\u7684\u65b9\u5f0f\uff0c\u5c06\u5217\u8f6c\u6362\u4e3a\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = [[col1[i], col2[i], col3[i]] for i in range(len(col1))]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6253\u5370\u8f6c\u6362\u540e\u7684\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u5c06\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u65b0\u7684\u5217\u8868\u4e2d\uff0c\u5e76\u6253\u5370\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528itertools\u5e93\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><code>itertools<\/code>\u662fPython\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u7528\u4e8e\u64cd\u4f5c\u8fed\u4ee3\u5668\u7684\u51fd\u6570\u3002\u901a\u8fc7\u4f7f\u7528<code>itertools<\/code>\u5e93\u4e2d\u7684<code>zip_longest<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5904\u7406\u957f\u5ea6\u4e0d\u7b49\u7684\u5217\u8868\uff0c\u5e76\u8fdb\u884c\u884c\u5217\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165itertools\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165<code>itertools<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u591a\u4e2a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u591a\u4e2a\u5217\u8868\uff0c\u6bcf\u4e2a\u5217\u8868\u4ee3\u8868\u4e00\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">col1 = [1, 2, 3]<\/p>\n<p>col2 = [4, 5, 6]<\/p>\n<p>col3 = [7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4f7f\u7528zip_longest\u51fd\u6570\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>itertools<\/code>\u5e93\u4e2d\u7684<code>zip_longest<\/code>\u51fd\u6570\uff0c\u5c06\u5217\u8f6c\u6362\u4e3a\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = list(itertools.zip_longest(col1, col2, col3, fillvalue=None))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u6253\u5370\u8f6c\u6362\u540e\u7684\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u5c06\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u65b0\u7684\u5217\u8868\u4e2d\uff0c\u5e76\u6253\u5370\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528csv\u6a21\u5757\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h3>\n<\/p>\n<p><p><code>csv<\/code>\u6a21\u5757\u662fPython\u7684\u4e00\u4e2a\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165CSV\u6587\u4ef6\u7684\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5c06CSV\u6587\u4ef6\u4e2d\u7684\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u884c\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165csv\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165<code>csv<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u4e00\u4e2aCSV\u6587\u4ef6\u5e76\u5199\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aCSV\u6587\u4ef6\uff0c\u5e76\u5199\u5165\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;data.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([1, 2, 3])<\/p>\n<p>    writer.writerow([4, 5, 6])<\/p>\n<p>    writer.writerow([7, 8, 9])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6CSV\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u8fdb\u884c\u884c\u5217\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;data.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    columns = list(reader)<\/p>\n<p>    rows = list(zip(*columns))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u6253\u5370\u8f6c\u6362\u540e\u7684\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u5c06\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u65b0\u7684\u5217\u8868\u4e2d\uff0c\u5e76\u6253\u5370\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5d4c\u5957\u5faa\u73af\u662f\u4e00\u79cd\u57fa\u7840\u7684\u7f16\u7a0b\u6280\u5de7\uff0c\u53ef\u4ee5\u7528\u4e8e\u904d\u5386\u548c\u5904\u7406\u591a\u7ef4\u6570\u636e\u3002\u901a\u8fc7\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\uff0c\u53ef\u4ee5\u5c06\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u884c\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u591a\u4e2a\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u591a\u4e2a\u5217\u8868\uff0c\u6bcf\u4e2a\u5217\u8868\u4ee3\u8868\u4e00\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">col1 = [1, 2, 3]<\/p>\n<p>col2 = [4, 5, 6]<\/p>\n<p>col3 = [7, 8, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u8fdb\u884c\u884c\u5217\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\uff0c\u5c06\u5217\u6570\u636e\u8f6c\u6362\u4e3a\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rows = []<\/p>\n<p>for i in range(len(col1)):<\/p>\n<p>    row = []<\/p>\n<p>    for col in [col1, col2, col3]:<\/p>\n<p>        row.append(col[i])<\/p>\n<p>    rows.append(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6253\u5370\u8f6c\u6362\u540e\u7684\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u5c06\u8f6c\u6362\u540e\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u65b0\u7684\u5217\u8868\u4e2d\uff0c\u5e76\u6253\u5370\u6bcf\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u7ed3\u679c\u5217\u8868\u8f6c\u6362\u4e3a\u884c\u683c\u5f0f\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u4e03\u79cd\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u3001<code>numpy<\/code>\u5e93\u3001<code>pandas<\/code>\u5e93\u3001\u5217\u8868\u89e3\u6790\u3001<code>itertools<\/code>\u5e93\u3001<code>csv<\/code>\u6a21\u5757\u548c\u5d4c\u5957\u5faa\u73af\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u4e2d\u7ed3\u679c\u5217\u8f6c\u6362\u4e3a\u884c\u7684\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u636e\u6846\u7684\u5217\u8f6c\u6362\u4e3a\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u8f7b\u677e\u5730\u5c06\u6570\u636e\u6846\u7684\u5217\u8f6c\u6362\u4e3a\u884c\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u4f7f\u7528<code>melt()<\/code>\u51fd\u6570\u6216<code>transpose()<\/code>\u65b9\u6cd5\u3002<code>melt()<\/code>\u51fd\u6570\u9002\u5408\u5c06\u591a\u4e2a\u5217\u8f6c\u6362\u4e3a\u4e24\u5217\u7684\u5f62\u5f0f\uff0c\u800c<code>transpose()<\/code>\u5219\u53ef\u4ee5\u5c06\u6574\u4e2a\u6570\u636e\u6846\u8fdb\u884c\u8f6c\u7f6e\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u884c\u5217\u4e92\u6362\u3002<\/p>\n<p><strong>\u5728Pandas\u4e2d\u8f6c\u7f6e\u6570\u636e\u6846\u7684\u6027\u80fd\u5982\u4f55\uff1f<\/strong><br \/>Pandas\u4e2d\u7684\u8f6c\u7f6e\u64cd\u4f5c\u5728\u5904\u7406\u5c0f\u578b\u6570\u636e\u6846\u65f6\u975e\u5e38\u9ad8\u6548\uff0c\u4f46\u968f\u7740\u6570\u636e\u89c4\u6a21\u7684\u589e\u5927\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u5f71\u54cd\u3002\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528\u5206\u5757\u5904\u7406\u7684\u65b9\u6cd5\uff0c\u6216\u8005\u8003\u8651\u6570\u636e\u7684\u5b58\u50a8\u548c\u64cd\u4f5c\u65b9\u5f0f\uff0c\u4ee5\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6Python\u5e93\u53ef\u4ee5\u5b9e\u73b0\u5217\u8f6c\u884c\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u9664\u4e86Pandas\uff0cNumPy\u4e5f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u7ec4\u7684\u91cd\u5851\u529f\u80fd\u5b9e\u73b0\u5217\u8f6c\u884c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy.reshape()<\/code>\u6216<code>numpy.transpose()<\/code>\u53ef\u4ee5\u8fbe\u5230\u76f8\u4f3c\u6548\u679c\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u6570\u503c\u6570\u636e\u65f6\uff0cNumPy\u7684\u6027\u80fd\u901a\u5e38\u4f18\u4e8ePandas\u3002\u6839\u636e\u6570\u636e\u7684\u6027\u8d28\u9009\u62e9\u5408\u9002\u7684\u5e93\u4f1a\u66f4\u6709\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u7ed3\u679c\u5217\u8868\u8f6c\u6362\u4e3a\u884c\u683c\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528zip\u3001numpy\u3001pandas\u7b49\u3002 \u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1054538,"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\/1054526"}],"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=1054526"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054526\/revisions"}],"predecessor-version":[{"id":1054543,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1054526\/revisions\/1054543"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1054538"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1054526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1054526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1054526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}