{"id":1150710,"date":"2025-01-13T17:04:43","date_gmt":"2025-01-13T09:04:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1150710.html"},"modified":"2025-01-13T17:04:47","modified_gmt":"2025-01-13T09:04:47","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e6%b1%87%e6%80%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1150710.html","title":{"rendered":"python\u5982\u4f55\u5199\u6c47\u603b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181117\/0d0214ab-a60b-47b4-b432-e255969132c7.webp\" alt=\"python\u5982\u4f55\u5199\u6c47\u603b\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7f16\u5199\u6c47\u603b\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u6570\u636e\u8bfb\u53d6\u3001\u6570\u636e\u5904\u7406\u3001\u6570\u636e\u805a\u5408\u548c\u6570\u636e\u8f93\u51fa\u3002<\/strong> \u6570\u636e\u8bfb\u53d6\u53ef\u4ee5\u901a\u8fc7pandas\u8bfb\u53d6CSV\u6587\u4ef6\u6216Excel\u6587\u4ef6\uff0c\u6570\u636e\u5904\u7406\u53ef\u4ee5\u4f7f\u7528pandas\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\u548c\u8f6c\u6362\uff0c\u6570\u636e\u805a\u5408\u53ef\u4ee5\u901a\u8fc7groupby\u548cagg\u51fd\u6570\u8fdb\u884c\uff0c\u6570\u636e\u8f93\u51fa\u53ef\u4ee5\u5c06\u7ed3\u679c\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002<strong>\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u70b9\uff1a\u6570\u636e\u805a\u5408\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u6570\u636e\u805a\u5408\u662f\u6c47\u603b\u64cd\u4f5c\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u4e2d\u7684<code>groupby<\/code>\u51fd\u6570\u5c06\u6570\u636e\u6309\u7167\u67d0\u4e2a\u6216\u591a\u4e2a\u5217\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>agg<\/code>\u51fd\u6570\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\u3002\u5e38\u89c1\u7684\u805a\u5408\u64cd\u4f5c\u5305\u62ec\u6c42\u548c\u3001\u5747\u503c\u3001\u8ba1\u6570\u3001\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c\u7b49\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5305\u542b\u9500\u552e\u6570\u636e\u7684DataFrame\uff0c\u53ef\u4ee5\u4f7f\u7528<code>groupby<\/code>\u51fd\u6570\u6309\u6708\u4efd\u5bf9\u9500\u552e\u989d\u8fdb\u884c\u6c47\u603b\uff0c\u4f7f\u7528<code>agg<\/code>\u51fd\u6570\u8ba1\u7b97\u6bcf\u4e2a\u6708\u7684\u603b\u9500\u552e\u989d\u548c\u5e73\u5747\u9500\u552e\u989d\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5feb\u901f\u5f97\u5230\u6570\u636e\u7684\u6c47\u603b\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528Python\u7f16\u5199\u6c47\u603b\u64cd\u4f5c\uff0c\u5e76\u5206\u4e3a\u51e0\u4e2a\u4e3b\u8981\u90e8\u5206\u8fdb\u884c\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u6570\u636e\u8bfb\u53d6<\/strong><\/h2>\n<p><p>\u5728\u8fdb\u884c\u6570\u636e\u6c47\u603b\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u8bfb\u53d6\u6570\u636e\u3002Python\u4e2d\u5e38\u7528\u7684\u6570\u636e\u8bfb\u53d6\u5e93\u662fpandas\u3002pandas\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6CSV\u6587\u4ef6\u3001Excel\u6587\u4ef6\u3001SQL\u6570\u636e\u5e93\u7b49\u591a\u79cd\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u8bfb\u53d6CSV\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>CSV\u6587\u4ef6\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0cpandas\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u8bfb\u53d6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u8bfb\u53d6Excel\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>Excel\u6587\u4ef6\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0cpandas\u540c\u6837\u63d0\u4f9b\u4e86\u8bfb\u53d6Excel\u6587\u4ef6\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_excel(&#39;data.xlsx&#39;)<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u8bfb\u53d6SQL\u6570\u636e\u5e93<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u6570\u636e\u5b58\u50a8\u5728SQL\u6570\u636e\u5e93\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;data.db&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6SQL\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_sql_query(&#39;SELECT * FROM sales&#39;, conn)<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u6570\u636e\u5904\u7406<\/strong><\/h2>\n<p><p>\u5728\u8bfb\u53d6\u6570\u636e\u540e\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4e00\u4e9b\u9884\u5904\u7406\u64cd\u4f5c\uff0c\u5305\u62ec\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u8f6c\u6362\u7b49\u3002pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u6570\u636e\u6e05\u6d17<\/h2>\n<\/p>\n<p><p>\u6570\u636e\u6e05\u6d17\u662f\u6307\u53bb\u9664\u6216\u586b\u8865\u6570\u636e\u4e2d\u7684\u7f3a\u5931\u503c\u3001\u91cd\u590d\u503c\u548c\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u53bb\u9664\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53bb\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c<\/p>\n<p>data_cleaned = data.dropna()<\/p>\n<p>print(data_cleaned.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u586b\u8865\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7528\u5e73\u5747\u503c\u586b\u8865\u7f3a\u5931\u503c<\/p>\n<p>data_filled = data.fillna(data.mean())<\/p>\n<p>print(data_filled.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u53bb\u9664\u91cd\u590d\u503c<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53bb\u9664\u91cd\u590d\u503c<\/p>\n<p>data_unique = data.drop_duplicates()<\/p>\n<p>print(data_unique.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u6570\u636e\u8f6c\u6362<\/h2>\n<\/p>\n<p><p>\u6570\u636e\u8f6c\u6362\u662f\u6307\u5bf9\u6570\u636e\u8fdb\u884c\u683c\u5f0f\u8f6c\u6362\u3001\u7c7b\u578b\u8f6c\u6362\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u8f6c\u6362\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u67d0\u5217\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b<\/p>\n<p>data[&#39;column_name&#39;] = data[&#39;column_name&#39;].astype(int)<\/p>\n<p>print(data.dtypes)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u521b\u5efa\u65b0\u7684\u5217<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217<\/p>\n<p>data[&#39;new_column&#39;] = data[&#39;column1&#39;] + data[&#39;column2&#39;]<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u6570\u636e\u805a\u5408<\/strong><\/h2>\n<p><p>\u6570\u636e\u805a\u5408\u662f\u6c47\u603b\u64cd\u4f5c\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u4e3b\u8981\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\u3002pandas\u7684<code>groupby<\/code>\u548c<code>agg<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u6570\u636e\u805a\u5408\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u6309\u5355\u5217\u5206\u7ec4<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>groupby<\/code>\u51fd\u6570\u6309\u5355\u5217\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>agg<\/code>\u51fd\u6570\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u636e\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u6708\u4efd\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u6708\u7684\u603b\u9500\u552e\u989d\u548c\u5e73\u5747\u9500\u552e\u989d<\/p>\n<p>monthly_summary = data.groupby(&#39;month&#39;).agg({&#39;sales&#39;: [&#39;sum&#39;, &#39;mean&#39;]})<\/p>\n<p>print(monthly_summary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u6309\u591a\u5217\u5206\u7ec4<\/h2>\n<\/p>\n<p><p>\u540c\u6837\u53ef\u4ee5\u6309\u591a\u5217\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u6708\u4efd\u548c\u4ea7\u54c1\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u6708\u6bcf\u4e2a\u4ea7\u54c1\u7684\u603b\u9500\u552e\u989d\u548c\u5e73\u5747\u9500\u552e\u989d<\/p>\n<p>monthly_product_summary = data.groupby([&#39;month&#39;, &#39;product&#39;]).agg({&#39;sales&#39;: [&#39;sum&#39;, &#39;mean&#39;]})<\/p>\n<p>print(monthly_product_summary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u805a\u5408\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u7684\u805a\u5408\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u805a\u5408\u51fd\u6570\u5e76\u5e94\u7528\u4e8e\u5206\u7ec4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u81ea\u5b9a\u4e49\u805a\u5408\u51fd\u6570\uff0c\u8ba1\u7b97\u9500\u552e\u989d\u7684\u8303\u56f4<\/p>\n<p>def range_func(x):<\/p>\n<p>    return x.max() - x.min()<\/p>\n<h2><strong>\u6309\u6708\u4efd\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u6708\u7684\u9500\u552e\u989d\u8303\u56f4<\/strong><\/h2>\n<p>monthly_range = data.groupby(&#39;month&#39;).agg({&#39;sales&#39;: range_func})<\/p>\n<p>print(monthly_range)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u6570\u636e\u8f93\u51fa<\/strong><\/h2>\n<p><p>\u5728\u5b8c\u6210\u6570\u636e\u6c47\u603b\u540e\uff0c\u901a\u5e38\u9700\u8981\u5c06\u7ed3\u679c\u4fdd\u5b58\u4e3a\u6587\u4ef6\u6216\u8f93\u51fa\u5230\u5176\u4ed6\u5b58\u50a8\u4ecb\u8d28\u3002pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u8f93\u51fa\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u8f93\u51fa\u5230CSV\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u6c47\u603b\u7ed3\u679c\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff0c\u4fbf\u4e8e\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6c47\u603b\u7ed3\u679c\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/p>\n<p>monthly_summary.to_csv(&#39;monthly_summary.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u8f93\u51fa\u5230Excel\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u540c\u6837\u53ef\u4ee5\u5c06\u6c47\u603b\u7ed3\u679c\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6c47\u603b\u7ed3\u679c\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6<\/p>\n<p>monthly_summary.to_excel(&#39;monthly_summary.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u8f93\u51fa\u5230SQL\u6570\u636e\u5e93<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5c06\u6c47\u603b\u7ed3\u679c\u4fdd\u5b58\u5230SQL\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u7684<code>to_sql<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;summary.db&#39;)<\/p>\n<h2><strong>\u5c06\u6c47\u603b\u7ed3\u679c\u4fdd\u5b58\u5230SQL\u6570\u636e\u5e93<\/strong><\/h2>\n<p>monthly_summary.to_sql(&#39;monthly_summary&#39;, conn, if_exists=&#39;replace&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u6848\u4f8b\u5206\u6790<\/strong><\/h2>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u6c47\u603b\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u6848\u4f8b\u8fdb\u884c\u8be6\u7ec6\u5206\u6790\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4efd\u5305\u542b\u9500\u552e\u6570\u636e\u7684CSV\u6587\u4ef6\uff0c\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>date,product,sales<\/p>\n<p>2023-01-01,Product A,100<\/p>\n<p>2023-01-01,Product B,150<\/p>\n<p>2023-01-02,Product A,200<\/p>\n<p>2023-01-02,Product B,250<\/p>\n<p>2023-02-01,Product A,300<\/p>\n<p>2023-02-01,Product B,350<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>1\u3001\u8bfb\u53d6\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;sales_data.csv&#39;)<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u6570\u636e\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u786e\u4fdd\u6ca1\u6709\u7f3a\u5931\u503c\u548c\u91cd\u590d\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53bb\u9664\u7f3a\u5931\u503c<\/p>\n<p>data_cleaned = data.dropna()<\/p>\n<h2><strong>\u53bb\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>data_cleaned = data_cleaned.drop_duplicates()<\/p>\n<p>print(data_cleaned.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u6570\u636e\u805a\u5408<\/h2>\n<\/p>\n<p><p>\u6309\u6708\u4efd\u548c\u4ea7\u54c1\u5bf9\u9500\u552e\u6570\u636e\u8fdb\u884c\u6c47\u603b\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u6708\u6bcf\u4e2a\u4ea7\u54c1\u7684\u603b\u9500\u552e\u989d\u548c\u5e73\u5747\u9500\u552e\u989d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u65e5\u671f\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u7c7b\u578b<\/p>\n<p>data_cleaned[&#39;date&#39;] = pd.to_datetime(data_cleaned[&#39;date&#39;])<\/p>\n<h2><strong>\u63d0\u53d6\u6708\u4efd<\/strong><\/h2>\n<p>data_cleaned[&#39;month&#39;] = data_cleaned[&#39;date&#39;].dt.to_period(&#39;M&#39;)<\/p>\n<h2><strong>\u6309\u6708\u4efd\u548c\u4ea7\u54c1\u5206\u7ec4\uff0c\u5e76\u8fdb\u884c\u805a\u5408<\/strong><\/h2>\n<p>monthly_product_summary = data_cleaned.groupby([&#39;month&#39;, &#39;product&#39;]).agg({&#39;sales&#39;: [&#39;sum&#39;, &#39;mean&#39;]})<\/p>\n<p>print(monthly_product_summary)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4\u3001\u6570\u636e\u8f93\u51fa<\/h2>\n<\/p>\n<p><p>\u5c06\u6c47\u603b\u7ed3\u679c\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6c47\u603b\u7ed3\u679c\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6<\/p>\n<p>monthly_product_summary.to_csv(&#39;monthly_product_summary.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u6210\u529f\u5730\u4f7f\u7528Python\u5bf9\u9500\u552e\u6570\u636e\u8fdb\u884c\u4e86\u6c47\u603b\uff0c\u5e76\u5c06\u6c47\u603b\u7ed3\u679c\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3002\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6848\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528pandas\u8fdb\u884c\u6570\u636e\u8bfb\u53d6\u3001\u6570\u636e\u5904\u7406\u3001\u6570\u636e\u805a\u5408\u548c\u6570\u636e\u8f93\u51fa\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e2a\u6848\u4f8b\uff0c\u4f60\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u6c47\u603b\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u8fdb\u9636\u5e94\u7528<\/strong><\/h2>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u6570\u636e\u6c47\u603b\u64cd\u4f5c\uff0cpandas\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6570\u636e\u6c47\u603b\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528Pivot Table<\/h2>\n<\/p>\n<p><p>Pivot Table\uff08\u900f\u89c6\u8868\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u6c47\u603b\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u6309\u7167\u884c\u548c\u5217\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\u3002pandas\u7684<code>pivot_table<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u900f\u89c6\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u900f\u89c6\u8868\uff0c\u6309\u6708\u4efd\u548c\u4ea7\u54c1\u8fdb\u884c\u6c47\u603b<\/p>\n<p>pivot_table = data_cleaned.pivot_table(values=&#39;sales&#39;, index=&#39;month&#39;, columns=&#39;product&#39;, aggfunc=&#39;sum&#39;)<\/p>\n<p>print(pivot_table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2\u3001\u4f7f\u7528Resample<\/h2>\n<\/p>\n<p><p>Resample\uff08\u91cd\u91c7\u6837\uff09\u662f\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u5e38\u7528\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u6309\u7167\u4e0d\u540c\u7684\u65f6\u95f4\u9891\u7387\u8fdb\u884c\u6c47\u603b\u3002pandas\u7684<code>resample<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u91cd\u91c7\u6837\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6309\u6708\u4efd\u91cd\u91c7\u6837\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u6708\u7684\u603b\u9500\u552e\u989d<\/p>\n<p>monthly_sales = data_cleaned.set_index(&#39;date&#39;).resample(&#39;M&#39;).sum()<\/p>\n<p>print(monthly_sales)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3\u3001\u4f7f\u7528Rolling<\/h2>\n<\/p>\n<p><p>Rolling\uff08\u6ed1\u52a8\u7a97\u53e3\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u5e94\u7528\u6ed1\u52a8\u7a97\u53e3\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\u3002pandas\u7684<code>rolling<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6ed1\u52a8\u7a97\u53e3\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u6bcf\u4e2a\u65e5\u671f\u524d7\u5929\u7684\u6ed1\u52a8\u5e73\u5747\u9500\u552e\u989d<\/p>\n<p>data_cleaned[&#39;rolling_mean&#39;] = data_cleaned[&#39;sales&#39;].rolling(window=7).mean()<\/p>\n<p>print(data_cleaned.head(10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4\u3001\u4f7f\u7528Crosstab<\/h2>\n<\/p>\n<p><p>Crosstab\uff08\u4ea4\u53c9\u8868\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u8ba1\u7b97\u5206\u7ec4\u9891\u7387\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7edf\u8ba1\u4e0d\u540c\u7c7b\u522b\u7ec4\u5408\u7684\u9891\u6b21\u3002pandas\u7684<code>crosstab<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u4ea4\u53c9\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u6bcf\u4e2a\u6708\u6bcf\u4e2a\u4ea7\u54c1\u7684\u9500\u552e\u6b21\u6570<\/p>\n<p>crosstab = pd.crosstab(data_cleaned[&#39;month&#39;], data_cleaned[&#39;product&#39;])<\/p>\n<p>print(crosstab)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8fdb\u9636\u5e94\u7528\uff0c\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u6c47\u603b\u548c\u5206\u6790\uff0c\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u6c47\u603b\uff0c\u4e3b\u8981\u5305\u62ec\u6570\u636e\u8bfb\u53d6\u3001\u6570\u636e\u5904\u7406\u3001\u6570\u636e\u805a\u5408\u548c\u6570\u636e\u8f93\u51fa\u56db\u4e2a\u90e8\u5206\u3002\u901a\u8fc7\u4f7f\u7528pandas\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u5404\u79cd\u6570\u636e\u6c47\u603b\u64cd\u4f5c\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u4e3a\u6587\u4ef6\u6216\u8f93\u51fa\u5230\u5176\u4ed6\u5b58\u50a8\u4ecb\u8d28\u3002\u6b64\u5916\uff0c\u8fd8\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u8fdb\u9636\u5e94\u7528\uff0c\u5982\u900f\u89c6\u8868\u3001\u91cd\u91c7\u6837\u3001\u6ed1\u52a8\u7a97\u53e3\u548c\u4ea4\u53c9\u8868\u7b49\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6570\u636e\u6c47\u603b\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u4e2d\u7684\u6570\u636e\u6c47\u603b\u64cd\u4f5c\uff0c\u5e76\u5e94\u7528\u4e8e\u5b9e\u9645\u7684\u6570\u636e\u5206\u6790\u5de5\u4f5c\u4e2d\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6570\u636e\u6c47\u603b\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u5206\u6790\uff0cpandas\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7075\u6d3b\u7684\u64cd\u4f5c\u65b9\u5f0f\uff0c\u4f7f\u6570\u636e\u5904\u7406\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u6c47\u603b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6570\u636e\u6c47\u603b\u901a\u5e38\u4f7f\u7528Pandas\u5e93\u6765\u5904\u7406\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>groupby<\/code>\u51fd\u6570\u5c06\u6570\u636e\u6839\u636e\u67d0\u4e9b\u5217\u8fdb\u884c\u5206\u7ec4\uff0c\u7136\u540e\u4f7f\u7528\u805a\u5408\u51fd\u6570\uff08\u5982<code>sum<\/code>\u3001<code>mean<\/code>\u3001<code>count<\/code>\u7b49\uff09\u6765\u6c47\u603b\u6570\u636e\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;\u7c7b\u522b&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;A&#39;, &#39;B&#39;],\n        &#39;\u503c&#39;: [10, 20, 30, 40]}\ndf = pd.DataFrame(data)\n\n\u6c47\u603b\u7ed3\u679c = df.groupby(&#39;\u7c7b\u522b&#39;).sum()\nprint(\u6c47\u603b\u7ed3\u679c)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6839\u636e\u201c\u7c7b\u522b\u201d\u5217\u5bf9\u201c\u503c\u201d\u8fdb\u884c\u6c47\u603b\uff0c\u8f93\u51fa\u6bcf\u4e2a\u7c7b\u522b\u7684\u603b\u548c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u7f3a\u5931\u503c\u4ee5\u8fdb\u884c\u6709\u6548\u6c47\u603b\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6570\u636e\u6c47\u603b\u524d\uff0c\u5904\u7406\u7f3a\u5931\u503c\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002Pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u7f3a\u5931\u503c\uff0c\u4f8b\u5982<code>dropna()<\/code>\u548c<code>fillna()<\/code>\u3002\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u60a8\u7684\u6c47\u603b\u7ed3\u679c\u66f4\u51c6\u786e\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728\u6c47\u603b\u524d\u4f7f\u7528<code>fillna(0)<\/code>\u6765\u5c06\u7f3a\u5931\u503c\u66ff\u6362\u4e3a0\uff0c\u786e\u4fdd\u5728\u8ba1\u7b97\u603b\u548c\u65f6\u4e0d\u9057\u6f0f\u4efb\u4f55\u6570\u636e\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u6c47\u603b\u65f6\uff0c\u5982\u4f55\u63d0\u9ad8\u6027\u80fd\uff1f<\/strong><br \/>\u5f53\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u6210\u4e3a\u4e00\u4e2a\u95ee\u9898\u3002\u4f7f\u7528Pandas\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f18\u5316\u6570\u636e\u7c7b\u578b\uff08\u5982\u5c06<code>float64<\/code>\u8f6c\u6362\u4e3a<code>float32<\/code>\uff09\u6765\u8282\u7701\u5185\u5b58\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>numba<\/code>\u5e93\u53ef\u4ee5\u52a0\u901f\u67d0\u4e9b\u6570\u503c\u8ba1\u7b97\uff0c\u6216\u8005\u4f7f\u7528<code>Dask<\/code>\u5e93\u6765\u5904\u7406\u5206\u5e03\u5f0f\u6570\u636e\uff0c\u4ece\u800c\u63d0\u9ad8\u6570\u636e\u6c47\u603b\u7684\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7f16\u5199\u6c47\u603b\u7684\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u6570\u636e\u8bfb\u53d6\u3001\u6570\u636e\u5904\u7406\u3001\u6570\u636e\u805a\u5408\u548c\u6570\u636e\u8f93\u51fa\u3002 \u6570\u636e\u8bfb\u53d6\u53ef\u4ee5\u901a\u8fc7pandas\u8bfb [&hellip;]","protected":false},"author":3,"featured_media":1150716,"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\/1150710"}],"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=1150710"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150710\/revisions"}],"predecessor-version":[{"id":1150719,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1150710\/revisions\/1150719"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1150716"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1150710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1150710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1150710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}