{"id":1069749,"date":"2025-01-08T10:51:50","date_gmt":"2025-01-08T02:51:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069749.html"},"modified":"2025-01-08T10:51:53","modified_gmt":"2025-01-08T02:51:53","slug":"%e5%a6%82%e4%bd%95%e7%bb%93%e5%90%88%e4%b8%a4%e4%b8%aaexcel%e8%a1%a8%e6%a0%bcpython-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069749.html","title":{"rendered":"\u5982\u4f55\u7ed3\u5408\u4e24\u4e2aexcel\u8868\u683cpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100703\/dbe0ffa2-bee4-4c91-9e6d-3bc3af081b33.webp\" alt=\"\u5982\u4f55\u7ed3\u5408\u4e24\u4e2aexcel\u8868\u683cpython\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7ed3\u5408\u4e24\u4e2aexcel\u8868\u683cpython<\/strong><\/p>\n<\/p>\n<p><p>\u7ed3\u5408\u4e24\u4e2a Excel \u8868\u683c\u5728 Python \u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u6709 <strong>Pandas\u3001Openpyxl\u3001xlrd<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u6709\u6548\u5730\u5c06\u4e24\u4e2a Excel \u8868\u683c\u4e2d\u7684\u6570\u636e\u5408\u5e76\u5728\u4e00\u8d77\u3002<strong>\u4f7f\u7528 Pandas\u3001\u64cd\u4f5c\u7b80\u5355\u3001\u5904\u7406\u901f\u5ea6\u5feb\u3001\u529f\u80fd\u5f3a\u5927<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Pandas \u5e93\u6765\u7ed3\u5408\u4e24\u4e2a Excel \u8868\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u6240\u9700\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86 Pandas \u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u5982\u679c\u4f60\u7684 Excel \u6587\u4ef6\u662f xlsx \u683c\u5f0f\uff0c\u8fd8\u9700\u8981\u5b89\u88c5 Openpyxl \u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bfb\u53d6 Excel \u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8bfb\u53d6\u4e24\u4e2a Excel \u6587\u4ef6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a Excel \u6587\u4ef6 <code>file1.xlsx<\/code> \u548c <code>file2.xlsx<\/code>\uff0c\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u90fd\u6709\u4e00\u4e2a\u5de5\u4f5c\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Pandas \u7684 <code>read_excel<\/code> \u51fd\u6570\u6765\u8bfb\u53d6\u8fd9\u4e24\u4e2a\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df1 = pd.read_excel(&#39;file1.xlsx&#39;)<\/p>\n<p>df2 = pd.read_excel(&#39;file2.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5408\u5e76\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5c06\u4e24\u4e2a DataFrame \u5408\u5e76\u5728\u4e00\u8d77\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528 <code>concat<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u53ea\u9700\u8981\u5c06\u4e24\u4e2a DataFrame \u6309\u884c\u6216\u6309\u5217\u7b80\u5355\u5730\u62fc\u63a5\u5728\u4e00\u8d77\uff0c\u53ef\u4ee5\u4f7f\u7528 Pandas \u7684 <code>concat<\/code> \u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u884c\u5408\u5e76<\/p>\n<p>result = pd.concat([df1, df2], axis=0)<\/p>\n<h2><strong>\u6309\u5217\u5408\u5e76<\/strong><\/h2>\n<p>result = pd.concat([df1, df2], axis=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 <code>merge<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u6839\u636e\u67d0\u4e9b\u952e\uff08key\uff09\u5c06\u4e24\u4e2a DataFrame \u5408\u5e76\u5728\u4e00\u8d77\uff0c\u53ef\u4ee5\u4f7f\u7528 Pandas \u7684 <code>merge<\/code> \u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6211\u4eec\u6839\u636e &#39;ID&#39; \u5217\u6765\u5408\u5e76<\/p>\n<p>result = pd.merge(df1, df2, on=&#39;ID&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>merge<\/code> \u51fd\u6570\u6709\u5f88\u591a\u53c2\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u7684\u5408\u5e76\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u5408\u5e76\u7684\u7c7b\u578b\uff08\u5185\u8fde\u63a5\u3001\u5916\u8fde\u63a5\u3001\u5de6\u8fde\u63a5\u3001\u53f3\u8fde\u63a5\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5185\u8fde\u63a5<\/p>\n<p>result = pd.merge(df1, df2, on=&#39;ID&#39;, how=&#39;inner&#39;)<\/p>\n<h2><strong>\u5916\u8fde\u63a5<\/strong><\/h2>\n<p>result = pd.merge(df1, df2, on=&#39;ID&#39;, how=&#39;outer&#39;)<\/p>\n<h2><strong>\u5de6\u8fde\u63a5<\/strong><\/h2>\n<p>result = pd.merge(df1, df2, on=&#39;ID&#39;, how=&#39;left&#39;)<\/p>\n<h2><strong>\u53f3\u8fde\u63a5<\/strong><\/h2>\n<p>result = pd.merge(df1, df2, on=&#39;ID&#39;, how=&#39;right&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528 <code>join<\/code> \u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4e24\u4e2a DataFrame \u6709\u76f8\u540c\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>join<\/code> \u51fd\u6570\u6765\u5408\u5e76\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = df1.join(df2, lsuffix=&#39;_left&#39;, rsuffix=&#39;_right&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5408\u5e76\u540e\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5408\u5e76\u6570\u636e\u540e\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\uff0c\u4f8b\u5982\u5220\u9664\u91cd\u590d\u7684\u5217\u3001\u5904\u7406\u7f3a\u5931\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5220\u9664\u91cd\u590d\u7684\u5217<\/h4>\n<\/p>\n<p><p>\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u91cd\u590d\u7684\u5217\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>drop<\/code> \u51fd\u6570\u6765\u5220\u9664\u8fd9\u4e9b\u91cd\u590d\u7684\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = result.loc[:,~result.columns.duplicated()]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u5408\u5e76\u6570\u636e\u540e\uff0c\u53ef\u80fd\u4f1a\u4ea7\u751f\u4e00\u4e9b\u7f3a\u5931\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>fillna<\/code> \u51fd\u6570\u6765\u5904\u7406\u8fd9\u4e9b\u7f3a\u5931\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7528 0 \u586b\u5145\u7f3a\u5931\u503c<\/p>\n<p>result = result.fillna(0)<\/p>\n<h2><strong>\u7528\u524d\u4e00\u4e2a\u503c\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>result = result.fillna(method=&#39;ffill&#39;)<\/p>\n<h2><strong>\u7528\u540e\u4e00\u4e2a\u503c\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>result = result.fillna(method=&#39;bfill&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4fdd\u5b58\u5408\u5e76\u540e\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u5b8c\u6570\u636e\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>to_excel<\/code> \u51fd\u6570\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u4e00\u4e2a\u65b0\u7684 Excel \u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result.to_excel(&#39;result.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u5982\u4f55\u7ed3\u5408\u4e24\u4e2a Excel \u8868\u683c\u5e76\u4fdd\u5b58\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6 Excel \u6587\u4ef6<\/strong><\/h2>\n<p>df1 = pd.read_excel(&#39;file1.xlsx&#39;)<\/p>\n<p>df2 = pd.read_excel(&#39;file2.xlsx&#39;)<\/p>\n<h2><strong>\u5408\u5e76\u6570\u636e<\/strong><\/h2>\n<p>result = pd.merge(df1, df2, on=&#39;ID&#39;, how=&#39;inner&#39;)<\/p>\n<h2><strong>\u5904\u7406\u5408\u5e76\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>result = result.loc[:,~result.columns.duplicated()]<\/p>\n<p>result = result.fillna(0)<\/p>\n<h2><strong>\u4fdd\u5b58\u7ed3\u679c<\/strong><\/h2>\n<p>result.to_excel(&#39;result.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u7ed3\u5408\u4e24\u4e2a Excel \u8868\u683c\u5e76\u4fdd\u5b58\u7ed3\u679c\u3002<strong>Pandas \u5e93\u529f\u80fd\u5f3a\u5927\u3001\u64cd\u4f5c\u7b80\u5355\uff0c\u662f\u5904\u7406 Excel \u6587\u4ef6\u7684\u7406\u60f3\u9009\u62e9<\/strong>\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002\u5982\u679c\u4f60\u6709\u66f4\u590d\u6742\u7684\u5408\u5e76\u9700\u6c42\uff0c\u53ef\u4ee5\u53c2\u8003 Pandas \u5b98\u65b9\u6587\u6863\uff0c\u4e86\u89e3\u66f4\u591a\u9ad8\u7ea7\u7528\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u4e24\u4e2aExcel\u8868\u683c\u5408\u5e76\u4e3a\u4e00\u4e2a\uff1f<\/strong><br \/>\u5c06\u4e24\u4e2aExcel\u8868\u683c\u5408\u5e76\u4e3a\u4e00\u4e2a\u53ef\u4ee5\u901a\u8fc7Pandas\u5e93\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Pandas\u5e93\u548cOpenPyXL\u5e93\u3002\u8bfb\u53d6\u4e24\u4e2aExcel\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pd.concat()<\/code>\u51fd\u6570\u5408\u5e76\u5b83\u4eec\uff0c\u6216\u8005\u5229\u7528<code>merge()<\/code>\u51fd\u6570\u57fa\u4e8e\u7279\u5b9a\u7684\u5217\u8fdb\u884c\u5408\u5e76\u3002\u5408\u5e76\u540e\uff0c\u4f7f\u7528<code>to_excel()<\/code>\u65b9\u6cd5\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u65b0\u7684Excel\u6587\u4ef6\u4e2d\u3002<\/p>\n<p><strong>\u5728\u5408\u5e76Excel\u8868\u683c\u65f6\uff0c\u5982\u4f55\u5904\u7406\u91cd\u590d\u7684\u6570\u636e\u884c\uff1f<\/strong><br \/>\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u91cd\u590d\u7684\u6570\u636e\u884c\u3002\u53ef\u4ee5\u5728\u4f7f\u7528<code>pd.concat()<\/code>\u6216<code>merge()<\/code>\u51fd\u6570\u540e\uff0c\u5229\u7528<code>drop_duplicates()<\/code>\u65b9\u6cd5\u6765\u53bb\u9664\u91cd\u590d\u884c\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5408\u5e76\u65f6\u7684\u53c2\u6570\u6765\u63a7\u5236\u5982\u4f55\u5904\u7406\u91cd\u590d\u6570\u636e\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u8bbe\u7f6e<code>how=&#39;outer&#39;<\/code>\u6216<code>how=&#39;inner&#39;<\/code>\u6765\u9009\u62e9\u4fdd\u7559\u54ea\u4e9b\u6570\u636e\u3002<\/p>\n<p><strong>\u5408\u5e76Excel\u8868\u683c\u65f6\uff0c\u5982\u4f55\u4fdd\u8bc1\u6570\u636e\u7684\u683c\u5f0f\u4e0d\u88ab\u6539\u53d8\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Pandas\u5408\u5e76Excel\u8868\u683c\u65f6\uff0c\u6570\u636e\u7684\u683c\u5f0f\u53ef\u80fd\u4f1a\u53d7\u5230\u5f71\u54cd\u3002\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u4e0d\u53d8\uff0c\u53ef\u4ee5\u5728\u8bfb\u53d6Excel\u6587\u4ef6\u65f6\u4f7f\u7528<code>dtype<\/code>\u53c2\u6570\u6307\u5b9a\u6bcf\u5217\u7684\u6570\u636e\u7c7b\u578b\u3002\u6b64\u5916\uff0c\u5408\u5e76\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>style<\/code>\u5c5e\u6027\u6765\u8c03\u6574\u8f93\u51faExcel\u7684\u6837\u5f0f\u3002\u786e\u4fdd\u5728\u4fdd\u5b58\u6587\u4ef6\u65f6\uff0c\u4f7f\u7528<code>to_excel()<\/code>\u65b9\u6cd5\u4e2d\u7684<code>engine=&#39;openpyxl&#39;<\/code>\u53c2\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u5730\u4fdd\u6301\u539f\u59cb\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7ed3\u5408\u4e24\u4e2aexcel\u8868\u683cpython \u7ed3\u5408\u4e24\u4e2a Excel \u8868\u683c\u5728 Python \u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1069759,"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\/1069749"}],"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=1069749"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069749\/revisions"}],"predecessor-version":[{"id":1069760,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069749\/revisions\/1069760"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069759"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}