{"id":1092151,"date":"2025-01-08T14:18:29","date_gmt":"2025-01-08T06:18:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1092151.html"},"modified":"2025-01-08T14:18:33","modified_gmt":"2025-01-08T06:18:33","slug":"python%e6%95%b0%e6%8d%ae%e6%a1%86%e5%a6%82%e4%bd%95%e5%8a%a0%e4%b8%80%e5%88%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1092151.html","title":{"rendered":"python\u6570\u636e\u6846\u5982\u4f55\u52a0\u4e00\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205317\/cffed381-164b-4c31-ba0e-f1a58df299d7.webp\" alt=\"python\u6570\u636e\u6846\u5982\u4f55\u52a0\u4e00\u5217\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u5904\u7406\u6570\u636e\u6846\uff0c\u5e76\u4e14\u975e\u5e38\u5bb9\u6613\u5730\u5411\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u4e00\u5217\u3002<strong>\u901a\u8fc7\u76f4\u63a5\u7ed9\u6570\u636e\u6846\u6307\u5b9a\u65b0\u5217\u540d\u3001\u4f7f\u7528assign\u65b9\u6cd5\u3001\u4f7f\u7528insert\u65b9\u6cd5\u7b49\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u76f4\u63a5\u7ed9\u6570\u636e\u6846\u6307\u5b9a\u65b0\u5217\u540d<\/strong>\u662f\u4e00\u79cd\u7b80\u5355\u800c\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8d4b\u503c\u64cd\u4f5c\u6765\u5411\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u65b0\u5217\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846df\uff0c\u5e76\u4e14\u4f60\u60f3\u8981\u6dfb\u52a0\u4e00\u5217\u540d\u4e3a&#39;new_column&#39;\u7684\u5217\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/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\u7ed9\u6570\u636e\u6846\u6307\u5b9a\u65b0\u5217\u540d\u5e76\u8d4b\u503c<\/strong><\/h2>\n<p>df[&#39;new_column&#39;] = [7, 8, 9]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5c06\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  new_column<\/p>\n<p>0  1  4           7<\/p>\n<p>1  2  5           8<\/p>\n<p>2  3  6           9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\u6765\u5411\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u65b0\u5217\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c<\/strong><\/h2>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u662f\u4e00\u79cd\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u76f4\u63a5\u7ed9\u6570\u636e\u6846\u6307\u5b9a\u65b0\u5217\u540d\u5e76\u8d4b\u503c\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5411\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u65b0\u5217\u3002\u4e0a\u9762\u7684\u4f8b\u5b50\u5df2\u7ecf\u5c55\u793a\u4e86\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/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\u7ed9\u6570\u636e\u6846\u6307\u5b9a\u65b0\u5217\u540d\u5e76\u8d4b\u503c<\/strong><\/h2>\n<p>df[&#39;new_column&#39;] = [7, 8, 9]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u521d\u5b66\u8005\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528assign\u65b9\u6cd5<\/strong><\/h2>\n<p><p>assign\u65b9\u6cd5\u5141\u8bb8\u4f60\u5728\u94fe\u5f0f\u8c03\u7528\u4e2d\u5411\u6570\u636e\u6846\u6dfb\u52a0\u65b0\u5217\u3002\u5b83\u4e0d\u4f1a\u4fee\u6539\u539f\u59cb\u6570\u636e\u6846\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/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\u7528assign\u65b9\u6cd5\u6dfb\u52a0\u65b0\u5217<\/strong><\/h2>\n<p>df = df.assign(new_column=[7, 8, 9])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>assign\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u4e0e\u5176\u4ed6pandas\u65b9\u6cd5\u94fe\u5f0f\u8c03\u7528\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6d41\u7545\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528insert\u65b9\u6cd5<\/strong><\/h2>\n<p><p>insert\u65b9\u6cd5\u5141\u8bb8\u4f60\u5728\u7279\u5b9a\u4f4d\u7f6e\u63d2\u5165\u65b0\u5217\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u65b0\u5217\u7684\u4f4d\u7f6e\u3001\u5217\u540d\u548c\u5217\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/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\u7528insert\u65b9\u6cd5\u5728\u4f4d\u7f6e1\u63d2\u5165\u65b0\u5217<\/strong><\/h2>\n<p>df.insert(1, &#39;new_column&#39;, [7, 8, 9])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5c06\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  new_column  B<\/p>\n<p>0  1           7  4<\/p>\n<p>1  2           8  5<\/p>\n<p>2  3           9  6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>insert\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u63a7\u5236\u65b0\u5217\u7684\u63d2\u5165\u4f4d\u7f6e\uff0c\u7075\u6d3b\u6027\u66f4\u9ad8\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528apply\u65b9\u6cd5<\/strong><\/h2>\n<p><p>apply\u65b9\u6cd5\u5141\u8bb8\u4f60\u5bf9\u6570\u636e\u6846\u7684\u6bcf\u4e00\u884c\u6216\u6bcf\u4e00\u5217\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5e8f\u5217\u3002\u4f60\u53ef\u4ee5\u5229\u7528apply\u65b9\u6cd5\u751f\u6210\u65b0\u5217\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/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\u7528apply\u65b9\u6cd5\u751f\u6210\u65b0\u5217\u503c<\/strong><\/h2>\n<p>df[&#39;new_column&#39;] = df.apply(lambda row: row[&#39;A&#39;] + row[&#39;B&#39;], axis=1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5c06\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  new_column<\/p>\n<p>0  1  4           5<\/p>\n<p>1  2  5           7<\/p>\n<p>2  3  6           9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>apply\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u7075\u6d3b\u5730\u751f\u6210\u65b0\u5217\u503c\uff0c\u9002\u5408\u590d\u6742\u7684\u8ba1\u7b97\u548c\u903b\u8f91\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528concat\u65b9\u6cd5<\/strong><\/h2>\n<p><p>concat\u65b9\u6cd5\u5141\u8bb8\u4f60\u5c06\u591a\u4e2a\u6570\u636e\u6846\u6216\u5e8f\u5217\u62fc\u63a5\u5728\u4e00\u8d77\u3002\u4f60\u53ef\u4ee5\u5229\u7528concat\u65b9\u6cd5\u5411\u6570\u636e\u6846\u6dfb\u52a0\u65b0\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/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>\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u7684\u5e8f\u5217<\/strong><\/h2>\n<p>new_column = pd.Series([7, 8, 9], name=&#39;new_column&#39;)<\/p>\n<h2><strong>\u4f7f\u7528concat\u65b9\u6cd5\u62fc\u63a5\u65b0\u5217<\/strong><\/h2>\n<p>df = pd.concat([df, new_column], axis=1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5c06\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  new_column<\/p>\n<p>0  1  4           7<\/p>\n<p>1  2  5           8<\/p>\n<p>2  3  6           9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>concat\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u540c\u65f6\u62fc\u63a5\u591a\u4e2a\u6570\u636e\u6846\u6216\u5e8f\u5217\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u5408\u5e76\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528loc\u65b9\u6cd5<\/strong><\/h2>\n<p><p>loc\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u6807\u7b7e\u6765\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u636e\u6846\u7684\u503c\u3002\u4f60\u53ef\u4ee5\u5229\u7528loc\u65b9\u6cd5\u5411\u6570\u636e\u6846\u6dfb\u52a0\u65b0\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/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\u7528loc\u65b9\u6cd5\u6dfb\u52a0\u65b0\u5217<\/strong><\/h2>\n<p>df.loc[:, &#39;new_column&#39;] = [7, 8, 9]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5c06\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  new_column<\/p>\n<p>0  1  4           7<\/p>\n<p>1  2  5           8<\/p>\n<p>2  3  6           9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>loc\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u7075\u6d3b\u5730\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u636e\u6846\u7684\u503c\uff0c\u9002\u5408\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/strong><\/h2>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u65b0\u7684\u5e8f\u5217\u3002\u4f60\u53ef\u4ee5\u5229\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u65b0\u5217\u7684\u503c\uff0c\u5e76\u5411\u6570\u636e\u6846\u6dfb\u52a0\u65b0\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/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\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u65b0\u5217\u503c<\/strong><\/h2>\n<p>df[&#39;new_column&#39;] = [a + b for a, b in zip(df[&#39;A&#39;], df[&#39;B&#39;])]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5c06\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  new_column<\/p>\n<p>0  1  4           5<\/p>\n<p>1  2  5           7<\/p>\n<p>2  3  6           9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4f18\u70b9\u5728\u4e8e\u8bed\u6cd5\u7b80\u6d01\uff0c\u9002\u5408\u751f\u6210\u7b80\u5355\u7684\u65b0\u5217\u503c\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u4f7f\u7528numpy\u5e93<\/strong><\/h2>\n<p><p>numpy\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u5229\u7528numpy\u5e93\u751f\u6210\u65b0\u5217\u7684\u503c\uff0c\u5e76\u5411\u6570\u636e\u6846\u6dfb\u52a0\u65b0\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/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\u7528numpy\u5e93\u751f\u6210\u65b0\u5217\u503c<\/strong><\/h2>\n<p>df[&#39;new_column&#39;] = np.array([7, 8, 9])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5c06\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  new_column<\/p>\n<p>0  1  4           7<\/p>\n<p>1  2  5           8<\/p>\n<p>2  3  6           9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>numpy\u5e93\u7684\u4f18\u70b9\u5728\u4e8e\u6027\u80fd\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u4f7f\u7528map\u65b9\u6cd5<\/strong><\/h2>\n<p><p>map\u65b9\u6cd5\u5141\u8bb8\u4f60\u5bf9\u6570\u636e\u6846\u7684\u67d0\u4e00\u5217\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5e8f\u5217\u3002\u4f60\u53ef\u4ee5\u5229\u7528map\u65b9\u6cd5\u751f\u6210\u65b0\u5217\u7684\u503c\uff0c\u5e76\u5411\u6570\u636e\u6846\u6dfb\u52a0\u65b0\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/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\u7528map\u65b9\u6cd5\u751f\u6210\u65b0\u5217\u503c<\/strong><\/h2>\n<p>df[&#39;new_column&#39;] = df[&#39;A&#39;].map(lambda x: x * 2)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5c06\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  new_column<\/p>\n<p>0  1  4           2<\/p>\n<p>1  2  5           4<\/p>\n<p>2  3  6           6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>map\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u7075\u6d3b\u5730\u751f\u6210\u65b0\u5217\u503c\uff0c\u9002\u5408\u7b80\u5355\u7684\u8ba1\u7b97\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u3001\u4f7f\u7528transform\u65b9\u6cd5<\/strong><\/h2>\n<p><p>transform\u65b9\u6cd5\u5141\u8bb8\u4f60\u5bf9\u6570\u636e\u6846\u7684\u67d0\u4e00\u5217\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5e8f\u5217\u3002\u4f60\u53ef\u4ee5\u5229\u7528transform\u65b9\u6cd5\u751f\u6210\u65b0\u5217\u7684\u503c\uff0c\u5e76\u5411\u6570\u636e\u6846\u6dfb\u52a0\u65b0\u5217\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\u4ee3\u7801<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/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\u7528transform\u65b9\u6cd5\u751f\u6210\u65b0\u5217\u503c<\/strong><\/h2>\n<p>df[&#39;new_column&#39;] = df[&#39;A&#39;].transform(lambda x: x + 1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5c06\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  new_column<\/p>\n<p>0  1  4           2<\/p>\n<p>1  2  5           3<\/p>\n<p>2  3  6           4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>transform\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\u53ef\u4ee5\u7075\u6d3b\u5730\u751f\u6210\u65b0\u5217\u503c\uff0c\u9002\u5408\u7b80\u5355\u7684\u8ba1\u7b97\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<h2><strong>\u7ed3\u8bba<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5411\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u65b0\u5217\u3002<strong>\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528assign\u65b9\u6cd5\u3001\u4f7f\u7528insert\u65b9\u6cd5\u3001\u4f7f\u7528apply\u65b9\u6cd5\u3001\u4f7f\u7528concat\u65b9\u6cd5\u3001\u4f7f\u7528loc\u65b9\u6cd5\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528map\u65b9\u6cd5\u3001\u4f7f\u7528transform\u65b9\u6cd5<\/strong>\u7b49\u65b9\u5f0f\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5411\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u65b0\u5217\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u8d4b\u503c\u64cd\u4f5c\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u64cd\u4f5c\uff0cpandas\u5e93\u90fd\u63d0\u4f9b\u4e86\u7075\u6d3b\u800c\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u65b0\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5728\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u65b0\u5217\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\uff0c\u4f8b\u5982<code>df[&#39;new_column&#39;] = values<\/code>\uff0c\u6765\u6dfb\u52a0\u4e00\u5217\uff0c\u5176\u4e2d<code>values<\/code>\u53ef\u4ee5\u662f\u4e00\u4e2a\u5217\u8868\u3001\u6570\u7ec4\u6216\u5176\u4ed6\u5e8f\u5217\uff0c\u957f\u5ea6\u9700\u8981\u4e0e\u6570\u636e\u6846\u7684\u884c\u6570\u4e00\u81f4\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>assign()<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u65b0\u5217\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u8ba1\u7b97\u65b0\u5217\u7684\u503c\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u65b0\u5217\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u73b0\u6709\u5217\u7684\u8ba1\u7b97\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u7b97\u672f\u8fd0\u7b97\u3001\u6761\u4ef6\u5224\u65ad\u6216\u5e94\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u65b9\u5f0f\u751f\u6210\u65b0\u5217\u7684\u503c\u3002\u4f8b\u5982\uff0c<code>df[&#39;new_column&#39;] = df[&#39;column1&#39;] + df[&#39;column2&#39;]<\/code>\u5c06\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\uff0c\u5176\u503c\u4e3a<code>column1<\/code>\u548c<code>column2<\/code>\u7684\u548c\u3002<\/p>\n<p><strong>\u6dfb\u52a0\u65b0\u5217\u540e\uff0c\u5982\u4f55\u67e5\u770b\u6570\u636e\u6846\u7684\u53d8\u5316\uff1f<\/strong><br \/>\u6dfb\u52a0\u65b0\u5217\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>print(df.head())<\/code>\u6216<code>df.info()<\/code>\u6765\u67e5\u770b\u6570\u636e\u6846\u7684\u53d8\u5316\uff0c\u786e\u8ba4\u65b0\u5217\u662f\u5426\u6210\u529f\u6dfb\u52a0\u3002<code>head()<\/code>\u65b9\u6cd5\u5c06\u663e\u793a\u6570\u636e\u6846\u7684\u524d\u51e0\u884c\uff0c\u800c<code>info()<\/code>\u65b9\u6cd5\u5219\u63d0\u4f9b\u5173\u4e8e\u6570\u636e\u6846\u7684\u6574\u4f53\u4fe1\u606f\uff0c\u5305\u62ec\u5217\u6570\u3001\u6570\u636e\u7c7b\u578b\u53ca\u975e\u7a7a\u503c\u7684\u6570\u91cf\u7b49\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u786e\u4fdd\u65b0\u5217\u7684\u6dfb\u52a0\u7b26\u5408\u9884\u671f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u5904\u7406\u6570\u636e\u6846\uff0c\u5e76\u4e14\u975e\u5e38\u5bb9\u6613\u5730\u5411\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u4e00\u5217\u3002\u901a\u8fc7\u76f4\u63a5\u7ed9\u6570\u636e\u6846\u6307\u5b9a\u65b0 [&hellip;]","protected":false},"author":3,"featured_media":1092161,"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\/1092151"}],"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=1092151"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092151\/revisions"}],"predecessor-version":[{"id":1092162,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092151\/revisions\/1092162"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1092161"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1092151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1092151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1092151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}