{"id":178762,"date":"2024-05-08T19:57:14","date_gmt":"2024-05-08T11:57:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/178762.html"},"modified":"2024-05-08T19:57:17","modified_gmt":"2024-05-08T11:57:17","slug":"python-pandas-%e6%80%8e%e6%a0%b7%e9%ab%98%e6%95%88%e5%9c%b0%e6%b7%bb%e5%8a%a0%e4%b8%80%e8%a1%8c%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/178762.html","title":{"rendered":"python pandas \u600e\u6837\u9ad8\u6548\u5730\u6dfb\u52a0\u4e00\u884c\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27063315\/c600fb57-74ee-45c3-8d84-c52983ebfa9d.webp\" alt=\"python pandas \u600e\u6837\u9ad8\u6548\u5730\u6dfb\u52a0\u4e00\u884c\u6570\u636e\" \/><\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5728DataFrame\u4e2d\u9ad8\u6548\u5730\u6dfb\u52a0\u4e00\u884c\u6570\u636e\u3002<strong>\u4f7f\u7528<code>append()<\/code>\u65b9\u6cd5\u3001<code>loc<\/code>\u5c5e\u6027<\/strong>\u3001<strong>\u4ee5\u53ca<code>concat()<\/code>\u51fd\u6570<\/strong>\uff0c\u90fd\u662f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u6709\u6548\u624b\u6bb5\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528<code>loc<\/code>\u5c5e\u6027<\/strong>\u5c24\u5176\u503c\u5f97\u63a8\u8350\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u80fd\u591f\u76f4\u63a5\u5728DataFrame\u7684\u5e95\u90e8\u6dfb\u52a0\u4e00\u884c\u6570\u636e\uff0c\u800c\u4e14\u5728\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u80fd\u591f\u4fdd\u6301\u6570\u636e\u7c7b\u578b\u7684\u4e00\u81f4\u6027\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>LOC<\/code>\u5c5e\u6027\u6dfb\u52a0\u4e00\u884c\u6570\u636e<\/h3>\n<\/p>\n<p><p><strong><code>loc<\/code>\u5c5e\u6027<\/strong>\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u6307\u5b9a\u7684\u7d22\u5f15\u503c\uff0c\u5728DataFrame\u4e2d\u6dfb\u52a0\u6216\u4fee\u6539\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4e00\u4e2a\u4e3b\u8981\u4f18\u52bf\u662f\u6267\u884c\u901f\u5ea6\u5feb\uff0c\u975e\u5e38\u9002\u7528\u4e8e\u5355\u6761\u6570\u636e\u7684\u5feb\u901f\u6dfb\u52a0\u3002\u53e6\u5916\uff0c<code>loc<\/code>\u5c5e\u6027\u8fd8\u80fd\u591f\u786e\u4fdd\u6dfb\u52a0\u6570\u636e\u65f6\u4e0d\u4f1a\u56e0\u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\u800c\u5f15\u53d1\u9519\u8bef\uff0c\u8fd9\u5bf9\u4e8e\u4fdd\u6301\u6570\u636e\u96c6\u7684\u6574\u4f53\u4e00\u81f4\u6027\u6781\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u8981\u901a\u8fc7<code>loc<\/code>\u5c5e\u6027\u6dfb\u52a0\u4e00\u884c\u6570\u636e\uff0c\u4f60\u53ea\u9700\u63d0\u4f9b\u4e00\u4e2a\u65b0\u7684\u7d22\u5f15\u503c\u548c\u4e00\u4e2a\u4e0eDataFrame\u5217\u76f8\u5bf9\u5e94\u7684\u6570\u636e\u5217\u8868\u6216\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u5728\u4e00\u4e2a\u5305\u542b\u4e09\u5217\u7684DataFrame\u4e2d\u6dfb\u52a0\u4e00\u884c\u6570\u636e\uff0c\u53ef\u4ee5\u5982\u4e0b\u64cd\u4f5c\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>df = pd.DataFrame({&#039;A&#039;: [1, 2], &#039;B&#039;: [3, 4], &#039;C&#039;: [5, 6]})<\/p>\n<h2><strong>\u4f7f\u7528loc\u5c5e\u6027\u6dfb\u52a0\u4e00\u884c\u6570\u636e<\/strong><\/h2>\n<p>df.loc[len(df)] = {&#039;A&#039;: 3, &#039;B&#039;: 5, &#039;C&#039;: 7}<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u76f4\u89c2\u4e14\u6613\u4e8e\u7406\u89e3\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5bf9\u6570\u636e\u5904\u7406\u6548\u7387\u6709\u8f83\u9ad8\u8981\u6c42\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>APPEND()<\/code>\u65b9\u6cd5\u6dfb\u52a0\u4e00\u884c\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>loc<\/code>\u5c5e\u6027\u662f\u63a8\u8350\u7684\u6dfb\u52a0\u6570\u636e\u65b9\u5f0f\uff0c\u4f46\u662f<code>append()<\/code>\u65b9\u6cd5\u4e5f\u5e38\u88ab\u7528\u4e8e\u5411DataFrame\u4e2d\u8ffd\u52a0\u6570\u636e\u3002<code>append()<\/code>\u65b9\u6cd5\u5141\u8bb8\u5c06\u4e00\u4e2aSeries\u6216\u5b57\u5178\u6dfb\u52a0\u5230DataFrame\u7684\u672b\u5c3e\uff0c\u8fd9\u4e00\u65b9\u5f0f\u975e\u5e38\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u6dfb\u52a0\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>append()<\/code>\u65f6\uff0c\u5e94\u8be5\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0c\u8be5\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684DataFrame\u5bf9\u8c61\uff0c\u800c\u4e0d\u4f1a\u4fee\u6539\u539f\u59cbDataFrame\u3002\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u60f3\u8981\u66f4\u65b0\u539f\u59cbDataFrame\uff0c\u9700\u8981\u5c06<code>append()<\/code>\u7684\u7ed3\u679c\u8d4b\u503c\u56de\u539fDataFrame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/p>\n<p>df = pd.DataFrame({&#039;A&#039;: [1, 2], &#039;B&#039;: [3, 4], &#039;C&#039;: [5, 6]})<\/p>\n<h2><strong>\u4f7f\u7528append\u65b9\u6cd5\u6dfb\u52a0\u4e00\u884c\u6570\u636e\uff0c\u5e76\u66f4\u65b0df<\/strong><\/h2>\n<p>df = df.append({&#039;A&#039;: 3, &#039;B&#039;: 5, &#039;C&#039;: 7}, ignore_index=True)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>CONCAT()<\/code>\u51fd\u6570\u5408\u5e76\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>loc<\/code>\u5c5e\u6027\u548c<code>append()<\/code>\u65b9\u6cd5\u5916\uff0c<code>concat()<\/code>\u51fd\u6570\u540c\u6837\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e24\u4e2aDataFrame\u5408\u5e76\u5728\u4e00\u8d77\u3002\u8fd9\u5728\u4f60\u9700\u8981\u5c06\u591a\u884c\u6570\u636e\u4e00\u6b21\u6027\u6dfb\u52a0\u5230DataFrame\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>concat()<\/code>\u51fd\u6570\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5b83\u662f\u5728\u5782\u76f4\u65b9\u5411\u4e0a\uff08axis=0\uff09\u5408\u5e76\u6570\u636e\uff0c\u8fd9\u4e0e\u6211\u4eec\u7684\u9700\u6c42\u6dfb\u52a0\u4e00\u884c\u6570\u636e\u4e00\u81f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/p>\n<p>df = pd.DataFrame({&#039;A&#039;: [1, 2], &#039;B&#039;: [3, 4], &#039;C&#039;: [5, 6]})<\/p>\n<h2><strong>\u521b\u5efa\u53e6\u4e00\u4e2aDataFrame\uff0c\u5176\u5305\u542b\u8981\u6dfb\u52a0\u7684\u6570\u636e<\/strong><\/h2>\n<p>new_row = pd.DataFrame({&#039;A&#039;: [3], &#039;B&#039;: [5], &#039;C&#039;: [7]})<\/p>\n<h2><strong>\u4f7f\u7528concat\u51fd\u6570\u6dfb\u52a0\u4e00\u884c\u6570\u636e<\/strong><\/h2>\n<p>df = pd.concat([df, new_row], ignore_index=True)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c<code>loc<\/code>\u5c5e\u6027\u3001<code>append()<\/code>\u65b9\u6cd5\u548c<code>concat()<\/code>\u51fd\u6570\u90fd\u662f\u5728Pandas\u4e2d\u6dfb\u52a0\u4e00\u884c\u6570\u636e\u7684\u6709\u6548\u5de5\u5177\u3002\u5728\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\uff0c<strong>\u4f7f\u7528<code>loc<\/code>\u5c5e\u6027\u4e0d\u4ec5\u80fd\u591f\u786e\u4fdd\u9ad8\u6548\u5730\u5b8c\u6210\u6570\u636e\u6dfb\u52a0\u64cd\u4f5c\uff0c\u800c\u4e14\u8fd8\u80fd\u591f\u4fdd\u6301\u6570\u636e\u7c7b\u578b\u7684\u4e00\u81f4\u6027\uff0c\u4ece\u800c\u4fdd\u8bc1\u6570\u636e\u5904\u7406\u7684\u51c6\u786e\u6027\u3002<\/strong> \u56e0\u6b64\uff0c\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728Python pandas\u4e2d\u5feb\u901f\u6dfb\u52a0\u4e00\u884c\u6570\u636e\uff1f<\/strong><br \/>\u8981\u5728pandas\u4e2d\u9ad8\u6548\u5730\u6dfb\u52a0\u4e00\u884c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>df.loc<\/code>\u65b9\u6cd5\u3002\u60a8\u9700\u8981\u6307\u5b9a\u65b0\u884c\u7684\u7d22\u5f15\u548c\u8981\u6dfb\u52a0\u7684\u503c\uff0c\u7136\u540e\u4f7f\u7528<code>df.loc[new_index] = new_values<\/code>\u5c06\u65b0\u884c\u6dfb\u52a0\u5230DataFrame\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u6548\u7387\u8f83\u9ad8\uff0c\u56e0\u4e3a\u5b83\u76f4\u63a5\u5728\u539f\u59cbDataFrame\u4e0a\u8fdb\u884c\u64cd\u4f5c\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u526f\u672c\u3002<\/p>\n<p><strong>2. \u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u9ad8\u6548\u5730\u5411Python pandas\u4e2d\u6dfb\u52a0\u65b0\u6570\u636e\u884c\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>df.loc<\/code>\u65b9\u6cd5\u4e4b\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>df.append<\/code>\u65b9\u6cd5\u6765\u6dfb\u52a0\u65b0\u7684\u884c\u6570\u636e\u3002\u4f46\u662f\uff0c\u8bf7\u6ce8\u610f\uff0c<code>df.append<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684DataFrame\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u5c31\u5730\u4fee\u6539\u539f\u59cbDataFrame\u3002\u5982\u679c\u60a8\u9700\u8981\u9891\u7e41\u5730\u6dfb\u52a0\u884c\uff0c\u4f7f\u7528<code>df.loc<\/code>\u65b9\u6cd5\u53ef\u80fd\u66f4\u9ad8\u6548\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u5176\u4ed6\u6280\u5de7\u53ef\u4ee5\u5728Python pandas\u4e2d\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u6570\u636e\u884c\u7684\u6dfb\u52a0\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u884c\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>pd.concat<\/code>\u6765\u6279\u91cf\u6dfb\u52a0\u591a\u884c\u6570\u636e\u3002<code>pd.concat<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2aDataFrame\u5bf9\u8c61\u6309\u884c\u6216\u5217\u5408\u5e76\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684DataFrame\u3002\u5c06\u8981\u6dfb\u52a0\u7684\u884c\u6570\u636e\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61\uff0c\u5e76\u5c06\u5176\u4e0e\u539f\u59cbDataFrame\u4f7f\u7528<code>pd.concat<\/code>\u5408\u5e76\uff0c\u53ef\u4ee5\u51cf\u5c11\u5faa\u73af\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u5904\u7406\u6570\u636e\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5728DataFrame\u4e2d\u9ad8\u6548\u5730\u6dfb\u52a0\u4e00\u884c\u6570\u636e\u3002\u4f7f\u7528append()\u65b9\u6cd5\u3001loc\u5c5e\u6027\u3001\u4ee5\u53ca [&hellip;]","protected":false},"author":3,"featured_media":178768,"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\/178762"}],"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=178762"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/178762\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/178768"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=178762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=178762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=178762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}