{"id":1018894,"date":"2024-12-27T12:47:51","date_gmt":"2024-12-27T04:47:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1018894.html"},"modified":"2024-12-27T12:48:02","modified_gmt":"2024-12-27T04:48:02","slug":"python%e5%a6%82%e4%bd%95%e6%8a%93%e5%8f%96exlce%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1018894.html","title":{"rendered":"python\u5982\u4f55\u6293\u53d6exlce\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161513\/37005956-2257-4ae1-9f06-f6ba9be905ab.webp\" alt=\"python\u5982\u4f55\u6293\u53d6exlce\u6570\u636e\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u6293\u53d6Excel\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b8c\u6210\uff0c<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u3001xlrd\u5e93<\/strong>\u3002\u5176\u4e2d\uff0cpandas\u5e93\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u800c\u5f3a\u5927\u7684\u65b9\u6cd5\u6765\u8bfb\u53d6\u548c\u64cd\u4f5cExcel\u6570\u636e\u3002openpyxl\u5e93\u4e3b\u8981\u7528\u4e8e\u5904\u7406Excel\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\uff0c\u800cxlrd\u5e93\u4e5f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u4f46\u5b83\u66f4\u9002\u5408\u65e7\u7248\u672c\u7684Excel\u6587\u4ef6\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548cExcel\u6587\u4ef6\u7684\u683c\u5f0f\u6765\u51b3\u5b9a<\/strong>\uff0c\u5982\u6587\u4ef6\u5927\u5c0f\u3001\u590d\u6742\u6027\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528pandas\u5e93\u8bfb\u53d6Excel\u6570\u636e\u7684\u8fc7\u7a0b\u3002pandas\u5e93\u662f\u4e00\u4e2a\u6570\u636e\u5206\u6790\u7684\u5229\u5668\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406Excel\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406CSV\u3001JSON\u7b49\u591a\u79cd\u683c\u5f0f\u7684\u6570\u636e\u6587\u4ef6\u3002\u4f7f\u7528pandas\u8bfb\u53d6Excel\u6587\u4ef6\u7684\u6b65\u9aa4\u5927\u81f4\u5305\u62ec\u5b89\u88c5pandas\u5e93\u3001\u4f7f\u7528<code>read_excel<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6570\u636e\u3001\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\u7b49\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u9ad8\u6548\u5730\u4eceExcel\u6587\u4ef6\u4e2d\u6293\u53d6\u6570\u636e\uff0c\u5e76\u5728Python\u4e2d\u8fdb\u884c\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u5904\u7406Excel\u6587\u4ef6\u4e4b\u524d\uff0c\u786e\u4fdd\u5b89\u88c5\u5e76\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002pandas\u5e93\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u800copenpyxl\u5e93\u901a\u5e38\u4f5c\u4e3apandas\u8bfb\u53d6Excel\u6587\u4ef6\u7684\u9ed8\u8ba4\u5f15\u64ce\u4f7f\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5pandas\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u547d\u4ee4\u5c06\u4e0b\u8f7d\u5e76\u5b89\u88c5pandas\u53ca\u5176\u76f8\u5173\u4f9d\u8d56\u3002\u5982\u679c\u9700\u8981\u8bfb\u53d6\u8f83\u65b0\u7684Excel\u6587\u4ef6\uff0c\u5efa\u8bae\u540c\u65f6\u5b89\u88c5openpyxl\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u5165\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u60a8\u7684Python\u811a\u672c\u6216Jupyter Notebook\u4e2d\uff0c\u5bfc\u5165pandas\u548c\u5176\u4ed6\u53ef\u80fd\u9700\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><p>\u5982\u679c\u8ba1\u5212\u5904\u7406\u5927\u578bExcel\u6587\u4ef6\u6216\u9700\u8981\u7279\u5b9a\u529f\u80fd\uff0c\u8fd8\u53ef\u4ee5\u5bfc\u5165\u76f8\u5173\u5e93\uff0c\u5982numpy\u7b49\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528pandas\u8bfb\u53d6Excel\u6587\u4ef6<\/p>\n<\/p>\n<p><p>pandas\u5e93\u63d0\u4f9b\u4e86<code>read_excel<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5177\u6709\u7075\u6d3b\u7684\u53c2\u6570\u8bbe\u7f6e\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570\u9700\u6c42\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>read_excel<\/code>\u65b9\u6cd5\u8bfb\u53d6Excel\u6587\u4ef6\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u4f8b\u4e2d\uff0c<code>df<\/code>\u662f\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u8868\u793aExcel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6307\u5b9a\u5de5\u4f5c\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679cExcel\u6587\u4ef6\u4e2d\u6709\u591a\u4e2a\u5de5\u4f5c\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sheet_name<\/code>\u53c2\u6570\u6307\u5b9a\u8981\u8bfb\u53d6\u7684\u5de5\u4f5c\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;, sheet_name=&#39;\u5de5\u4f5c\u8868\u540d\u79f0&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5de5\u4f5c\u8868\u7684\u7d22\u5f15\u6765\u6307\u5b9a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;, sheet_name=0)  # \u8bfb\u53d6\u7b2c\u4e00\u4e2a\u5de5\u4f5c\u8868<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u7279\u5b9a\u7684\u5217<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u53ea\u9700\u8981\u8bfb\u53d6\u7279\u5b9a\u7684\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>usecols<\/code>\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;, usecols=[&#39;\u5217\u540d1&#39;, &#39;\u5217\u540d2&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u505a\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\uff0c\u5c24\u5176\u5728\u5904\u7406\u5927\u578b\u6587\u4ef6\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5bf9\u8bfb\u53d6\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406<\/p>\n<\/p>\n<p><p>\u8bfb\u53d6Excel\u6570\u636e\u540e\uff0c\u901a\u5e38\u9700\u8981\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u8f6c\u6362\u6216\u5206\u6790\u3002pandas\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u65b9\u6cd5\u6765\u5904\u7406DataFrame\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u67e5\u770b\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u67e5\u770bDataFrame\u7684\u524d\u51e0\u884c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>head<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51faDataFrame\u7684\u524d\u4e94\u884c\u6570\u636e\uff0c\u4ee5\u4fbf\u5feb\u901f\u68c0\u67e5\u6570\u636e\u7684\u683c\u5f0f\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u6e05\u6d17<\/strong><\/p>\n<\/p>\n<p><p>\u5904\u7406\u7f3a\u5931\u503c\u662f\u6570\u636e\u6e05\u6d17\u7684\u91cd\u8981\u6b65\u9aa4\u4e4b\u4e00\u3002\u53ef\u4ee5\u4f7f\u7528<code>dropna<\/code>\u65b9\u6cd5\u5220\u9664\u542b\u6709\u7f3a\u5931\u503c\u7684\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_cleaned = df.dropna()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528<code>fillna<\/code>\u65b9\u6cd5\u7528\u6307\u5b9a\u503c\u586b\u5145\u7f3a\u5931\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df_filled = df.fillna(value=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u8f6c\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u6570\u636e\u8f6c\u6362\u53ef\u80fd\u6d89\u53ca\u66f4\u6539\u6570\u636e\u7c7b\u578b\u3001\u521b\u5efa\u65b0\u7684\u5217\u7b49\u3002\u4f8b\u5982\uff0c\u5c06\u67d0\u5217\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;\u5217\u540d&#39;] = df[&#39;\u5217\u540d&#39;].astype(int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u64cd\u4f5c\u521b\u5efa\u65b0\u7684\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;\u65b0\u5217&#39;] = df[&#39;\u5217\u540d1&#39;] + df[&#39;\u5217\u540d2&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5206\u6790\u548c\u53ef\u89c6\u5316\u6570\u636e<\/p>\n<\/p>\n<p><p>\u4e00\u65e6\u6570\u636e\u88ab\u8bfb\u53d6\u548c\u6e05\u6d17\uff0c\u4e0b\u4e00\u6b65\u901a\u5e38\u662f\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002pandas\u4e0ematplotlib\u5e93\u7ed3\u5408\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5f3a\u5927\u7684\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7edf\u8ba1\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>describe<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6DataFrame\u7684\u7edf\u8ba1\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(df.describe())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u5305\u62ec\u5747\u503c\u3001\u6807\u51c6\u5dee\u3001\u6700\u5c0f\u503c\u3001\u56db\u5206\u4f4d\u6570\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u5206\u7ec4\u548c\u805a\u5408<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>groupby<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u548c\u805a\u5408\u5206\u6790\u3002\u4f8b\u5982\uff0c\u6309\u67d0\u5217\u5206\u7ec4\u5e76\u8ba1\u7b97\u5e73\u5747\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">grouped = df.groupby(&#39;\u5206\u7ec4\u5217&#39;).mean()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u53ef\u89c6\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528matplotlib\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u591a\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u67f1\u72b6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>df[&#39;\u5217\u540d&#39;].value_counts().plot(kind=&#39;bar&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u751f\u6210\u4e00\u4e2a\u67f1\u72b6\u56fe\uff0c\u663e\u793a\u6bcf\u4e2a\u7c7b\u522b\u7684\u9891\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5904\u7406\u5927\u578bExcel\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578bExcel\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5185\u5b58\u4e0d\u8db3\u7684\u95ee\u9898\u3002\u6b64\u65f6\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5206\u5757\u8bfb\u53d6\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5206\u5757\u8bfb\u53d6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>read_excel<\/code>\u7684<code>chunksize<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u5c06Excel\u6587\u4ef6\u5206\u5757\u8bfb\u53d6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">chunk_size = 1000<\/p>\n<p>for chunk in pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;, chunksize=chunk_size):<\/p>\n<p>    # \u5bf9\u6bcf\u4e2a\u5757\u8fdb\u884c\u5904\u7406<\/p>\n<p>    print(chunk.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5904\u7406\u975e\u5e38\u5927\u7684\u6587\u4ef6\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316\u5185\u5b58\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6570\u636e\u7c7b\u578b\u6765\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u6307\u5b9a\u67d0\u5217\u7684\u6570\u636e\u7c7b\u578b\u4e3a\u6574\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;, dtype={&#39;\u5217\u540d&#39;: int})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u53ef\u4ee5\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5185\u5b58\u5f00\u9500\uff0c\u63d0\u9ad8\u5904\u7406\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u5199\u5165Excel\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6570\u636e\u540e\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u7ed3\u679c\u5199\u5165\u65b0\u7684Excel\u6587\u4ef6\u3002pandas\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5199\u5165Excel\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>to_excel<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06DataFrame\u5199\u5165Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.to_excel(&#39;\u8f93\u51fa\u8def\u5f84.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>index=False<\/code>\u53c2\u6570\u8868\u793a\u4e0d\u5199\u5165\u884c\u7d22\u5f15\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5199\u5165\u591a\u4e2a\u5de5\u4f5c\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5c06\u591a\u4e2aDataFrame\u5199\u5165\u4e00\u4e2aExcel\u6587\u4ef6\u7684\u4e0d\u540c\u5de5\u4f5c\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528ExcelWriter\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with pd.ExcelWriter(&#39;\u8f93\u51fa\u8def\u5f84.xlsx&#39;) as writer:<\/p>\n<p>    df1.to_excel(writer, sheet_name=&#39;\u5de5\u4f5c\u88681&#39;)<\/p>\n<p>    df2.to_excel(writer, sheet_name=&#39;\u5de5\u4f5c\u88682&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u591a\u4e2a\u5de5\u4f5c\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u9ad8\u6548\u5730\u4eceExcel\u6587\u4ef6\u4e2d\u6293\u53d6\u6570\u636e\uff0c\u5e76\u5728Python\u4e2d\u8fdb\u884c\u5904\u7406\u3001\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u548c\u5e93\u6709\u5404\u81ea\u7684\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u9002\u5408\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u6570\u636e\u5904\u7406\u5de5\u4f5c\u66f4\u52a0\u987a\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u5982<code>pandas<\/code>\u548c<code>openpyxl<\/code>\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>pandas.read_excel()<\/code>\u51fd\u6570\u8f7b\u677e\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u652f\u6301\u591a\u79cd\u683c\u5f0f\uff0c\u5305\u62ec<code>.xls<\/code>\u548c<code>.xlsx<\/code>\u3002\u786e\u4fdd\u5b89\u88c5\u4e86\u76f8\u5e94\u7684\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install pandas openpyxl<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u8bfb\u53d6\u540e\uff0c\u6570\u636e\u5c06\u4ee5DataFrame\u7684\u5f62\u5f0f\u5448\u73b0\uff0c\u65b9\u4fbf\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<p><strong>\u5728\u6293\u53d6Excel\u6570\u636e\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7a7a\u503c\u6216\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u5904\u7406Excel\u6570\u636e\u65f6\uff0c\u7a7a\u503c\u6216\u7f3a\u5931\u503c\u662f\u5e38\u89c1\u95ee\u9898\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u8fd9\u4e9b\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>DataFrame.fillna()<\/code>\u65b9\u6cd5\u6765\u586b\u5145\u7f3a\u5931\u503c\uff0c\u6216\u8005\u4f7f\u7528<code>DataFrame.dropna()<\/code>\u65b9\u6cd5\u5220\u9664\u5305\u542b\u7a7a\u503c\u7684\u884c\u3002\u6839\u636e\u4f60\u7684\u6570\u636e\u5206\u6790\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u6293\u53d6\u5230\u7684Excel\u6570\u636e\u4fdd\u5b58\u56deExcel\u6587\u4ef6\uff1f<\/strong><br \/>\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u4fdd\u5b58\u56deExcel\u6587\u4ef6\u4e5f\u5f88\u7b80\u5355\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>DataFrame.to_excel()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4f60\u53ea\u9700\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u4fdd\u5b58\u8def\u5f84\uff0c\u6570\u636e\u5c31\u4f1a\u88ab\u5199\u5165\u65b0\u7684Excel\u6587\u4ef6\u4e2d\u3002\u786e\u4fdd\u5728\u4fdd\u5b58\u65f6\u8bbe\u7f6e\u5408\u9002\u7684\u53c2\u6570\uff0c\u6bd4\u5982<code>index=False<\/code>\u4ee5\u907f\u514d\u5c06\u884c\u7d22\u5f15\u5199\u5165\u6587\u4ef6\u4e2d\uff0c\u4fdd\u6301\u6587\u4ef6\u7684\u6574\u6d01\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6293\u53d6Excel\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b8c\u6210\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1018913,"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\/1018894"}],"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=1018894"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018894\/revisions"}],"predecessor-version":[{"id":1018914,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1018894\/revisions\/1018914"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1018913"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1018894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1018894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1018894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}