{"id":1154005,"date":"2025-01-13T17:45:56","date_gmt":"2025-01-13T09:45:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1154005.html"},"modified":"2025-01-13T17:45:59","modified_gmt":"2025-01-13T09:45:59","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e5%af%bc%e5%87%baexcel","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1154005.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165\u5bfc\u51faexcel"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25183710\/f9805fab-d644-451a-adca-8bf64e9783ac.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165\u5bfc\u51faexcel\" \/><\/p>\n<p><p> <strong>Python\u5bfc\u5165\u5bfc\u51faExcel\u53ef\u4ee5\u4f7f\u7528pandas\u3001openpyxl\u3001xlrd\u7b49\u5e93\u3002\u63a8\u8350\u4f7f\u7528pandas\u5e93\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\u3001\u4f7f\u7528\u7b80\u5355\u3001\u652f\u6301\u8bfb\u5199\u591a\u79cd\u683c\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0cpandas\u5e93\u4ee5\u5176\u9ad8\u6548\u3001\u7b80\u6d01\u7684\u7279\u70b9\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u5c55\u5f00\u5982\u4f55\u4f7f\u7528pandas\u5e93\u8fdb\u884cExcel\u6587\u4ef6\u7684\u5bfc\u5165\u4e0e\u5bfc\u51fa\u3002<\/p>\n<\/p>\n<p><p><strong>\u5bfc\u5165Excel\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5bfc\u5165Excel\u6587\u4ef6\u4e3b\u8981\u4f7f\u7528pandas\u5e93\u7684<code>read_excel<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aDataFrame\u683c\u5f0f\u3002\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u6307\u5b9a\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;your_file.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u53c2\u6570\u5bf9\u8bfb\u53d6\u6570\u636e\u8fdb\u884c\u7ec6\u5316\uff0c\u6bd4\u5982\u6307\u5b9a\u8bfb\u53d6\u7684\u5de5\u4f5c\u8868\u3001\u6307\u5b9a\u5217\u540d\u3001\u5904\u7406\u7f3a\u5931\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6307\u5b9a\u5de5\u4f5c\u8868<\/p>\n<p>df = pd.read_excel(&#39;your_file.xlsx&#39;, sheet_name=&#39;Sheet1&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6307\u5b9a\u5217<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;your_file.xlsx&#39;, usecols=[&#39;Column1&#39;, &#39;Column2&#39;])<\/p>\n<h2><strong>\u5904\u7406\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;your_file.xlsx&#39;, na_values=[&#39;NA&#39;, &#39;NaN&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5bfc\u51faExcel\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5bfc\u51faExcel\u6587\u4ef6\u4f7f\u7528pandas\u5e93\u7684<code>to_excel<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u5c06DataFrame\u683c\u5f0f\u7684\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\u3002\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bfc\u51faDataFrame\u5230Excel\u6587\u4ef6<\/p>\n<p>df.to_excel(&#39;output_file.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u53c2\u6570\u5bf9\u5bfc\u51fa\u7684\u6570\u636e\u8fdb\u884c\u7ec6\u5316\uff0c\u6bd4\u5982\u6307\u5b9a\u5199\u5165\u7684\u5de5\u4f5c\u8868\u3001\u6307\u5b9a\u5217\u540d\u3001\u5904\u7406\u7f3a\u5931\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bfc\u51fa\u5230\u6307\u5b9a\u5de5\u4f5c\u8868<\/p>\n<p>df.to_excel(&#39;output_file.xlsx&#39;, sheet_name=&#39;Sheet1&#39;, index=False)<\/p>\n<h2><strong>\u5bfc\u51fa\u6307\u5b9a\u5217<\/strong><\/h2>\n<p>df.to_excel(&#39;output_file.xlsx&#39;, columns=[&#39;Column1&#39;, &#39;Column2&#39;], index=False)<\/p>\n<h2><strong>\u5904\u7406\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df.to_excel(&#39;output_file.xlsx&#39;, na_rep=&#39;NA&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e00\u3001PANDAS\u5e93\u7b80\u4ecb<\/strong><\/p>\n<\/p>\n<p><p>pandas\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u9886\u57df\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u3001\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u64cd\u4f5c\u5de5\u5177\u3002pandas\u5e93\u4e3b\u8981\u5305\u542b\u4e24\u4e2a\u6838\u5fc3\u6570\u636e\u7ed3\u6784\uff1aSeries\uff08\u5e8f\u5217\uff09\u548cDataFrame\uff08\u6570\u636e\u6846\uff09\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>Series<\/strong>\uff1a\u7c7b\u4f3c\u4e8e\u4e00\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u5b58\u50a8\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li><strong>DataFrame<\/strong>\uff1a\u7c7b\u4f3c\u4e8e\u4e8c\u7ef4\u6570\u7ec4\u6216\u8868\u683c\uff0c\u53ef\u4ee5\u5b58\u50a8\u591a\u79cd\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><p>pandas\u5e93\u7684\u529f\u80fd\u975e\u5e38\u5f3a\u5927\uff0c\u652f\u6301\u6570\u636e\u7684\u6e05\u6d17\u3001\u5904\u7406\u3001\u5206\u6790\u3001\u53ef\u89c6\u5316\u7b49\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662fpandas\u5e93\u7684\u4e00\u4e9b\u5e38\u7528\u529f\u80fd\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u8bfb\u53d6\u4e0e\u5b58\u50a8<\/strong>\uff1a\u652f\u6301\u8bfb\u53d6\u548c\u5b58\u50a8\u591a\u79cd\u683c\u5f0f\u7684\u6570\u636e\uff0c\u5982CSV\u3001Excel\u3001SQL\u7b49\u3002<\/li>\n<li><strong>\u6570\u636e\u6e05\u6d17<\/strong>\uff1a\u652f\u6301\u6570\u636e\u7f3a\u5931\u503c\u5904\u7406\u3001\u91cd\u590d\u503c\u5904\u7406\u3001\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u7b49\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u6570\u636e\u5904\u7406<\/strong>\uff1a\u652f\u6301\u6570\u636e\u7b5b\u9009\u3001\u6392\u5e8f\u3001\u5206\u7ec4\u3001\u805a\u5408\u7b49\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u6570\u636e\u5206\u6790<\/strong>\uff1a\u652f\u6301\u7edf\u8ba1\u5206\u6790\u3001\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u3001\u673a\u5668\u5b66\u4e60\u7b49\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u6570\u636e\u53ef\u89c6\u5316<\/strong>\uff1a\u652f\u6301\u4e0eMatplotlib\u3001Seaborn\u7b49\u53ef\u89c6\u5316\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002<\/li>\n<\/ul>\n<p><p><strong>\u4e8c\u3001PANDAS\u5e93\u7684\u5b89\u88c5\u4e0e\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>pandas\u5e93\u53ef\u4ee5\u901a\u8fc7pip\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165pandas\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><strong>\u4e09\u3001PANDAS\u5e93\u7684\u57fa\u672c\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9bpandas\u5e93\u7684\u57fa\u672c\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efaSeries\u548cDataFrame<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaSeries<\/strong><\/h2>\n<p>s = pd.Series([1, 2, 3, 4, 5])<\/p>\n<p>print(s)<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Tom&#39;, &#39;Jerry&#39;, &#39;Mike&#39;], &#39;Age&#39;: [20, 21, 19]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6570\u636e\u8bfb\u53d6\u4e0e\u5b58\u50a8<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6CSV\u6587\u4ef6<\/p>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<h2><strong>\u5b58\u50a8\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<h2><strong>\u5b58\u50a8\u4e3aExcel\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6570\u636e\u6e05\u6d17<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u5904\u7406\u7f3a\u5931\u503c<\/p>\n<p>df.fillna(0, inplace=True)  # \u75280\u586b\u5145\u7f3a\u5931\u503c<\/p>\n<p>df.dropna(inplace=True)     # \u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c<\/p>\n<h2><strong>\u5904\u7406\u91cd\u590d\u503c<\/strong><\/h2>\n<p>df.drop_duplicates(inplace=True)  # \u5220\u9664\u91cd\u590d\u503c<\/p>\n<h2><strong>\u6570\u636e\u7c7b\u578b\u8f6c\u6362<\/strong><\/h2>\n<p>df[&#39;Age&#39;] = df[&#39;Age&#39;].astype(int)  # \u5c06Age\u5217\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u6570\u636e\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6570\u636e\u7b5b\u9009<\/p>\n<p>filtered_df = df[df[&#39;Age&#39;] &gt; 20]  # \u7b5b\u9009Age\u5927\u4e8e20\u7684\u884c<\/p>\n<h2><strong>\u6570\u636e\u6392\u5e8f<\/strong><\/h2>\n<p>sorted_df = df.sort_values(by=&#39;Age&#39;, ascending=False)  # \u6309Age\u964d\u5e8f\u6392\u5e8f<\/p>\n<h2><strong>\u6570\u636e\u5206\u7ec4\u4e0e\u805a\u5408<\/strong><\/h2>\n<p>grouped_df = df.groupby(&#39;Name&#39;).mean()  # \u6309Name\u5206\u7ec4\u5e76\u8ba1\u7b97\u5747\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li><strong>\u6570\u636e\u5206\u6790<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u7edf\u8ba1\u5206\u6790<\/p>\n<p>mean_age = df[&#39;Age&#39;].mean()  # \u8ba1\u7b97Age\u5217\u7684\u5747\u503c<\/p>\n<p>max_age = df[&#39;Age&#39;].max()    # \u8ba1\u7b97Age\u5217\u7684\u6700\u5927\u503c<\/p>\n<h2><strong>\u65f6\u95f4\u5e8f\u5217\u5206\u6790<\/strong><\/h2>\n<p>df[&#39;Date&#39;] = pd.to_datetime(df[&#39;Date&#39;])<\/p>\n<p>df.set_index(&#39;Date&#39;, inplace=True)<\/p>\n<p>monthly_df = df.resample(&#39;M&#39;).mean()  # \u6309\u6708\u91cd\u65b0\u91c7\u6837\u5e76\u8ba1\u7b97\u5747\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"6\">\n<li><strong>\u6570\u636e\u53ef\u89c6\u5316<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>df[&#39;Age&#39;].plot()<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236\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<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u56db\u3001\u4f7f\u7528PANDAS\u5e93\u5bfc\u5165Excel\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5bfc\u5165Excel\u6587\u4ef6\u662fpandas\u5e93\u7684\u4e00\u4e2a\u5e38\u89c1\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5bfc\u5165Excel\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u6307\u5b9a\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6307\u5b9a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;data.xlsx&#39;, sheet_name=&#39;Sheet1&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6307\u5b9a\u5217<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;data.xlsx&#39;, usecols=[&#39;Name&#39;, &#39;Age&#39;])<\/p>\n<h2><strong>\u5904\u7406\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;data.xlsx&#39;, na_values=[&#39;NA&#39;, &#39;NaN&#39;])<\/p>\n<h2><strong>\u67e5\u770b\u6570\u636e<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e94\u3001\u4f7f\u7528PANDAS\u5e93\u5bfc\u51faExcel\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5bfc\u51faExcel\u6587\u4ef6\u662fpandas\u5e93\u7684\u53e6\u4e00\u4e2a\u5e38\u89c1\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5bfc\u51faExcel\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Tom&#39;, &#39;Jerry&#39;, &#39;Mike&#39;], &#39;Age&#39;: [20, 21, 19]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5bfc\u51faDataFrame\u5230Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<h2><strong>\u5bfc\u51fa\u5230\u6307\u5b9a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, sheet_name=&#39;Sheet1&#39;, index=False)<\/p>\n<h2><strong>\u5bfc\u51fa\u6307\u5b9a\u5217<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, columns=[&#39;Name&#39;], index=False)<\/p>\n<h2><strong>\u5904\u7406\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, na_rep=&#39;NA&#39;, index=False)<\/p>\n<h2><strong>\u67e5\u770b\u5bfc\u51fa\u7684\u6587\u4ef6<\/strong><\/h2>\n<p>print(&#39;Data exported successfully!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u516d\u3001PANDAS\u5e93\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u5bfc\u5165\u5bfc\u51fa\u64cd\u4f5c\uff0cpandas\u5e93\u8fd8\u652f\u6301\u8bb8\u591a\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u5982\u591a\u5de5\u4f5c\u8868\u5904\u7406\u3001\u683c\u5f0f\u8bbe\u7f6e\u3001\u516c\u5f0f\u5199\u5165\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u591a\u5de5\u4f5c\u8868\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u591a\u4e2a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>df1 = pd.read_excel(&#39;data.xlsx&#39;, sheet_name=&#39;Sheet1&#39;)<\/p>\n<p>df2 = pd.read_excel(&#39;data.xlsx&#39;, sheet_name=&#39;Sheet2&#39;)<\/p>\n<h2><strong>\u5bfc\u51fa\u5230\u591a\u4e2a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>with pd.ExcelWriter(&#39;output.xlsx&#39;) as writer:<\/p>\n<p>    df1.to_excel(writer, sheet_name=&#39;Sheet1&#39;, index=False)<\/p>\n<p>    df2.to_excel(writer, sheet_name=&#39;Sheet2&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u683c\u5f0f\u8bbe\u7f6e<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from openpyxl import load_workbook<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Tom&#39;, &#39;Jerry&#39;, &#39;Mike&#39;], &#39;Age&#39;: [20, 21, 19]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5bfc\u51faDataFrame\u5230Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<h2><strong>\u52a0\u8f7dExcel\u6587\u4ef6<\/strong><\/h2>\n<p>book = load_workbook(&#39;output.xlsx&#39;)<\/p>\n<p>sheet = book.active<\/p>\n<h2><strong>\u8bbe\u7f6e\u5217\u5bbd<\/strong><\/h2>\n<p>sheet.column_dimensions[&#39;A&#39;].width = 20<\/p>\n<p>sheet.column_dimensions[&#39;B&#39;].width = 10<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53<\/strong><\/h2>\n<p>from openpyxl.styles import Font<\/p>\n<p>font = Font(name=&#39;Arial&#39;, size=12, bold=True)<\/p>\n<p>for cell in sheet[&#39;A&#39;] + sheet[1]:<\/p>\n<p>    cell.font = font<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>book.save(&#39;output.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u516c\u5f0f\u5199\u5165<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Tom&#39;, &#39;Jerry&#39;, &#39;Mike&#39;], &#39;Age&#39;: [20, 21, 19]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5bfc\u51faDataFrame\u5230Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<h2><strong>\u52a0\u8f7dExcel\u6587\u4ef6<\/strong><\/h2>\n<p>from openpyxl import load_workbook<\/p>\n<p>book = load_workbook(&#39;output.xlsx&#39;)<\/p>\n<p>sheet = book.active<\/p>\n<h2><strong>\u5199\u5165\u516c\u5f0f<\/strong><\/h2>\n<p>sheet[&#39;C1&#39;] = &#39;Sum of Ages&#39;<\/p>\n<p>sheet[&#39;C2&#39;] = &#39;=SUM(B2:B4)&#39;<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>book.save(&#39;output.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e03\u3001PANDAS\u5e93\u4e0e\u5176\u4ed6\u5e93\u7684\u7ed3\u5408\u4f7f\u7528<\/strong><\/p>\n<\/p>\n<p><p>pandas\u5e93\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u5982NumPy\u3001Matplotlib\u3001Seaborn\u7b49\uff0c\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e0eNumPy\u7ed3\u5408\u4f7f\u7528<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Tom&#39;, &#39;Jerry&#39;, &#39;Mike&#39;], &#39;Age&#39;: [20, 21, 19]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u8fdb\u884c\u6570\u636e\u5904\u7406<\/strong><\/h2>\n<p>df[&#39;Age_Square&#39;] = np.square(df[&#39;Age&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4e0eMatplotlib\u7ed3\u5408\u4f7f\u7528<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Tom&#39;, &#39;Jerry&#39;, &#39;Mike&#39;], &#39;Age&#39;: [20, 21, 19]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316<\/strong><\/h2>\n<p>df[&#39;Age&#39;].plot(kind=&#39;bar&#39;)<\/p>\n<p>plt.title(&#39;Age of Individuals&#39;)<\/p>\n<p>plt.xlabel(&#39;Name&#39;)<\/p>\n<p>plt.ylabel(&#39;Age&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4e0eSeaborn\u7ed3\u5408\u4f7f\u7528<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Tom&#39;, &#39;Jerry&#39;, &#39;Mike&#39;], &#39;Age&#39;: [20, 21, 19]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528Seaborn\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316<\/strong><\/h2>\n<p>sns.barplot(x=&#39;Name&#39;, y=&#39;Age&#39;, data=df)<\/p>\n<p>plt.title(&#39;Age of Individuals&#39;)<\/p>\n<p>plt.xlabel(&#39;Name&#39;)<\/p>\n<p>plt.ylabel(&#39;Age&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u8f7b\u677e\u5730\u5bfc\u5165\u548c\u5bfc\u51faExcel\u6587\u4ef6\uff0c\u4ee5\u53ca\u8fdb\u884c\u5404\u79cd\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u64cd\u4f5c\u3002pandas\u5e93\u7684\u5f3a\u5927\u529f\u80fd\u548c\u7b80\u6d01\u6613\u7528\u7684\u63a5\u53e3\uff0c\u4f7f\u5f97\u5b83\u6210\u4e3a\u6570\u636e\u79d1\u5b66\u548c\u673a\u5668\u5b66\u4e60\u9886\u57df\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528pandas\u5e93\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\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5e93\u6765\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f<code>pandas<\/code>\u548c<code>openpyxl<\/code>\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pd.read_excel()<\/code>\u51fd\u6570\u65b9\u4fbf\u5730\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aDataFrame\uff0c\u4fbf\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u786e\u4fdd\u5728\u4f7f\u7528\u4e4b\u524d\u5b89\u88c5\u4e86\u76f8\u5e94\u7684\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4<code>pip install pandas openpyxl<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>Python\u5bfc\u51fa\u6570\u636e\u5230Excel\u65f6\uff0c\u652f\u6301\u54ea\u4e9b\u6587\u4ef6\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>pandas<\/code>\u5e93\u5bfc\u51fa\u6570\u636e\u65f6\uff0c\u901a\u5e38\u652f\u6301<code>.xlsx<\/code>\u548c<code>.xls<\/code>\u4e24\u79cd\u683c\u5f0f\u3002\u901a\u8fc7<code>DataFrame.to_excel()<\/code>\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u683c\u5f0f\uff0c\u9009\u62e9\u662f\u5426\u5305\u542b\u7d22\u5f15\u3001\u5217\u540d\u7b49\u4fe1\u606f\u3002\u5bf9\u4e8e\u8f83\u5927\u7684\u6570\u636e\u96c6\uff0c<code>.xlsx<\/code>\u683c\u5f0f\u901a\u5e38\u662f\u9996\u9009\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u66f4\u5927\u7684\u6570\u636e\u91cf\u548c\u66f4\u590d\u6742\u7684\u683c\u5f0f\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u64cd\u4f5cExcel\u6587\u4ef6\u65f6\uff0c\u662f\u5426\u9700\u8981\u5b89\u88c5Excel\u8f6f\u4ef6\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u8fdb\u884cExcel\u6587\u4ef6\u7684\u64cd\u4f5c\u65f6\uff0c\u4e0d\u9700\u8981\u5b89\u88c5Excel\u8f6f\u4ef6\u3002Python\u7684\u76f8\u5173\u5e93\uff08\u5982<code>pandas<\/code>\u548c<code>openpyxl<\/code>\uff09\u80fd\u591f\u72ec\u7acb\u5904\u7406Excel\u6587\u4ef6\u7684\u8bfb\u53d6\u4e0e\u5199\u5165\uff0c\u5b8c\u5168\u4f9d\u8d56\u4e8ePython\u73af\u5883\u3002\u56e0\u6b64\uff0c\u65e0\u8bba\u662f\u5728\u670d\u52a1\u5668\u4e0a\u8fd8\u662f\u672c\u5730\u673a\u5668\u4e0a\uff0c\u53ea\u8981\u914d\u7f6e\u597d\u76f8\u5e94\u7684\u5e93\uff0c\u5c31\u53ef\u4ee5\u8fdb\u884cExcel\u6587\u4ef6\u7684\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bfc\u5165\u5bfc\u51faExcel\u53ef\u4ee5\u4f7f\u7528pandas\u3001openpyxl\u3001xlrd\u7b49\u5e93\u3002\u63a8\u8350\u4f7f\u7528pandas\u5e93\uff0c\u56e0 [&hellip;]","protected":false},"author":3,"featured_media":1154010,"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\/1154005"}],"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=1154005"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154005\/revisions"}],"predecessor-version":[{"id":1154013,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1154005\/revisions\/1154013"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1154010"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1154005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1154005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1154005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}