{"id":1035509,"date":"2024-12-31T11:59:22","date_gmt":"2024-12-31T03:59:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1035509.html"},"modified":"2024-12-31T11:59:25","modified_gmt":"2024-12-31T03:59:25","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e6%95%b0%e6%8d%ae%e8%bd%ac%e5%8c%96%e6%88%90excel","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1035509.html","title":{"rendered":"python\u5982\u4f55\u628a\u6570\u636e\u8f6c\u5316\u6210excel"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/13375281-e57c-45a9-bc5e-0fe8820a138a.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u628a\u6570\u636e\u8f6c\u5316\u6210excel\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u8f6c\u5316\u4e3aExcel\u6587\u4ef6\uff0c\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u3001xlsxwriter\u5e93\u7b49\u3002\u4f7f\u7528pandas\u5e93\u3001\u521b\u5efaDataFrame\u5bf9\u8c61\u3001\u8c03\u7528to_excel\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>pandas\u5e93<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u529f\u80fd\u5f3a\u5927\uff0c\u800c\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u901a\u8fc7pandas\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u8f6c\u6362\u4e3aExcel\u6587\u4ef6\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528pandas\u5e93\u5c06\u6570\u636e\u8f6c\u6362\u4e3aExcel\u6587\u4ef6\uff0c\u5e76\u5c55\u793a\u5176\u4ed6\u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86pandas\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\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><h4>2. \u521b\u5efaDataFrame\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>pandas\u5e93\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u662fDataFrame\u3002\u4f60\u53ef\u4ee5\u5c06\u5404\u79cd\u6570\u636e\u7c7b\u578b\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\u3001Numpy\u6570\u7ec4\u7b49\uff09\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Anna&#39;, &#39;Peter&#39;, &#39;Linda&#39;],<\/p>\n<p>    &#39;Age&#39;: [28, 24, 35, 32],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Paris&#39;, &#39;Berlin&#39;, &#39;London&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5c06DataFrame\u5bf9\u8c61\u8f6c\u6362\u4e3aExcel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pandas\u5e93\u7684<code>to_excel<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06DataFrame\u5bf9\u8c61\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u5de5\u4f5c\u8868\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06DataFrame\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6<\/p>\n<p>df.to_excel(&#39;output.xlsx&#39;, sheet_name=&#39;Sheet1&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>index=False<\/code>\u53c2\u6570\u8868\u793a\u4e0d\u4fdd\u5b58\u884c\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528openpyxl\u5e93<\/h3>\n<\/p>\n<p><p>openpyxl\u662f\u4e00\u4e2a\u7528\u4e8e\u64cd\u4f5cExcel\u6587\u4ef6\u7684\u5e93\uff0c\u652f\u6301Excel 2010\u53ca\u66f4\u9ad8\u7248\u672c\u7684.xlsx\u6587\u4ef6\u3002\u5b83\u80fd\u591f\u8bfb\u53d6\u548c\u5199\u5165Excel\u6587\u4ef6\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5openpyxl\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u548c\u4fdd\u5b58Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u7c3f\u5bf9\u8c61<\/strong><\/h2>\n<p>wb = Workbook()<\/p>\n<h2><strong>\u6fc0\u6d3b\u9ed8\u8ba4\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u5411\u5de5\u4f5c\u8868\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>ws.append([&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;])<\/p>\n<p>ws.append([&#39;John&#39;, 28, &#39;New York&#39;])<\/p>\n<p>ws.append([&#39;Anna&#39;, 24, &#39;Paris&#39;])<\/p>\n<p>ws.append([&#39;Peter&#39;, 35, &#39;Berlin&#39;])<\/p>\n<p>ws.append([&#39;Linda&#39;, 32, &#39;London&#39;])<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&#39;output.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528xlsxwriter\u5e93<\/h3>\n<\/p>\n<p><p>xlsxwriter\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u521b\u5efaExcel\u6587\u4ef6\u7684\u5e93\uff0c\u652f\u6301Excel 97\u53ca\u66f4\u9ad8\u7248\u672c\u7684.xlsx\u6587\u4ef6\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5xlsxwriter\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install xlsxwriter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u548c\u4fdd\u5b58Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import xlsxwriter<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u7c3f\u5bf9\u8c61<\/strong><\/h2>\n<p>workbook = xlsxwriter.Workbook(&#39;output.xlsx&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u4e00\u4e2a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>worksheet = workbook.add_worksheet()<\/p>\n<h2><strong>\u5411\u5de5\u4f5c\u8868\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;John&#39;, 28, &#39;New York&#39;],<\/p>\n<p>    [&#39;Anna&#39;, 24, &#39;Paris&#39;],<\/p>\n<p>    [&#39;Peter&#39;, 35, &#39;Berlin&#39;],<\/p>\n<p>    [&#39;Linda&#39;, 32, &#39;London&#39;]<\/p>\n<p>]<\/p>\n<p>for row_num, row_data in enumerate(data):<\/p>\n<p>    for col_num, col_data in enumerate(row_data):<\/p>\n<p>        worksheet.write(row_num, col_num, col_data)<\/p>\n<h2><strong>\u5173\u95ed\u5de5\u4f5c\u7c3f\u5bf9\u8c61<\/strong><\/h2>\n<p>workbook.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001pandas\u5e93\u8be6\u7ec6\u4ecb\u7ecd<\/h3>\n<\/p>\n<p><h4>1. pandas\u5e93\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\u3002\u5b83\u5efa\u7acb\u5728Numpy\u5e93\u4e4b\u4e0a\uff0c\u5177\u6709\u66f4\u9ad8\u5c42\u6b21\u7684\u6570\u636e\u64cd\u4f5c\u80fd\u529b\uff0c\u9002\u5408\u4e8e\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u8f6c\u6362\u3001\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u53ef\u89c6\u5316\u7b49\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>2. \u521b\u5efaDataFrame<\/h4>\n<\/p>\n<p><p>DataFrame\u662fpandas\u5e93\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u7535\u5b50\u8868\u683c\u6216SQL\u8868\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521b\u5efaDataFrame\u5bf9\u8c61\uff0c\u4f8b\u5982\u4ece\u5b57\u5178\u3001\u5217\u8868\u3001Numpy\u6570\u7ec4\u3001CSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4ece\u5b57\u5178\u521b\u5efaDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Anna&#39;, &#39;Peter&#39;, &#39;Linda&#39;],<\/p>\n<p>    &#39;Age&#39;: [28, 24, 35, 32],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Paris&#39;, &#39;Berlin&#39;, &#39;London&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4ece\u5217\u8868\u521b\u5efaDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&#39;John&#39;, 28, &#39;New York&#39;],<\/p>\n<p>    [&#39;Anna&#39;, 24, &#39;Paris&#39;],<\/p>\n<p>    [&#39;Peter&#39;, 35, &#39;Berlin&#39;],<\/p>\n<p>    [&#39;Linda&#39;, 32, &#39;London&#39;]<\/p>\n<p>]<\/p>\n<p>df = pd.DataFrame(data, columns=[&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;])<\/p>\n<h2><strong>\u4eceNumpy\u6570\u7ec4\u521b\u5efaDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>import numpy as np<\/p>\n<p>data = np.array([<\/p>\n<p>    [&#39;John&#39;, 28, &#39;New York&#39;],<\/p>\n<p>    [&#39;Anna&#39;, 24, &#39;Paris&#39;],<\/p>\n<p>    [&#39;Peter&#39;, 35, &#39;Berlin&#39;],<\/p>\n<p>    [&#39;Linda&#39;, 32, &#39;London&#39;]<\/p>\n<p>])<\/p>\n<p>df = pd.DataFrame(data, columns=[&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6570\u636e\u5904\u7406\u548c\u5206\u6790<\/h4>\n<\/p>\n<p><p>pandas\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u529f\u80fd\uff0c\u4f8b\u5982\u6570\u636e\u9009\u62e9\u3001\u8fc7\u6ee4\u3001\u6392\u5e8f\u3001\u805a\u5408\u3001\u5408\u5e76\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u636e\u9009\u62e9<\/p>\n<p>print(df[&#39;Name&#39;])<\/p>\n<p>print(df[[&#39;Name&#39;, &#39;Age&#39;]])<\/p>\n<p>print(df.iloc[0])<\/p>\n<p>print(df.iloc[0:2])<\/p>\n<p>print(df[df[&#39;Age&#39;] &gt; 30])<\/p>\n<h2><strong>\u6570\u636e\u8fc7\u6ee4<\/strong><\/h2>\n<p>print(df[df[&#39;City&#39;] == &#39;New York&#39;])<\/p>\n<p>print(df[df[&#39;Age&#39;] &gt; 30])<\/p>\n<h2><strong>\u6570\u636e\u6392\u5e8f<\/strong><\/h2>\n<p>print(df.sort_values(by=&#39;Age&#39;))<\/p>\n<p>print(df.sort_values(by=[&#39;City&#39;, &#39;Age&#39;]))<\/p>\n<h2><strong>\u6570\u636e\u805a\u5408<\/strong><\/h2>\n<p>print(df.groupby(&#39;City&#39;).sum())<\/p>\n<p>print(df.groupby(&#39;City&#39;).mean())<\/p>\n<h2><strong>\u6570\u636e\u5408\u5e76<\/strong><\/h2>\n<p>data1 = {<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Anna&#39;],<\/p>\n<p>    &#39;Age&#39;: [28, 24]<\/p>\n<p>}<\/p>\n<p>data2 = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Peter&#39;, &#39;Linda&#39;],<\/p>\n<p>    &#39;Age&#39;: [35, 32]<\/p>\n<p>}<\/p>\n<p>df1 = pd.DataFrame(data1)<\/p>\n<p>df2 = pd.DataFrame(data2)<\/p>\n<p>df = pd.concat([df1, df2])<\/p>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Anna&#39;, &#39;Peter&#39;, &#39;Linda&#39;],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Paris&#39;, &#39;Berlin&#39;, &#39;London&#39;]<\/p>\n<p>}<\/p>\n<p>df1 = pd.DataFrame(data)<\/p>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Anna&#39;, &#39;Peter&#39;, &#39;Linda&#39;],<\/p>\n<p>    &#39;Age&#39;: [28, 24, 35, 32]<\/p>\n<p>}<\/p>\n<p>df2 = pd.DataFrame(data)<\/p>\n<p>df = pd.merge(df1, df2, on=&#39;Name&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u6570\u636e\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>pandas\u5e93\u53ef\u4ee5\u4e0ematplotlib\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u521b\u5efa\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u3001\u997c\u56fe\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Anna&#39;, &#39;Peter&#39;, &#39;Linda&#39;],<\/p>\n<p>    &#39;Age&#39;: [28, 24, 35, 32],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Paris&#39;, &#39;Berlin&#39;, &#39;London&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521b\u5efa\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>df.plot(kind=&#39;bar&#39;, x=&#39;Name&#39;, y=&#39;Age&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u521b\u5efa\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>df.plot(kind=&#39;line&#39;, x=&#39;Name&#39;, y=&#39;Age&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u521b\u5efa\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>df.plot(kind=&#39;scatter&#39;, x=&#39;Name&#39;, y=&#39;Age&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u521b\u5efa\u997c\u56fe<\/strong><\/h2>\n<p>df.plot(kind=&#39;pie&#39;, y=&#39;Age&#39;, labels=df[&#39;Name&#39;], autopct=&#39;%1.1f%%&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u8f6c\u6362\u4e3aExcel\u6587\u4ef6\uff0c\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u3001xlsxwriter\u5e93\u7b49\u3002\u5176\u4e2d\uff0c<strong>pandas\u5e93<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u529f\u80fd\u5f3a\u5927\uff0c\u800c\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u901a\u8fc7pandas\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u8f6c\u6362\u4e3aExcel\u6587\u4ef6\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u5904\u7406Excel\u6587\u4ef6\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fPandas\u548cOpenPyXL\u3002\u4f7f\u7528Pandas\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06DataFrame\u5bf9\u8c61\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\u3002\u53ea\u9700\u4f7f\u7528<code>to_excel()<\/code>\u65b9\u6cd5\uff0c\u5e76\u6307\u5b9a\u6587\u4ef6\u540d\u5373\u53ef\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<\/p>\n<p><strong>\u5728\u8f6c\u6362\u6570\u636e\u4e3aExcel\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728\u6570\u636e\u8f6c\u6362\u8fc7\u7a0b\u4e2d\uff0c\u7f3a\u5931\u503c\u53ef\u80fd\u4f1a\u5f71\u54cd\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002\u53ef\u4ee5\u5728\u4f7f\u7528Pandas\u65f6\uff0c\u901a\u8fc7<code>fillna()<\/code>\u65b9\u6cd5\u6765\u586b\u8865\u7f3a\u5931\u503c\uff0c\u6216\u8005\u9009\u62e9\u4f7f\u7528<code>dropna()<\/code>\u65b9\u6cd5\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u3002\u5728\u5bfc\u51fa\u524d\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u51c6\u786e\u6027\uff0c\u8fd9\u6837\u751f\u6210\u7684Excel\u6587\u4ef6\u4f1a\u66f4\u52a0\u53ef\u9760\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u591a\u4e2a\u6570\u636e\u8868\u5bfc\u51fa\u5230\u540c\u4e00\u4e2aExcel\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7Pandas\u7684ExcelWriter\u5bf9\u8c61\u5b9e\u73b0\u5c06\u591a\u4e2a\u6570\u636e\u8868\u5bfc\u51fa\u5230\u540c\u4e00\u4e2aExcel\u6587\u4ef6\u4e2d\u3002\u60a8\u53ef\u4ee5\u4e3a\u6bcf\u4e2aDataFrame\u6307\u5b9a\u4e00\u4e2a\u4e0d\u540c\u7684\u5de5\u4f5c\u8868\u540d\u79f0\uff0c\u4f8b\u5982\u4f7f\u7528<code>ExcelWriter(&#39;filename.xlsx&#39;)<\/code>\u521b\u5efa\u4e00\u4e2a\u5199\u5165\u5668\uff0c\u7136\u540e\u901a\u8fc7<code>to_excel()<\/code>\u65b9\u6cd5\u5206\u522b\u5c06\u4e0d\u540c\u7684DataFrame\u5199\u5165\u4e0d\u540c\u7684\u5de5\u4f5c\u8868\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u9700\u8981\u5c06\u76f8\u5173\u6570\u636e\u5206\u7ec4\u5b58\u50a8\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u8f6c\u5316\u4e3aExcel\u6587\u4ef6\uff0c\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u3001xlsxw [&hellip;]","protected":false},"author":3,"featured_media":1035519,"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\/1035509"}],"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=1035509"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035509\/revisions"}],"predecessor-version":[{"id":1035522,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035509\/revisions\/1035522"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1035519"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1035509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1035509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1035509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}