{"id":1021950,"date":"2024-12-27T13:28:54","date_gmt":"2024-12-27T05:28:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1021950.html"},"modified":"2024-12-27T13:28:57","modified_gmt":"2024-12-27T05:28:57","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%88%b6%e4%bd%9c%e6%8a%a5%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1021950.html","title":{"rendered":"\u5982\u4f55\u7528python\u5236\u4f5c\u62a5\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25164305\/dfe66806-f06a-4ff7-999c-c5a5ac413ace.webp\" alt=\"\u5982\u4f55\u7528python\u5236\u4f5c\u62a5\u8868\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5236\u4f5c\u62a5\u8868\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\u3002<strong>\u5229\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u3001\u901a\u8fc7Matplotlib\u6216Seaborn\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3001\u4f7f\u7528ReportLab\u751f\u6210PDF\u3001\u91c7\u7528openpyxl\u6216xlsxwriter\u521b\u5efaExcel\u62a5\u8868<\/strong>\uff0c\u8fd9\u4e9b\u6b65\u9aa4\u6784\u6210\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684\u62a5\u8868\u5236\u4f5c\u6d41\u7a0b\u3002\u5728\u8fd9\u5176\u4e2d\uff0cPandas\u662f\u6570\u636e\u5904\u7406\u7684\u6838\u5fc3\u5de5\u5177\uff0c\u800cMatplotlib\u548cSeaborn\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u53ef\u89c6\u5316\u529f\u80fd\u3002\u8be6\u7ec6\u7684\u62a5\u8868\u53ef\u4ee5\u901a\u8fc7ReportLab\u751f\u6210PDF\u683c\u5f0f\uff0c\u6216\u8005\u901a\u8fc7openpyxl\u548cxlsxwriter\u521b\u5efaExcel\u6587\u4ef6\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u5236\u4f5c\u4e13\u4e1a\u7684Python\u62a5\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6570\u636e\u5904\u7406\u4e0e\u5206\u6790<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684Pandas\u5e93\u662f\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u62a5\u8868\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1.1 Pandas\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>Pandas\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u662fSeries\u548cDataFrame\u3002<strong>Series\u662f\u4e00\u7ef4\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u5e26\u6807\u7b7e\u7684\u6570\u7ec4\uff1bDataFrame\u662f\u4e8c\u7ef4\u7684\u8868\u683c\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8e\u7535\u5b50\u8868\u683c\u6216SQL\u8868\u3002<\/strong>DataFrame\u7684\u6bcf\u4e00\u5217\u90fd\u662f\u4e00\u4e2aSeries\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efaDataFrame\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\uff0c\u5982\u4ece\u5b57\u5178\u3001\u5217\u8868\u3001Numpy\u6570\u7ec4\u6216\u8bfb\u53d6\u6587\u4ef6\uff08CSV\u3001Excel\u7b49\uff09\u3002\u4f8b\u5982\uff0c\u4ece\u5b57\u5178\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684DataFrame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35],<\/p>\n<p>    &#39;Salary&#39;: [50000, 60000, 70000]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u6570\u636e\u6e05\u6d17\u4e0e\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u6e05\u6d17\u5305\u62ec\u5904\u7406\u7f3a\u5931\u503c\u3001\u91cd\u590d\u6570\u636e\u3001\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7b49\u3002Pandas\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\u6765\u7b80\u5316\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5904\u7406\u7f3a\u5931\u503c<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>df.dropna()<\/code>\u5220\u9664\u7f3a\u5931\u503c\uff0c\u6216\u4f7f\u7528<code>df.fillna(value)<\/code>\u586b\u5145\u7f3a\u5931\u503c\u3002<\/li>\n<li><strong>\u5220\u9664\u91cd\u590d\u9879<\/strong>\uff1a\u4f7f\u7528<code>df.drop_duplicates()<\/code>\u53ef\u4ee5\u5220\u9664\u91cd\u590d\u884c\u3002<\/li>\n<li><strong>\u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/strong>\uff1a\u4f7f\u7528<code>df.astype()<\/code>\u53ef\u4ee5\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;Age&#39;] = df[&#39;Age&#39;].fillna(df[&#39;Age&#39;].mean())  # \u586b\u5145\u7f3a\u5931\u7684\u5e74\u9f84\u503c<\/p>\n<p>df.drop_duplicates(inplace=True)  # \u5220\u9664\u91cd\u590d\u884c<\/p>\n<p>df[&#39;Salary&#39;] = df[&#39;Salary&#39;].astype(float)  # \u5c06\u85aa\u6c34\u8f6c\u6362\u4e3a\u6d6e\u70b9\u578b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3 \u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u529f\u80fd\uff0c\u5982\u5206\u7ec4\u3001\u805a\u5408\u3001\u900f\u89c6\u8868\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5206\u7ec4\u4e0e\u805a\u5408<\/strong>\uff1a\u4f7f\u7528<code>groupby()<\/code>\u51fd\u6570\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u5e76\u805a\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">grouped = df.groupby(&#39;Department&#39;).agg({&#39;Salary&#39;: &#39;mean&#39;, &#39;Age&#39;: &#39;max&#39;})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u900f\u89c6\u8868<\/strong>\uff1a\u4f7f\u7528<code>pivot_table()<\/code>\u53ef\u4ee5\u521b\u5efa\u7c7b\u4f3cExcel\u7684\u900f\u89c6\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pivot = df.pivot_table(index=&#39;Department&#39;, values=&#39;Salary&#39;, aggfunc=&#39;mean&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u6570\u636e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u53ef\u89c6\u5316\u662f\u62a5\u8868\u5236\u4f5c\u7684\u91cd\u8981\u90e8\u5206\u3002Python\u4e2d\u6709\u8bb8\u591a\u53ef\u89c6\u5316\u5e93\uff0c\u5982Matplotlib\u3001Seaborn\u3001Plotly\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2.1 Matplotlib<\/h4>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u57fa\u7840\u7684\u7ed8\u56fe\u5e93\uff0c\u529f\u80fd\u5f3a\u5927\uff0c\u53ef\u4ee5\u521b\u5efa\u591a\u79cd\u56fe\u8868\uff0c\u5982\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u997c\u56fe\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u6298\u7ebf\u56fe<\/strong>\uff1a\u9002\u5408\u5c55\u793a\u6570\u636e\u7684\u53d8\u5316\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.plot(df[&#39;Date&#39;], df[&#39;Sales&#39;])<\/p>\n<p>plt.title(&#39;Sales Over Time&#39;)<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Sales&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u67f1\u72b6\u56fe<\/strong>\uff1a\u9002\u5408\u6bd4\u8f83\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.bar(df[&#39;Product&#39;], df[&#39;Sales&#39;])<\/p>\n<p>plt.title(&#39;Sales by Product&#39;)<\/p>\n<p>plt.xlabel(&#39;Product&#39;)<\/p>\n<p>plt.ylabel(&#39;Sales&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2.2 Seaborn<\/h4>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684API\u548c\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u7bb1\u7ebf\u56fe<\/strong>\uff1a\u7528\u4e8e\u5c55\u793a\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>sns.boxplot(x=&#39;Category&#39;, y=&#39;Sales&#39;, data=df)<\/p>\n<p>plt.title(&#39;Sales Distribution by Category&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u70ed\u529b\u56fe<\/strong>\uff1a\u9002\u5408\u5c55\u793a\u77e9\u9635\u6570\u636e\u7684\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sns.heatmap(df.corr(), annot=True, cmap=&#39;coolwarm&#39;)<\/p>\n<p>plt.title(&#39;Correlation Matrix&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u751f\u6210PDF\u62a5\u8868<\/h3>\n<\/p>\n<p><p>\u751f\u6210PDF\u62a5\u8868\u53ef\u4ee5\u4f7f\u7528ReportLab\u5e93\uff0c\u5b83\u662f\u4e00\u4e2a\u5f3a\u5927\u7684PDF\u751f\u6210\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684PDF\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5ReportLab<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5ReportLab\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install reportlab<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u521b\u5efaPDF\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528ReportLab\u521b\u5efaPDF\u6587\u6863\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7Canvas\u5bf9\u8c61\u7ed8\u5236\u6587\u672c\u3001\u56fe\u5f62\u548c\u56fe\u50cf\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u521b\u5efaCanvas<\/strong>\uff1a\u9996\u5148\u521b\u5efa\u4e00\u4e2aCanvas\u5bf9\u8c61\uff0c\u6307\u5b9aPDF\u6587\u4ef6\u7684\u540d\u79f0\u548c\u5c3a\u5bf8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from reportlab.pdfgen import canvas<\/p>\n<p>c = canvas.Canvas(&quot;report.pdf&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed8\u5236\u6587\u672c<\/strong>\uff1a\u4f7f\u7528<code>drawString()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5728PDF\u4e0a\u7ed8\u5236\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">c.drawString(100, 750, &quot;Sales Report&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed8\u5236\u56fe\u5f62\u548c\u56fe\u50cf<\/strong>\uff1a\u4f7f\u7528<code>drawImage()<\/code>\u53ef\u4ee5\u5728PDF\u4e2d\u63d2\u5165\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">c.drawImage(&quot;chart.png&quot;, 100, 500, width=200, height=100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u5b58PDF<\/strong>\uff1a\u6700\u540e\u8c03\u7528<code>save()<\/code>\u65b9\u6cd5\u4fdd\u5b58PDF\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">c.save()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>3.3 \u6dfb\u52a0\u8868\u683c<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528ReportLab\u7684Table\u7c7b\u6765\u521b\u5efaPDF\u4e2d\u7684\u8868\u683c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from reportlab.lib import colors<\/p>\n<p>from reportlab.lib.pagesizes import letter<\/p>\n<p>from reportlab.platypus import SimpleDocTemplate, Table, TableStyle<\/p>\n<p>document = SimpleDocTemplate(&quot;report_table.pdf&quot;, pagesize=letter)<\/p>\n<p>data = [[&#39;Name&#39;, &#39;Age&#39;, &#39;Salary&#39;], [&#39;Alice&#39;, 25, 50000], [&#39;Bob&#39;, 30, 60000]]<\/p>\n<p>table = Table(data)<\/p>\n<p>style = TableStyle([(&#39;BACKGROUND&#39;, (0, 0), (-1, 0), colors.grey),<\/p>\n<p>                    (&#39;TEXTCOLOR&#39;, (0, 0), (-1, 0), colors.whitesmoke),<\/p>\n<p>                    (&#39;ALIGN&#39;, (0, 0), (-1, -1), &#39;CENTER&#39;),<\/p>\n<p>                    (&#39;FONTNAME&#39;, (0, 0), (-1, 0), &#39;Helvetica-Bold&#39;),<\/p>\n<p>                    (&#39;BOTTOMPADDING&#39;, (0, 0), (-1, 0), 12),<\/p>\n<p>                    (&#39;BACKGROUND&#39;, (0, 1), (-1, -1), colors.beige),<\/p>\n<p>                    (&#39;GRID&#39;, (0, 0), (-1, -1), 1, colors.black)])<\/p>\n<p>table.setStyle(style)<\/p>\n<p>elements = [table]<\/p>\n<p>document.build(elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u751f\u6210Excel\u62a5\u8868<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u751f\u6210Excel\u62a5\u8868\u5e38\u7528\u7684\u5e93\u6709openpyxl\u548cxlsxwriter\u3002\u5b83\u4eec\u90fd\u53ef\u4ee5\u521b\u5efa\u3001\u4fee\u6539Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528openpyxl<\/h4>\n<\/p>\n<p><p>openpyxl\u53ef\u4ee5\u8bfb\u5199Excel 2010 xlsx\/xlsm\/xltx\/xltm\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5openpyxl<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaExcel\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<p>wb = Workbook()<\/p>\n<p>ws = wb.active<\/p>\n<p>ws.title = &quot;Sales Report&quot;<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>ws.append([&quot;Name&quot;, &quot;Age&quot;, &quot;Salary&quot;])<\/p>\n<p>ws.append([&quot;Alice&quot;, 25, 50000])<\/p>\n<p>ws.append([&quot;Bob&quot;, 30, 60000])<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&quot;report.xlsx&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>4.2 \u4f7f\u7528xlsxwriter<\/h4>\n<\/p>\n<p><p>xlsxwriter\u9002\u5408\u521b\u5efa\u590d\u6742\u683c\u5f0f\u7684Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5xlsxwriter<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install XlsxWriter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaExcel\u6587\u4ef6<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlsxwriter<\/p>\n<p>workbook = xlsxwriter.Workbook(&#39;report.xlsx&#39;)<\/p>\n<p>worksheet = workbook.add_worksheet()<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>worksheet.write(&#39;A1&#39;, &#39;Name&#39;)<\/p>\n<p>worksheet.write(&#39;B1&#39;, &#39;Age&#39;)<\/p>\n<p>worksheet.write(&#39;C1&#39;, &#39;Salary&#39;)<\/p>\n<p>worksheet.write(&#39;A2&#39;, &#39;Alice&#39;)<\/p>\n<p>worksheet.write(&#39;B2&#39;, 25)<\/p>\n<p>worksheet.write(&#39;C2&#39;, 50000)<\/p>\n<p>worksheet.write(&#39;A3&#39;, &#39;Bob&#39;)<\/p>\n<p>worksheet.write(&#39;B3&#39;, 30)<\/p>\n<p>worksheet.write(&#39;C3&#39;, 60000)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>workbook.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>4.3 \u683c\u5f0f\u5316Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u5355\u5143\u683c\u683c\u5f0f\u53ef\u4ee5\u8ba9Excel\u6587\u4ef6\u66f4\u7f8e\u89c2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u683c\u5f0f<\/p>\n<p>bold = workbook.add_format({&#39;bold&#39;: True})<\/p>\n<p>money = workbook.add_format({&#39;num_format&#39;: &#39;$#,##0&#39;})<\/p>\n<h2><strong>\u5e94\u7528\u683c\u5f0f<\/strong><\/h2>\n<p>worksheet.write(&#39;A1&#39;, &#39;Name&#39;, bold)<\/p>\n<p>worksheet.write(&#39;B1&#39;, &#39;Age&#39;, bold)<\/p>\n<p>worksheet.write(&#39;C1&#39;, &#39;Salary&#39;, bold)<\/p>\n<p>worksheet.write(&#39;C2&#39;, 50000, money)<\/p>\n<p>worksheet.write(&#39;C3&#39;, 60000, money)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7Python\u5236\u4f5c\u62a5\u8868\u7684\u8fc7\u7a0b\u6d89\u53ca\u591a\u4e2a\u6b65\u9aa4\u548c\u5de5\u5177\u3002<strong>\u9996\u5148\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u5206\u6790\uff0c\u7136\u540e\u901a\u8fc7Matplotlib\u6216Seaborn\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\uff0c\u6700\u540e\u751f\u6210PDF\u6216Excel\u683c\u5f0f\u7684\u62a5\u8868<\/strong>\u3002\u8fd9\u4e9b\u5de5\u5177\u548c\u5e93\u7684\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4ece\u6570\u636e\u83b7\u53d6\u3001\u5904\u7406\u3001\u5206\u6790\u5230\u6700\u7ec8\u62a5\u8868\u751f\u6210\u7684\u5b8c\u6574\u6d41\u7a0b\u3002Python\u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u7684\u751f\u6001\u7cfb\u7edf\u4f7f\u5176\u6210\u4e3a\u62a5\u8868\u5236\u4f5c\u7684\u7406\u60f3\u9009\u62e9\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u8868\u683c\u8fd8\u662f\u590d\u6742\u7684\u53ef\u89c6\u5316\u56fe\u8868\uff0cPython\u90fd\u80fd\u4ee5\u9ad8\u6548\u4e14\u4e13\u4e1a\u7684\u65b9\u5f0f\u5b8c\u6210\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684Python\u5e93\u6765\u5236\u4f5c\u62a5\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u5e93\u53ef\u4ee5\u7528\u6765\u5236\u4f5c\u62a5\u8868\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecPandas\u3001Matplotlib\u3001Seaborn\u548cReportLab\u7b49\u3002Pandas\u975e\u5e38\u9002\u5408\u6570\u636e\u5904\u7406\u548c\u8868\u683c\u64cd\u4f5c\uff0c\u800cMatplotlib\u548cSeaborn\u5219\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5236\u4f5c\u56fe\u5f62\u5316\u7684\u62a5\u8868\u3002ReportLab\u5219\u53ef\u4ee5\u7528\u4e8e\u751f\u6210PDF\u683c\u5f0f\u7684\u62a5\u8868\u3002\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\uff08\u4f8b\u5982\u6570\u636e\u5206\u6790\u3001\u53ef\u89c6\u5316\u6216\u6253\u5370\uff09\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u5c06\u5927\u5927\u63d0\u9ad8\u62a5\u8868\u5236\u4f5c\u7684\u6548\u7387\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5236\u4f5c\u62a5\u8868\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6570\u636e\u7684\u6e05\u6d17\u4e0e\u8f6c\u6362\uff1f<\/strong><br \/>\u6570\u636e\u6e05\u6d17\u662f\u5236\u4f5c\u9ad8\u8d28\u91cf\u62a5\u8868\u7684\u91cd\u8981\u4e00\u6b65\u3002\u4f7f\u7528Pandas\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u7f3a\u5931\u503c\u3001\u91cd\u590d\u6570\u636e\u548c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7b49\u95ee\u9898\u3002\u901a\u8fc7\u65b9\u6cd5\u5982<code>dropna()<\/code>\u3001<code>fillna()<\/code>\u3001<code>astype()<\/code>\u7b49\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\uff0c\u4e3a\u540e\u7eed\u7684\u62a5\u8868\u751f\u6210\u6253\u4e0b\u575a\u5b9e\u7684\u57fa\u7840\u3002\u638c\u63e1\u6570\u636e\u6e05\u6d17\u6280\u5de7\uff0c\u5c06\u4f7f\u5f97\u62a5\u8868\u66f4\u52a0\u53ef\u9760\u548c\u4e13\u4e1a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u751f\u6210\u7684Python\u62a5\u8868\u5bfc\u51fa\u4e3a\u4e0d\u540c\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u751f\u6210\u7684\u62a5\u8868\u53ef\u4ee5\u5bfc\u51fa\u4e3a\u591a\u79cd\u683c\u5f0f\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ecCSV\u3001Excel\u3001PDF\u548cHTML\u3002\u4f7f\u7528Pandas\u7684<code>to_csv()<\/code>\u548c<code>to_excel()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5c06\u6570\u636e\u6846\u5bfc\u51fa\u4e3aCSV\u548cExcel\u683c\u5f0f\u3002\u5bf9\u4e8ePDF\u683c\u5f0f\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528ReportLab\u6216Matplotlib\u7684<code>savefig()<\/code>\u65b9\u6cd5\u3002\u82e5\u5e0c\u671b\u751f\u6210HTML\u683c\u5f0f\u7684\u62a5\u8868\uff0cPandas\u540c\u6837\u63d0\u4f9b\u4e86<code>to_html()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e9b\u5bfc\u51fa\u529f\u80fd\u4f7f\u5f97\u62a5\u8868\u5728\u4e0d\u540c\u573a\u5408\u4e0b\u7684\u5e94\u7528\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5236\u4f5c\u62a5\u8868\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5de5\u5177\u548c\u5e93\u6765\u5b9e\u73b0\u3002\u5229\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u3001\u901a\u8fc7Matplotlib\u6216S [&hellip;]","protected":false},"author":3,"featured_media":1021956,"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\/1021950"}],"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=1021950"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1021950\/revisions"}],"predecessor-version":[{"id":1021957,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1021950\/revisions\/1021957"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1021956"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1021950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1021950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1021950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}