{"id":1114612,"date":"2025-01-08T17:59:09","date_gmt":"2025-01-08T09:59:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1114612.html"},"modified":"2025-01-08T17:59:12","modified_gmt":"2025-01-08T09:59:12","slug":"python%e9%87%8c%e9%9d%a2%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e6%95%b0%e6%8d%ae%e6%a1%86%e5%8a%a0%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1114612.html","title":{"rendered":"python\u91cc\u9762\u5982\u4f55\u5904\u7406\u6570\u636e\u6846\u52a0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075709\/129ea1c6-1830-46d7-b68b-8b4b6a84b12b.webp\" alt=\"python\u91cc\u9762\u5982\u4f55\u5904\u7406\u6570\u636e\u6846\u52a0\u884c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528pandas\u5e93\u6765\u5904\u7406\u6570\u636e\u6846\uff08DataFrame\uff09\u5e76\u6dfb\u52a0\u884c\u3002\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5904\u7406\u3001\u5206\u6790\u548c\u64cd\u4f5c\u3002\u4e3a\u4e86\u5728\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u884c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982\uff1aappend()\u65b9\u6cd5\u3001loc\u65b9\u6cd5\u3001concat()\u51fd\u6570\u7b49\u3002<\/strong> \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u5982\u4f55\u4f7f\u7528\u5b83\u4eec\u6765\u5904\u7406\u6570\u636e\u6846\u5e76\u6dfb\u52a0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528append()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>append()\u65b9\u6cd5\u662fpandas\u5e93\u4e2d\u7528\u4e8e\u5728\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u884c\u7684\u5e38\u7528\u65b9\u6cd5\u4e4b\u4e00\u3002\u8be5\u65b9\u6cd5\u5141\u8bb8\u4f60\u5c06\u4e00\u4e2a\u6570\u636e\u6846\u6216\u5b57\u5178\u5f62\u5f0f\u7684\u6570\u636e\u6dfb\u52a0\u5230\u73b0\u6709\u7684\u6570\u636e\u6846\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Alice&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [23, 30, 25]<\/p>\n<p>})<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u884c\u6570\u636e<\/strong><\/h2>\n<p>new_row = {&#39;Name&#39;: &#39;Eve&#39;, &#39;Age&#39;: 28}<\/p>\n<h2><strong>\u4f7f\u7528append()\u65b9\u6cd5\u6dfb\u52a0\u65b0\u884c<\/strong><\/h2>\n<p>df = df.append(new_row, ignore_index=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>append()\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u5b57\u5178\u6216\u6570\u636e\u6846\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846\u3002\u901a\u8fc7\u8bbe\u7f6eignore_index\u53c2\u6570\u4e3aTrue\uff0c\u53ef\u4ee5\u786e\u4fdd\u65b0\u884c\u7684\u7d22\u5f15\u6309\u987a\u5e8f\u6392\u5217\uff0c\u800c\u4e0d\u662f\u4fdd\u7559\u539f\u59cb\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528loc\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>loc\u65b9\u6cd5\u662f\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u901a\u8fc7\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u6765\u6dfb\u52a0\u65b0\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Alice&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [23, 30, 25]<\/p>\n<p>})<\/p>\n<h2><strong>\u4f7f\u7528loc\u65b9\u6cd5\u6dfb\u52a0\u65b0\u884c<\/strong><\/h2>\n<p>df.loc[len(df.index)] = [&#39;Eve&#39;, 28]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>loc\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u6307\u5b9a\u7d22\u5f15\u4f4d\u7f6e\u6765\u6dfb\u52a0\u65b0\u884c\u3002len(df.index)\u8fd4\u56de\u6570\u636e\u6846\u7684\u5f53\u524d\u884c\u6570\uff0c\u65b0\u884c\u5c06\u88ab\u6dfb\u52a0\u5230\u6570\u636e\u6846\u7684\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528concat()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>concat()\u51fd\u6570\u662fpandas\u5e93\u4e2d\u7528\u4e8e\u8fde\u63a5\u6570\u636e\u6846\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u5c06\u591a\u4e2a\u6570\u636e\u6846\u6216\u7cfb\u5217\u6cbf\u8f74\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u6570\u636e\u6846<\/strong><\/h2>\n<p>df1 = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Alice&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [23, 30, 25]<\/p>\n<p>})<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u65b0\u884c\u6570\u636e\u7684\u6570\u636e\u6846<\/strong><\/h2>\n<p>df2 = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;Eve&#39;],<\/p>\n<p>    &#39;Age&#39;: [28]<\/p>\n<p>})<\/p>\n<h2><strong>\u4f7f\u7528concat()\u51fd\u6570\u8fde\u63a5\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.concat([df1, df2], ignore_index=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>concat()\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5217\u8868\u5f62\u5f0f\u7684\u6570\u636e\u6846\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846\u3002\u901a\u8fc7\u8bbe\u7f6eignore_index\u53c2\u6570\u4e3aTrue\uff0c\u53ef\u4ee5\u786e\u4fdd\u65b0\u884c\u7684\u7d22\u5f15\u6309\u987a\u5e8f\u6392\u5217\uff0c\u800c\u4e0d\u662f\u4fdd\u7559\u539f\u59cb\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528DataFrame\u7684append()\u65b9\u6cd5\u548c\u5b57\u5178\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528DataFrame\u7684append()\u65b9\u6cd5\u548c\u5b57\u5178\u5217\u8868\u6765\u6dfb\u52a0\u591a\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Alice&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [23, 30, 25]<\/p>\n<p>})<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u65b0\u884c\u6570\u636e\u7684\u5b57\u5178\u5217\u8868<\/strong><\/h2>\n<p>new_rows = [<\/p>\n<p>    {&#39;Name&#39;: &#39;Eve&#39;, &#39;Age&#39;: 28},<\/p>\n<p>    {&#39;Name&#39;: &#39;Dan&#39;, &#39;Age&#39;: 35}<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528DataFrame\u7684append()\u65b9\u6cd5\u6dfb\u52a0\u65b0\u884c<\/strong><\/h2>\n<p>df = df.append(new_rows, ignore_index=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5b57\u5178\u5217\u8868\u7ed9append()\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u884c\u6570\u636e\u3002\u8bbe\u7f6eignore_index\u53c2\u6570\u4e3aTrue\uff0c\u4ee5\u786e\u4fdd\u65b0\u884c\u7684\u7d22\u5f15\u6309\u987a\u5e8f\u6392\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528DataFrame\u7684insert()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>insert()\u65b9\u6cd5\u5141\u8bb8\u4f60\u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u65b0\u5217\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u63d2\u5165\u65b0\u884c\u6765\u95f4\u63a5\u6dfb\u52a0\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Alice&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [23, 30, 25]<\/p>\n<p>})<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u884c\u6570\u636e<\/strong><\/h2>\n<p>new_row = pd.DataFrame({&#39;Name&#39;: [&#39;Eve&#39;], &#39;Age&#39;: [28]})<\/p>\n<h2><strong>\u4f7f\u7528insert()\u65b9\u6cd5\u63d2\u5165\u65b0\u884c<\/strong><\/h2>\n<p>df = pd.concat([df.iloc[:2], new_row, df.iloc[2:]], ignore_index=True)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u6570\u636e\u6846\u5206\u4e3a\u4e24\u90e8\u5206\u5e76\u4f7f\u7528concat()\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5728\u6570\u636e\u6846\u7684\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u65b0\u884c\u3002\u6b64\u793a\u4f8b\u5c06\u65b0\u884c\u63d2\u5165\u5230\u6570\u636e\u6846\u7684\u7b2c\u4e8c\u884c\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528DataFrame\u7684at\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>at\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u6307\u5b9a\u884c\u5217\u6807\u7b7e\u6765\u8bbf\u95ee\u548c\u8bbe\u7f6e\u5355\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Alice&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [23, 30, 25]<\/p>\n<p>})<\/p>\n<h2><strong>\u4f7f\u7528at\u65b9\u6cd5\u6dfb\u52a0\u65b0\u884c<\/strong><\/h2>\n<p>df.at[len(df.index), &#39;Name&#39;] = &#39;Eve&#39;<\/p>\n<p>df.at[len(df.index) - 1, &#39;Age&#39;] = 28<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>at\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u6307\u5b9a\u884c\u5217\u6807\u7b7e\u6765\u8bbf\u95ee\u548c\u8bbe\u7f6e\u5355\u4e2a\u503c\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u6307\u5b9a\u7d22\u5f15\u548c\u5217\u6807\u7b7e\u6765\u6dfb\u52a0\u65b0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528DataFrame\u7684assign()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>assign()\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u4f20\u9012\u5173\u952e\u5b57\u53c2\u6570\u6765\u6dfb\u52a0\u65b0\u5217\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u5b57\u5178\u6765\u6dfb\u52a0\u65b0\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Alice&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [23, 30, 25]<\/p>\n<p>})<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u884c\u6570\u636e<\/strong><\/h2>\n<p>new_row = {&#39;Name&#39;: &#39;Eve&#39;, &#39;Age&#39;: 28}<\/p>\n<h2><strong>\u4f7f\u7528assign()\u65b9\u6cd5\u6dfb\u52a0\u65b0\u884c<\/strong><\/h2>\n<p>df = df.assign(new_row)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>assign()\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u4f20\u9012\u5173\u952e\u5b57\u53c2\u6570\u6765\u6dfb\u52a0\u65b0\u5217\u3002\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5b57\u5178\u5f62\u5f0f\u7684\u65b0\u884c\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u65b0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528DataFrame\u7684reindex()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>reindex()\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u91cd\u65b0\u7d22\u5f15\u6765\u6dfb\u52a0\u65b0\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Alice&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [23, 30, 25]<\/p>\n<p>})<\/p>\n<h2><strong>\u4f7f\u7528reindex()\u65b9\u6cd5\u6dfb\u52a0\u65b0\u884c<\/strong><\/h2>\n<p>df = df.reindex(df.index.union([3]))<\/p>\n<p>df.at[3, &#39;Name&#39;] = &#39;Eve&#39;<\/p>\n<p>df.at[3, &#39;Age&#39;] = 28<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>reindex()\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u91cd\u65b0\u7d22\u5f15\u6765\u6dfb\u52a0\u65b0\u884c\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u91cd\u65b0\u7d22\u5f15\u5e76\u4f7f\u7528at\u65b9\u6cd5\u6dfb\u52a0\u65b0\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528DataFrame\u7684merge()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>merge()\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u5408\u5e76\u6570\u636e\u6846\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5408\u5e76\u65b0\u884c\u6570\u636e\u6765\u6dfb\u52a0\u884c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u6570\u636e\u6846<\/strong><\/h2>\n<p>df1 = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Alice&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [23, 30, 25]<\/p>\n<p>})<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u65b0\u884c\u6570\u636e\u7684\u6570\u636e\u6846<\/strong><\/h2>\n<p>df2 = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;Eve&#39;],<\/p>\n<p>    &#39;Age&#39;: [28]<\/p>\n<p>})<\/p>\n<h2><strong>\u4f7f\u7528merge()\u65b9\u6cd5\u5408\u5e76\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.merge(df1, df2, how=&#39;outer&#39;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>merge()\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u5408\u5e76\u6570\u636e\u6846\u6765\u6dfb\u52a0\u65b0\u884c\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5408\u5e76\u5305\u542b\u65b0\u884c\u6570\u636e\u7684\u6570\u636e\u6846\u6765\u6dfb\u52a0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528DataFrame\u7684update()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>update()\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u4f20\u9012\u53e6\u4e00\u4e2a\u6570\u636e\u6846\u6765\u66f4\u65b0\u73b0\u6709\u6570\u636e\u6846\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u6570\u636e\u6846<\/strong><\/h2>\n<p>df1 = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Alice&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [23, 30, 25]<\/p>\n<p>})<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u65b0\u884c\u6570\u636e\u7684\u6570\u636e\u6846<\/strong><\/h2>\n<p>df2 = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;Eve&#39;],<\/p>\n<p>    &#39;Age&#39;: [28]<\/p>\n<p>})<\/p>\n<h2><strong>\u4f7f\u7528update()\u65b9\u6cd5\u66f4\u65b0\u6570\u636e\u6846<\/strong><\/h2>\n<p>df1.update(df2)<\/p>\n<p>print(df1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p>update()\u65b9\u6cd5\u5141\u8bb8\u4f60\u901a\u8fc7\u4f20\u9012\u53e6\u4e00\u4e2a\u6570\u636e\u6846\u6765\u66f4\u65b0\u73b0\u6709\u6570\u636e\u6846\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u4f20\u9012\u5305\u542b\u65b0\u884c\u6570\u636e\u7684\u6570\u636e\u6846\u6765\u66f4\u65b0\u73b0\u6709\u6570\u636e\u6846\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406\u6570\u636e\u6846\u5e76\u6dfb\u52a0\u884c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528append()\u65b9\u6cd5\u3001loc\u65b9\u6cd5\u3001concat()\u51fd\u6570\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u6570\u636e\u6846\u3002\u65e0\u8bba\u662f\u6dfb\u52a0\u5355\u884c\u8fd8\u662f\u591a\u884c\u6570\u636e\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Pandas\u5e93\u6765\u6dfb\u52a0\u884c\u5230\u6570\u636e\u6846\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u8f7b\u677e\u5730\u5411\u6570\u636e\u6846\u6dfb\u52a0\u884c\u3002\u901a\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u6216<code>concat()<\/code>\u51fd\u6570\u3002<code>append()<\/code>\u65b9\u6cd5\u4f1a\u5728\u539f\u6570\u636e\u6846\u7684\u5e95\u90e8\u6dfb\u52a0\u65b0\u884c\uff0c\u800c<code>concat()<\/code>\u51fd\u6570\u5219\u53ef\u4ee5\u5c06\u591a\u4e2a\u6570\u636e\u6846\u5408\u5e76\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846\ndf = pd.DataFrame({&#39;A&#39;: [1, 2], &#39;B&#39;: [3, 4]})\n\n# \u521b\u5efa\u4e00\u4e2a\u65b0\u884c\nnew_row = pd.DataFrame({&#39;A&#39;: [5], &#39;B&#39;: [6]})\n\n# \u4f7f\u7528append\u65b9\u6cd5\u6dfb\u52a0\u65b0\u884c\ndf = df.append(new_row, ignore_index=True)\n\n# \u6216\u8005\u4f7f\u7528concat\u51fd\u6570\ndf = pd.concat([df, new_row], ignore_index=True)\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u5e2e\u52a9\u4f60\u8f7b\u677e\u5730\u7ba1\u7406\u6570\u636e\u6846\u4e2d\u7684\u884c\u3002<\/p>\n<p><strong>\u5728\u6dfb\u52a0\u65b0\u884c\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u6570\u636e\u6846\u7684\u5217\u540d\u4e00\u81f4\uff1f<\/strong><br \/>\u5728\u5411\u6570\u636e\u6846\u6dfb\u52a0\u65b0\u884c\u65f6\uff0c\u786e\u4fdd\u65b0\u884c\u7684\u6570\u636e\u7ed3\u6784\u4e0e\u73b0\u6709\u6570\u636e\u6846\u7684\u5217\u540d\u4e00\u81f4\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u5178\u952e\u6216\u6570\u636e\u6846\u6765\u521b\u5efa\u65b0\u884c\uff0c\u786e\u4fdd\u6240\u6709\u5217\u540d\u90fd\u5339\u914d\u3002\u5982\u679c\u5217\u540d\u4e0d\u4e00\u81f4\uff0cPandas\u4f1a\u586b\u5145\u7f3a\u5931\u503c\u4e3a<code>NaN<\/code>\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u5206\u6790\u65f6\u51fa\u73b0\u95ee\u9898\u3002\u56e0\u6b64\uff0c\u5728\u6dfb\u52a0\u65b0\u884c\u4e4b\u524d\uff0c\u53ef\u4ee5\u5148\u68c0\u67e5\u73b0\u6709\u6570\u636e\u6846\u7684\u5217\u540d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u591a\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u591a\u4e2a\u65b0\u884c\u7ec4\u5408\u6210\u4e00\u4e2a\u6570\u636e\u6846\u6765\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u884c\u3002\u521b\u5efa\u5305\u542b\u591a\u884c\u6570\u636e\u7684\u65b0\u6570\u636e\u6846\u540e\uff0c\u518d\u4f7f\u7528<code>concat()<\/code>\u51fd\u6570\u5c06\u5176\u4e0e\u539f\u6570\u636e\u6846\u5408\u5e76\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\"># \u521b\u5efa\u5305\u542b\u591a\u884c\u7684\u65b0\u6570\u636e\u6846\nnew_rows = pd.DataFrame({&#39;A&#39;: [7, 8], &#39;B&#39;: [9, 10]})\n\n# \u4f7f\u7528concat\u51fd\u6570\u6dfb\u52a0\u591a\u884c\ndf = pd.concat([df, new_rows], ignore_index=True)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u9ad8\u6548\u5730\u5411\u6570\u636e\u6846\u6dfb\u52a0\u591a\u884c\u6570\u636e\uff0c\u7b80\u5316\u6570\u636e\u5904\u7406\u8fc7\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528pandas\u5e93\u6765\u5904\u7406\u6570\u636e\u6846\uff08DataFrame\uff09\u5e76\u6dfb\u52a0\u884c\u3002\u4f7f\u7528pandas\u5e93\u53ef\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":1114619,"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\/1114612"}],"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=1114612"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114612\/revisions"}],"predecessor-version":[{"id":1114620,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114612\/revisions\/1114620"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1114619"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1114612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1114612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1114612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}