{"id":1093914,"date":"2025-01-08T14:35:49","date_gmt":"2025-01-08T06:35:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1093914.html"},"modified":"2025-01-08T14:35:51","modified_gmt":"2025-01-08T06:35:51","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%88%97%e8%bd%ac%e6%8d%a2%e4%b8%ba%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1093914.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5217\u8f6c\u6362\u4e3a\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210304\/0351821d-ea16-4673-8896-972742b51f50.webp\" alt=\"python\u5982\u4f55\u5c06\u5217\u8f6c\u6362\u4e3a\u884c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u5217\u8f6c\u6362\u4e3a\u884c<\/strong>\uff0c\u4f8b\u5982\u4f7f\u7528Pandas\u5e93\u7684<code>transpose()<\/code>\u65b9\u6cd5\u3001<code>pivot()<\/code>\u65b9\u6cd5\u3001\u4ee5\u53caNumPy\u5e93\u7684<code>transpose()<\/code>\u51fd\u6570\u3002<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7Pandas\u5e93<\/strong>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002<strong>\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Pandas\u5e93\u5c06\u5217\u8f6c\u6362\u4e3a\u884c<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Pandas\u7684transpose()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u7684<code>transpose()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06DataFrame\u7684\u884c\u548c\u5217\u8fdb\u884c\u4e92\u6362\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u5c0f\u578b\u548c\u4e2d\u578b\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\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><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>transpose()<\/code>\u65b9\u6cd5\u5c06\u5217\u8f6c\u6362\u4e3a\u884c\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3],<\/p>\n<p>        &#39;B&#39;: [4, 5, 6],<\/p>\n<p>        &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame:&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4f7f\u7528transpose()\u65b9\u6cd5\u5c06\u5217\u8f6c\u6362\u4e3a\u884c<\/strong><\/h2>\n<p>df_transposed = df.transpose()<\/p>\n<p>print(&quot;\\n\u8f6c\u6362\u540e\u7684DataFrame:&quot;)<\/p>\n<p>print(df_transposed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u5217\u7684\u6570\u636e\u6846\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>transpose()<\/code>\u65b9\u6cd5\u5c06\u5176\u5217\u8f6c\u6362\u4e3a\u884c\u3002\u6700\u7ec8\u7684\u8f93\u51fa\u663e\u793a\u4e86\u8f6c\u6362\u540e\u7684DataFrame\uff0c\u5176\u4e2d\u539f\u6765\u7684\u5217\u540d\u53d8\u6210\u4e86\u884c\u7d22\u5f15\uff0c\u539f\u6765\u7684\u884c\u7d22\u5f15\u53d8\u6210\u4e86\u5217\u540d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas\u7684pivot()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>pivot()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u7279\u5b9a\u7684\u5217\u8f6c\u6362\u4e3a\u884c\uff0c\u4f46\u5b83\u901a\u5e38\u7528\u4e8e\u5c06\u957f\u683c\u5f0f\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u5bbd\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>pivot()<\/code>\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;index&#39;: [&#39;row1&#39;, &#39;row2&#39;, &#39;row3&#39;],<\/p>\n<p>        &#39;variable&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;],<\/p>\n<p>        &#39;value&#39;: [1, 2, 3]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(&quot;\u539f\u59cbDataFrame:&quot;)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u4f7f\u7528pivot()\u65b9\u6cd5\u5c06\u5217\u8f6c\u6362\u4e3a\u884c<\/strong><\/h2>\n<p>df_pivoted = df.pivot(index=&#39;index&#39;, columns=&#39;variable&#39;, values=&#39;value&#39;)<\/p>\n<p>print(&quot;\\n\u8f6c\u6362\u540e\u7684DataFrame:&quot;)<\/p>\n<p>print(df_pivoted)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5217\u7684\u957f\u683c\u5f0fDataFrame\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>pivot()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5bbd\u683c\u5f0f\uff0c\u5176\u4e2d<code>index<\/code>\u5217\u4e2d\u7684\u503c\u7528\u4f5c\u884c\u7d22\u5f15\uff0c<code>variable<\/code>\u5217\u4e2d\u7684\u503c\u7528\u4f5c\u5217\u6807\u7b7e\uff0c<code>value<\/code>\u5217\u4e2d\u7684\u503c\u4f5c\u4e3aDataFrame\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528NumPy\u7684transpose()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u7684<code>transpose()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u6570\u7ec4\u7684\u884c\u548c\u5217\u8fdb\u884c\u4e92\u6362\u3002\u867d\u7136NumPy\u4e3b\u8981\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5NumPy\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\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><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>transpose()<\/code>\u51fd\u6570\u5c06\u5217\u8f6c\u6362\u4e3a\u884c\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([[1, 2, 3],<\/p>\n<p>                 [4, 5, 6],<\/p>\n<p>                 [7, 8, 9]])<\/p>\n<p>print(&quot;\u539f\u59cb\u6570\u7ec4:&quot;)<\/p>\n<p>print(data)<\/p>\n<h2><strong>\u4f7f\u7528transpose()\u51fd\u6570\u5c06\u5217\u8f6c\u6362\u4e3a\u884c<\/strong><\/h2>\n<p>data_transposed = np.transpose(data)<\/p>\n<p>print(&quot;\\n\u8f6c\u6362\u540e\u7684\u6570\u7ec4:&quot;)<\/p>\n<p>print(data_transposed)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u89e3\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5217\u7684\u4e8c\u7ef4\u6570\u7ec4\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>transpose()<\/code>\u51fd\u6570\u5c06\u5176\u5217\u8f6c\u6362\u4e3a\u884c\u3002\u6700\u7ec8\u7684\u8f93\u51fa\u663e\u793a\u4e86\u8f6c\u6362\u540e\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u539f\u6765\u7684\u5217\u53d8\u6210\u4e86\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u5c06\u5217\u8f6c\u6362\u4e3a\u884c\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u7684transpose()\u65b9\u6cd5\u3001pivot()\u65b9\u6cd5\u4ee5\u53caNumPy\u5e93\u7684transpose()\u51fd\u6570<\/strong>\u3002<strong>Pandas\u5e93\u7684transpose()\u65b9\u6cd5\u662f\u6700\u5e38\u7528\u548c\u6700\u7075\u6d3b\u7684<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<strong>pivot()\u65b9\u6cd5\u9002\u7528\u4e8e\u5c06\u957f\u683c\u5f0f\u6570\u636e\u8f6c\u6362\u4e3a\u5bbd\u683c\u5f0f\u6570\u636e<\/strong>\uff0c\u800cNumPy\u5e93\u7684transpose()\u51fd\u6570\u5219\u9002\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\u573a\u666f\u3002<strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06DataFrame\u7684\u5217\u8f6c\u6362\u4e3a\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u8f7b\u677e\u5730\u5c06DataFrame\u7684\u5217\u8f6c\u6362\u4e3a\u884c\u3002\u4f7f\u7528<code>melt()<\/code>\u51fd\u6570\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u5c06\u6307\u5b9a\u7684\u5217\u8f6c\u6362\u4e3a\u884c\u683c\u5f0f\uff0c\u4f7f\u5f97\u6570\u636e\u66f4\u52a0\u7075\u6d3b\u4e14\u6613\u4e8e\u5206\u6790\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5217\u7684DataFrame\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9a\u9700\u8981\u8f6c\u6362\u7684\u5217\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u884c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5217\u8f6c\u884c\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>melt()<\/code>\u51fd\u6570\uff0cpandas\u5e93\u8fd8\u63d0\u4f9b\u4e86<code>stack()<\/code>\u65b9\u6cd5\u3002<code>stack()<\/code>\u4f1a\u5c06DataFrame\u7684\u5217\u5806\u53e0\u5230\u884c\u4e0a\uff0c\u5f62\u6210\u4e00\u4e2a\u5c42\u6b21\u5316\u7684\u7d22\u5f15\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>transpose()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u5217\u4e0e\u884c\u7684\u4e92\u6362\uff0c\u4f46\u7ed3\u679c\u901a\u5e38\u662f\u4e00\u4e2a\u65b0\u7684DataFrame\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u8f6c\u6362\u4e3a\u884c\u683c\u5f0f\u3002<\/p>\n<p><strong>\u8f6c\u6362\u540e\u6570\u636e\u7684\u7d22\u5f15\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>melt()<\/code>\u6216<code>stack()<\/code>\u51fd\u6570\u8fdb\u884c\u5217\u8f6c\u884c\u64cd\u4f5c\u65f6\uff0c\u539f\u6709\u7684\u7d22\u5f15\u4f1a\u88ab\u4fdd\u7559\uff0c\u65b0\u7684\u884c\u5c06\u4f1a\u6839\u636e\u6307\u5b9a\u7684\u53c2\u6570\u751f\u6210\u65b0\u7684\u7d22\u5f15\u3002\u5982\u679c\u9700\u8981\u91cd\u65b0\u8bbe\u7f6e\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528<code>reset_index()<\/code>\u65b9\u6cd5\u6765\u91cd\u7f6e\u7d22\u5f15\uff0c\u786e\u4fdd\u6570\u636e\u7684\u6574\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u540e\u7eed\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u5217\u8f6c\u6362\u4e3a\u884c\uff0c\u4f8b\u5982\u4f7f\u7528Pandas\u5e93\u7684transpose()\u65b9\u6cd5\u3001pivo [&hellip;]","protected":false},"author":3,"featured_media":1093920,"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\/1093914"}],"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=1093914"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093914\/revisions"}],"predecessor-version":[{"id":1093921,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093914\/revisions\/1093921"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1093920"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1093914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1093914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1093914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}