{"id":962785,"date":"2024-12-27T04:11:33","date_gmt":"2024-12-26T20:11:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/962785.html"},"modified":"2024-12-27T04:11:35","modified_gmt":"2024-12-26T20:11:35","slug":"python%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9%e5%88%97%e5%90%8d%e7%a7%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/962785.html","title":{"rendered":"python\u5982\u4f55\u4fee\u6539\u5217\u540d\u79f0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104640\/e929e600-03a8-4d2e-b4c7-ff9d7fd5a1a2.webp\" alt=\"python\u5982\u4f55\u4fee\u6539\u5217\u540d\u79f0\" \/><\/p>\n<p><p> \u8981\u4fee\u6539Python\u4e2d\u7684\u5217\u540d\u79f0\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002<strong>\u901a\u5e38\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001\u91cd\u547d\u540d\u5355\u4e2a\u5217\u6216\u591a\u4e2a\u5217\u3001\u901a\u8fc7\u5b57\u5178\u6620\u5c04\u8fdb\u884c\u91cd\u547d\u540d<\/strong>\u3002\u5176\u4e2d\uff0cpandas\u5e93\u662f\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5c24\u5176\u662f\u5728\u6570\u636e\u6e05\u6d17\u548c\u9884\u5904\u7406\u9636\u6bb5\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4fee\u6539\u5217\u540d\u79f0\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u7528\u7684\u6280\u5de7\u548c\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PANDAS\u5e93\u91cd\u547d\u540d\u5217<\/p>\n<\/p>\n<p><p>pandas\u5e93\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u7684\u9996\u9009\u5de5\u5177\u4e4b\u4e00\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\u6765\u91cd\u547d\u540dDataFrame\u4e2d\u7684\u5217\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>rename()<\/code>\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p><code>rename()<\/code>\u65b9\u6cd5\u662fpandas\u63d0\u4f9b\u7684\u7528\u4e8e\u91cd\u547d\u540d\u5217\u6216\u7d22\u5f15\u7684\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5b57\u5178\u6765\u91cd\u547d\u540d\u5217\uff0c\u5176\u4e2d\u5b57\u5178\u7684\u952e\u662f\u65e7\u5217\u540d\uff0c\u503c\u662f\u65b0\u5217\u540d\u3002<\/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], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528rename()\u65b9\u6cd5\u91cd\u547d\u540d\u5217<\/strong><\/h2>\n<p>df.rename(columns={&#39;A&#39;: &#39;a&#39;, &#39;B&#39;: &#39;b&#39;}, inplace=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684DataFrame\uff0c\u5e76\u4f7f\u7528<code>rename()<\/code>\u65b9\u6cd5\u5c06\u5217\u540d\u79f0\u4ece&#39;A&#39;\u548c&#39;B&#39;\u66f4\u6539\u4e3a&#39;a&#39;\u548c&#39;b&#39;\u3002\u8bbe\u7f6e<code>inplace=True<\/code>\u4f1a\u76f4\u63a5\u4fee\u6539\u539f\u59cbDataFrame\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528<code>columns<\/code>\u5c5e\u6027<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4f60\u60f3\u91cd\u547d\u540d\u6240\u6709\u5217\uff0c\u53e6\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u4f7f\u7528DataFrame\u7684<code>columns<\/code>\u5c5e\u6027\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5217\u6570\u8f83\u5c11\u7684\u60c5\u51b5\u3002<\/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], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528columns\u5c5e\u6027\u91cd\u547d\u540d\u6240\u6709\u5217<\/strong><\/h2>\n<p>df.columns = [&#39;a&#39;, &#39;b&#39;]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u5c06DataFrame\u7684<code>columns<\/code>\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u8fd9\u4e2a\u5217\u8868\u5305\u542b\u4e86\u65b0\u7684\u5217\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u91cd\u547d\u540d\u5355\u4e2a\u5217<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u53ea\u9700\u8981\u91cd\u547d\u540dDataFrame\u4e2d\u7684\u5355\u4e2a\u5217\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u7684\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>rename()<\/code>\u65b9\u6cd5\u91cd\u547d\u540d\u5355\u4e2a\u5217<\/li>\n<\/ol>\n<p><p>\u524d\u9762\u63d0\u5230\u7684<code>rename()<\/code>\u65b9\u6cd5\u540c\u6837\u9002\u7528\u4e8e\u91cd\u547d\u540d\u5355\u4e2a\u5217\u3002<\/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], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528rename()\u65b9\u6cd5\u91cd\u547d\u540d\u5355\u4e2a\u5217<\/strong><\/h2>\n<p>df.rename(columns={&#39;A&#39;: &#39;alpha&#39;}, inplace=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u76f4\u63a5\u4fee\u6539<code>columns<\/code>\u5c5e\u6027<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u4f60\u77e5\u9053\u5217\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u4fee\u6539<code>columns<\/code>\u5c5e\u6027\u6765\u91cd\u547d\u540d\u5355\u4e2a\u5217\u3002<\/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], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u76f4\u63a5\u4fee\u6539columns\u5c5e\u6027\u91cd\u547d\u540d\u5355\u4e2a\u5217<\/strong><\/h2>\n<p>df.columns.values[0] = &#39;alpha&#39;<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u901a\u8fc7\u5b57\u5178\u6620\u5c04\u8fdb\u884c\u6279\u91cf\u91cd\u547d\u540d<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u91cd\u547d\u540d\u591a\u4e2a\u5217\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u6620\u5c04\u8fdb\u884c\u6279\u91cf\u5904\u7406\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u3002<\/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], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\u6620\u5c04<\/strong><\/h2>\n<p>column_mapping = {&#39;A&#39;: &#39;alpha&#39;, &#39;B&#39;: &#39;beta&#39;, &#39;C&#39;: &#39;gamma&#39;}<\/p>\n<h2><strong>\u4f7f\u7528rename()\u65b9\u6cd5\u548c\u5b57\u5178\u6620\u5c04\u91cd\u547d\u540d\u5217<\/strong><\/h2>\n<p>df.rename(columns=column_mapping, inplace=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b57\u5178<code>column_mapping<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u65e7\u5217\u540d\u548c\u65b0\u5217\u540d\u4e4b\u95f4\u7684\u6620\u5c04\u5173\u7cfb\u3002\u7136\u540e\u4f7f\u7528<code>rename()<\/code>\u65b9\u6cd5\u8fdb\u884c\u6279\u91cf\u91cd\u547d\u540d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u5b9e\u7528\u6280\u5de7<\/p>\n<\/p>\n<ol>\n<li><strong>\u68c0\u67e5\u5217\u540d\u79f0<\/strong>\uff1a\u5728\u5f00\u59cb\u91cd\u547d\u540d\u524d\uff0c\u68c0\u67e5DataFrame\u7684\u5217\u540d\u79f0\u4ee5\u907f\u514d\u9519\u8bef\u3002<\/li>\n<\/ol>\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], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6253\u5370\u5217\u540d\u79f0<\/strong><\/h2>\n<p>print(df.columns)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\n<p><strong>\u907f\u514d\u5217\u540d\u51b2\u7a81<\/strong>\uff1a\u786e\u4fdd\u65b0\u5217\u540d\u79f0\u4e0d\u4e0e\u73b0\u6709\u5217\u540d\u79f0\u51b2\u7a81\uff0c\u4ee5\u514d\u9020\u6210\u6570\u636e\u6df7\u6dc6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u6301\u4e00\u81f4\u6027<\/strong>\uff1a\u5728\u6574\u4e2a\u9879\u76ee\u4e2d\u4fdd\u6301\u5217\u540d\u79f0\u7684\u4e00\u81f4\u6027\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u6ce8\u91ca<\/strong>\uff1a\u5f53\u91cd\u547d\u540d\u5217\u65f6\uff0c\u4f7f\u7528\u6ce8\u91ca\u89e3\u91ca\u4e3a\u4ec0\u4e48\u9700\u8981\u91cd\u547d\u540d\uff0c\u4ee5\u5e2e\u52a9\u5176\u4ed6\u5f00\u53d1\u8005\u7406\u89e3\u4ee3\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u4fee\u6539\u5217\u540d\u79f0\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\u5c06\u5927\u5927\u63d0\u9ad8\u4f60\u7684\u6570\u636e\u5904\u7406\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002\u65e0\u8bba\u662f\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\u8fd8\u662f\u5927\u578b\u6570\u636e\u96c6\uff0cpandas\u5e93\u63d0\u4f9b\u7684\u7075\u6d3b\u6027\u548c\u529f\u80fd\u6027\u90fd\u5c06\u5e2e\u52a9\u4f60\u8f7b\u677e\u5b9e\u73b0\u5217\u540d\u79f0\u7684\u4fee\u6539\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u66f4\u6539DataFrame\u7684\u5217\u540d\u79f0\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u8f7b\u677e\u4fee\u6539DataFrame\u7684\u5217\u540d\u79f0\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Pandas\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7<code>pd.DataFrame.rename()<\/code>\u65b9\u6cd5\u6765\u4fee\u6539\u5217\u540d\u79f0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u6709\u4e00\u4e2aDataFrame\u5e76\u60f3\u5c06\u5217\u540d\u201c\u65e7\u5217\u540d\u201d\u66f4\u6539\u4e3a\u201c\u65b0\u5217\u540d\u201d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndf = pd.DataFrame({&#39;\u65e7\u5217\u540d&#39;: [1, 2, 3]})\ndf.rename(columns={&#39;\u65e7\u5217\u540d&#39;: &#39;\u65b0\u5217\u540d&#39;}, inplace=True)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u6210\u529f\u5730\u5c06\u5217\u540d\u79f0\u66f4\u6539\u4e3a\u65b0\u540d\u79f0\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u662f\u5426\u53ef\u4ee5\u540c\u65f6\u66f4\u6539\u591a\u4e2a\u5217\u7684\u540d\u79f0\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u4f7f\u7528<code>rename()<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u4e00\u6b21\u6027\u66f4\u6539\u591a\u4e2a\u5217\u7684\u540d\u79f0\u3002\u6bd4\u5982\uff0c\u5982\u679c\u60f3\u5c06\u201c\u5217A\u201d\u548c\u201c\u5217B\u201d\u5206\u522b\u6539\u4e3a\u201c\u65b0\u5217A\u201d\u548c\u201c\u65b0\u5217B\u201d\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python\">df.rename(columns={&#39;\u5217A&#39;: &#39;\u65b0\u5217A&#39;, &#39;\u5217B&#39;: &#39;\u65b0\u5217B&#39;}, inplace=True)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5316\u4e86\u591a\u4e2a\u5217\u540d\u79f0\u7684\u4fee\u6539\uff0c\u8282\u7701\u4e86\u65f6\u95f4\u548c\u4ee3\u7801\u884c\u6570\u3002<\/p>\n<p><strong>\u5728\u4fee\u6539\u5217\u540d\u79f0\u540e\uff0c\u5982\u4f55\u67e5\u770b\u5f53\u524d\u7684\u5217\u540d\u5217\u8868\uff1f<\/strong><br \/>\u4fee\u6539\u5217\u540d\u79f0\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528DataFrame\u7684<code>columns<\/code>\u5c5e\u6027\u6765\u67e5\u770b\u5f53\u524d\u7684\u5217\u540d\u3002\u53ea\u9700\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">print(df.columns)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u4e00\u4e2a\u5305\u542b\u5f53\u524d\u6240\u6709\u5217\u540d\u79f0\u7684\u5217\u8868\uff0c\u65b9\u4fbf\u60a8\u786e\u8ba4\u66f4\u6539\u662f\u5426\u6210\u529f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4fee\u6539Python\u4e2d\u7684\u5217\u540d\u79f0\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u901a\u5e38\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001\u91cd\u547d\u540d\u5355\u4e2a\u5217\u6216\u591a\u4e2a\u5217\u3001\u901a\u8fc7\u5b57 [&hellip;]","protected":false},"author":3,"featured_media":962796,"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\/962785"}],"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=962785"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962785\/revisions"}],"predecessor-version":[{"id":962797,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/962785\/revisions\/962797"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/962796"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=962785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=962785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=962785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}