{"id":13,"date":"2026-11-08T13:15:36","date_gmt":"2026-11-08T13:15:36","guid":{"rendered":"http:\/\/blog.antpython.net\/?p=13"},"modified":"2026-11-08T13:15:36","modified_gmt":"2026-11-08T13:15:36","slug":"%e7%94%a8-groupby-pivot_table-%e5%81%9a%e4%b8%9a%e5%8a%a1%e6%b1%87%e6%80%bb%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"http:\/\/blog.antpython.net\/?p=13","title":{"rendered":"\u7528 groupby \/ pivot_table \u505a\u4e1a\u52a1\u6c47\u603b\u5206\u6790"},"content":{"rendered":"<p>\u4e1a\u52a1\u540c\u5b66\u6700\u5e38\u95ee\uff1a\u300c\u6309\u57ce\u5e02\/\u54c1\u7c7b\/\u6708\u4efd\uff0c\u9500\u91cf\u591a\u5c11\u3001\u5ba2\u5355\u4ef7\u591a\u5c11\uff1f\u300dPandas \u7684 <code>groupby<\/code> \u548c <code>pivot_table<\/code> \u5c31\u662f\u4e3a\u8fd9\u7c7b\u95ee\u9898\u51c6\u5907\u7684\u3002<\/p>\n<h2>groupby \u57fa\u7840<\/h2>\n<pre><code>import pandas as pd\n\n# \u5047\u8bbe df \u6709 city, category, amount, qty\nsummary = (\n    df.groupby(\"city\", as_index=False)\n      .agg(\u8ba2\u5355\u6570=(\"order_id\", \"count\"),\n           \u9500\u552e\u989d=(\"amount\", \"sum\"),\n           \u5ba2\u5355\u4ef7=(\"amount\", \"mean\"))\n      .sort_values(\"\u9500\u552e\u989d\", ascending=False)\n)\n<\/code><\/pre>\n<h2>\u591a\u5b57\u6bb5\u5206\u7ec4<\/h2>\n<pre><code>by_city_cat = df.groupby([\"city\", \"category\"]).agg(\n    \u9500\u91cf=(\"qty\", \"sum\"),\n    \u9500\u552e\u989d=(\"amount\", \"sum\"),\n).reset_index()\n<\/code><\/pre>\n<h2>pivot_table\uff1a\u66f4\u50cf Excel \u900f\u89c6\u8868<\/h2>\n<pre><code>pivot = pd.pivot_table(\n    df,\n    index=\"city\",\n    columns=\"category\",\n    values=\"amount\",\n    aggfunc=\"sum\",\n    fill_value=0,\n    margins=True,\n    margins_name=\"\u5408\u8ba1\",\n)\n<\/code><\/pre>\n<h2>\u65f6\u95f4\u7ef4\u5ea6\u6c47\u603b<\/h2>\n<pre><code>df[\"month\"] = df[\"order_date\"].dt.to_period(\"M\").astype(str)\nmonthly = df.groupby(\"month\")[\"amount\"].sum()\n<\/code><\/pre>\n<h2>\u8f6c\u5316\u7387\u8fd9\u7c7b\u300c\u5148\u805a\u5408\u518d\u8ba1\u7b97\u300d<\/h2>\n<pre><code>funnel = df.groupby(\"channel\").agg(\n    \u8bbf\u95ee=(\"user_id\", \"nunique\"),\n    \u4e0b\u5355=(\"order_id\", \"nunique\"),\n)\nfunnel[\"\u4e0b\u5355\u7387\"] = funnel[\"\u4e0b\u5355\"] \/ funnel[\"\u8bbf\u95ee\"]\n<\/code><\/pre>\n<h2>\u9009\u578b\u5efa\u8bae<\/h2>\n<ul>\n<li>\u53ea\u8981\u957f\u8868\u6c47\u603b \u2192 <code>groupby<\/code><\/li>\n<li>\u8981\u884c\u5217\u4ea4\u53c9\u7684\u5bbd\u8868 \u2192 <code>pivot_table<\/code><\/li>\n<li>\u7ed3\u679c\u8981\u7ed9\u4e1a\u52a1\u770b \u2192 \u6700\u540e <code>round<\/code>\u3001\u6539\u4e2d\u6587\u5217\u540d\u3001\u5bfc\u51fa Excel<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e1a\u52a1\u540c\u5b66\u6700\u5e38\u95ee\uff1a\u300c\u6309\u57ce\u5e02\/\u54c1\u7c7b\/\u6708\u4efd\uff0c\u9500\u91cf\u591a\u5c11\u3001\u5ba2\u5355\u4ef7\u591a\u5c11\uff1f\u300dPandas \u7684 groupby \u548c pivot [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-pandas-analysis"],"_links":{"self":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13"}],"version-history":[{"count":0,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/13\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}