{"id":1091729,"date":"2025-01-08T14:14:29","date_gmt":"2025-01-08T06:14:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1091729.html"},"modified":"2025-01-08T14:14:32","modified_gmt":"2025-01-08T06:14:32","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99df%e5%a2%9e%e5%8a%a0%e4%b8%80%e5%88%97-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1091729.html","title":{"rendered":"python\u5982\u4f55\u7ed9df\u589e\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\/24205108\/1cc4f905-adf6-48e7-8a8a-a5c95e4f4ef3.webp\" alt=\"python\u5982\u4f55\u7ed9df\u589e\u52a0\u4e00\u5217\" \/><\/p>\n<p><p> <strong>Python \u4e2d\u7ed9 DataFrame \u589e\u52a0\u4e00\u5217\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528 <code>assign<\/code> \u65b9\u6cd5\u3001\u4f7f\u7528 <code>insert<\/code> \u65b9\u6cd5\u3001\u4ee5\u53ca\u901a\u8fc7\u8ba1\u7b97\u751f\u6210\u65b0\u5217\u7b49\u3002<\/strong> \u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u8d4b\u503c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\u4e14\u7075\u6d3b\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u4e3e\u4f8b\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u662f\u7ed9 DataFrame \u589e\u52a0\u65b0\u5217\u6700\u7b80\u5355\u548c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4f60\u53ea\u9700\u5c06\u65b0\u5217\u540d\u4f5c\u4e3a\u952e\uff0c\u65b0\u5217\u7684\u6570\u636e\u4f5c\u4e3a\u503c\u8d4b\u503c\u7ed9 DataFrame \u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b DataFrame<\/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\u8d4b\u503c\u589e\u52a0\u4e00\u5217<\/strong><\/h2>\n<p>df[&#39;C&#39;] = [7, 8, 9]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u4e00\u5217\u7684\u6240\u6709\u503c\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u5217\u7684\u503c\u8fdb\u884c\u8ba1\u7b97\u751f\u6210\u65b0\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>assign<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>assign<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 DataFrame\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u5217\u3002\u5b83\u7684\u597d\u5904\u662f\u53ef\u4ee5\u94fe\u5f0f\u8c03\u7528\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b DataFrame<\/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 assign \u65b9\u6cd5\u589e\u52a0\u65b0\u5217<\/strong><\/h2>\n<p>df = df.assign(C=[7, 8, 9], D=lambda x: x.A + x.B)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc <code>assign<\/code> \u65b9\u6cd5\u4e0d\u4ec5\u589e\u52a0\u4e86\u65b0\u5217 C\uff0c\u800c\u4e14\u901a\u8fc7 lambda \u8868\u8fbe\u5f0f\u8ba1\u7b97\u5e76\u589e\u52a0\u4e86\u65b0\u5217 D\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 <code>insert<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>insert<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u65b0\u5217\uff0c\u800c\u4e0d\u662f\u603b\u662f\u5c06\u65b0\u5217\u6dfb\u52a0\u5728\u6700\u540e\u3002\u5b83\u9700\u8981\u6307\u5b9a\u5217\u7684\u4f4d\u7f6e\uff08\u7d22\u5f15\uff09\u3001\u5217\u540d\u548c\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b DataFrame<\/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>\u5728\u7d22\u5f15\u4f4d\u7f6e 1 \u63d2\u5165\u65b0\u5217<\/strong><\/h2>\n<p>df.insert(1, &#39;C&#39;, [7, 8, 9])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5728\u7279\u5b9a\u4f4d\u7f6e\u63d2\u5165\u65b0\u5217\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7\u8ba1\u7b97\u751f\u6210\u65b0\u5217<\/h3>\n<\/p>\n<p><p>\u65b0\u5217\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u5bf9\u5df2\u6709\u5217\u8fdb\u884c\u8ba1\u7b97\u5f97\u5230\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u5df2\u6709\u5217\u7684\u52a0\u51cf\u4e58\u9664\u7b49\u64cd\u4f5c\u751f\u6210\u65b0\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\u4f8b DataFrame<\/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>\u901a\u8fc7\u8ba1\u7b97\u751f\u6210\u65b0\u5217<\/strong><\/h2>\n<p>df[&#39;C&#39;] = df[&#39;A&#39;] + df[&#39;B&#39;]<\/p>\n<p>df[&#39;D&#39;] = df[&#39;A&#39;] * df[&#39;B&#39;]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u9700\u8981\u901a\u8fc7\u5df2\u6709\u6570\u636e\u8fdb\u884c\u590d\u6742\u8ba1\u7b97\u751f\u6210\u65b0\u5217\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 <code>apply<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>apply<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5bf9 DataFrame \u7684\u884c\u6216\u5217\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u4ece\u800c\u751f\u6210\u65b0\u5217\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u5bf9\u6bcf\u4e00\u884c\u6216\u6bcf\u4e00\u5217\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\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\u4f8b DataFrame<\/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 apply \u65b9\u6cd5\u751f\u6210\u65b0\u5217<\/strong><\/h2>\n<p>df[&#39;C&#39;] = df.apply(lambda row: row.A + row.B, axis=1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc <code>apply<\/code> \u65b9\u6cd5\u5bf9 DataFrame \u7684\u6bcf\u4e00\u884c\u5e94\u7528\u4e86\u4e00\u4e2a lambda \u51fd\u6570\uff0c\u4ece\u800c\u751f\u6210\u4e86\u65b0\u5217 C\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528 <code>map<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>map<\/code> \u65b9\u6cd5\u53ef\u4ee5\u5bf9 Series \u5e94\u7528\u4e00\u4e2a\u51fd\u6570\u6216\u6620\u5c04\u5173\u7cfb\uff0c\u4ece\u800c\u751f\u6210\u65b0\u5217\u3002\u9002\u5408\u5bf9\u5355\u5217\u8fdb\u884c\u8f6c\u6362\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b DataFrame<\/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 map \u65b9\u6cd5\u751f\u6210\u65b0\u5217<\/strong><\/h2>\n<p>df[&#39;C&#39;] = df[&#39;A&#39;].map(lambda x: x * 2)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528 <code>applymap<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>applymap<\/code> \u65b9\u6cd5\u7c7b\u4f3c\u4e8e <code>apply<\/code>\uff0c\u4f46\u5b83\u662f\u5bf9 DataFrame \u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u4ece\u800c\u751f\u6210\u65b0\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\u4f8b DataFrame<\/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 applymap \u65b9\u6cd5\u751f\u6210\u65b0\u5217<\/strong><\/h2>\n<p>df[&#39;C&#39;] = df.applymap(lambda x: x * 2)[&#39;A&#39;]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\u8d77\u6765\uff0cPython \u4e2d\u7ed9 DataFrame \u589e\u52a0\u65b0\u5217\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u76f4\u63a5\u8d4b\u503c\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c<code>assign<\/code> \u65b9\u6cd5\u9002\u5408\u94fe\u5f0f\u8c03\u7528\uff0c<code>insert<\/code> \u65b9\u6cd5\u9002\u5408\u5728\u7279\u5b9a\u4f4d\u7f6e\u63d2\u5165\u65b0\u5217\uff0c\u800c\u901a\u8fc7\u8ba1\u7b97\u3001<code>apply<\/code>\u3001<code>map<\/code>\u3001<code>applymap<\/code> \u7b49\u65b9\u6cd5\u5219\u9002\u5408\u751f\u6210\u590d\u6742\u7684\u65b0\u5217\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u5404\u79cd\u6570\u636e\u64cd\u4f5c\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5411DataFrame\u6dfb\u52a0\u65b0\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5411DataFrame\u6dfb\u52a0\u65b0\u5217\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\uff0c\u6216\u4f7f\u7528<code>assign()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u8981\u6dfb\u52a0\u4e00\u5217\u540d\u4e3a\u201c\u65b0\u5217\u201d\uff0c\u5e76\u8d4b\u4e88\u5176\u503c\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;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]})\ndf[&#39;\u65b0\u5217&#39;] = [7, 8, 9]  # \u76f4\u63a5\u8d4b\u503c\n# \u6216\u8005\ndf = df.assign(\u65b0\u5217=[7, 8, 9])  # \u4f7f\u7528assign\u65b9\u6cd5\n<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u6839\u636e\u73b0\u6709\u5217\u7684\u503c\u6765\u6dfb\u52a0\u65b0\u5217\u5417\uff1f<\/strong><br \/>\u5b8c\u5168\u53ef\u4ee5\uff01\u60a8\u53ef\u4ee5\u4f7f\u7528\u5df2\u6709\u5217\u7684\u8fd0\u7b97\u7ed3\u679c\u6765\u521b\u5efa\u65b0\u5217\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u60a8\u6709\u4e00\u5217\u201cA\u201d\uff0c\u60f3\u8981\u521b\u5efa\u4e00\u5217\u201cC\u201d\uff0c\u5176\u503c\u4e3a\u201cA\u201d\u5217\u7684\u503c\u52a0\u4e0a5\uff0c\u53ef\u4ee5\u8fd9\u6837\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-python\">df[&#39;C&#39;] = df[&#39;A&#39;] + 5\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u201cC\u201d\u5217\u7684\u6bcf\u4e2a\u503c\u90fd\u4f1a\u662f\u201cA\u201d\u5217\u5bf9\u5e94\u503c\u52a05\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u6dfb\u52a0\u57fa\u4e8e\u6761\u4ef6\u7684\u65b0\u5217\uff1f<\/strong><br \/>\u5728Pandas\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>np.where()<\/code>\u51fd\u6570\u6216\u8005<code>apply()<\/code>\u65b9\u6cd5\u6765\u6839\u636e\u6761\u4ef6\u4e3a\u65b0\u5217\u8d4b\u503c\u3002\u5047\u8bbe\u60a8\u60f3\u8981\u6839\u636e\u201cA\u201d\u5217\u7684\u503c\u5224\u65ad\u65b0\u5217\u201cD\u201d\u7684\u503c\u662f\u5426\u5927\u4e8e2\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ndf[&#39;D&#39;] = np.where(df[&#39;A&#39;] &gt; 2, &#39;\u5927\u4e8e2&#39;, &#39;\u5c0f\u4e8e\u7b49\u4e8e2&#39;)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u201cA\u201d\u5217\u7684\u503c\u5927\u4e8e2\uff0c\u5219\u201cD\u201d\u5217\u5bf9\u5e94\u4f4d\u7f6e\u7684\u503c\u4e3a\u201c\u5927\u4e8e2\u201d\uff0c\u5426\u5219\u4e3a\u201c\u5c0f\u4e8e\u7b49\u4e8e2\u201d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u4e2d\u7ed9 DataFrame \u589e\u52a0\u4e00\u5217\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528 assign \u65b9\u6cd5\u3001\u4f7f\u7528 ins [&hellip;]","protected":false},"author":3,"featured_media":1091740,"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\/1091729"}],"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=1091729"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091729\/revisions"}],"predecessor-version":[{"id":1091742,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091729\/revisions\/1091742"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1091740"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1091729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1091729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1091729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}