{"id":1159456,"date":"2025-01-13T18:53:06","date_gmt":"2025-01-13T10:53:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159456.html"},"modified":"2025-01-13T18:53:09","modified_gmt":"2025-01-13T10:53:09","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e6%95%b0%e6%8d%ae%e5%88%86%e7%bb%84%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1159456.html","title":{"rendered":"python\u5982\u4f55\u628a\u6570\u636e\u5206\u7ec4\u8f93\u51fa"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201153\/007f5af3-97c4-41d7-a889-b6f20f097370.webp\" alt=\"python\u5982\u4f55\u628a\u6570\u636e\u5206\u7ec4\u8f93\u51fa\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u5206\u7ec4\u8f93\u51fa\uff0c<strong>\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4f7f\u7528itertools.groupby\u51fd\u6570\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u5206\u7ec4\u662f\u6bd4\u8f83\u76f4\u89c2\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002<strong>\u5b57\u5178\u5141\u8bb8\u6211\u4eec\u5c06\u6570\u636e\u6309\u67d0\u4e2a\u952e\u503c\u8fdb\u884c\u5206\u7ec4<\/strong>\uff0c\u4ece\u800c\u5b9e\u73b0\u6570\u636e\u7684\u5206\u7c7b\u8f93\u51fa\u3002\u4e0b\u9762\u8be6\u7ec6\u5c55\u5f00\u89e3\u91ca\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u6570\u636e\u5206\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u7ec4\u6570\u636e\uff0c\u8868\u793a\u4e0d\u540c\u5b66\u751f\u7684\u6210\u7ee9\uff0c\u5305\u62ec\u59d3\u540d\u3001\u79d1\u76ee\u548c\u5206\u6570\u3002\u6211\u4eec\u5e0c\u671b\u6309\u7167\u79d1\u76ee\u5bf9\u8fd9\u4e9b\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e<\/p>\n<p>data = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 85},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 90},<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 92},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 88},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 95},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 85}<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u5206\u7ec4<\/strong><\/h2>\n<p>grouped_data = {}<\/p>\n<p>for entry in data:<\/p>\n<p>    subject = entry[&#39;subject&#39;]<\/p>\n<p>    if subject not in grouped_data:<\/p>\n<p>        grouped_data[subject] = []<\/p>\n<p>    grouped_data[subject].append(entry)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>for subject, entries in grouped_data.items():<\/p>\n<p>    print(f&quot;Subject: {subject}&quot;)<\/p>\n<p>    for entry in entries:<\/p>\n<p>        print(entry)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u901a\u8fc7\u904d\u5386\u6570\u636e\u5217\u8868\uff0c\u5c06\u6bcf\u4e00\u6761\u6570\u636e\u6309\u79d1\u76ee\u5206\u7ec4\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u6700\u7ec8\u6253\u5370\u51fa\u5206\u7ec4\u540e\u7684\u6570\u636e\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5c0f\u6216\u7ed3\u6784\u8f83\u4e3a\u7b80\u5355\u7684\u60c5\u51b5<\/strong>\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u6570\u636e\u5206\u7ec4<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u5206\u7ec4\u573a\u666f\u3002\u901a\u8fc7\u5b57\u5178\u7684\u952e\u503c\u5bf9\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u6309\u67d0\u4e2a\u5b57\u6bb5\u8fdb\u884c\u5206\u7ec4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u5bf9\u5b66\u751f\u6210\u7ee9\u6570\u636e\u6309\u79d1\u76ee\u8fdb\u884c\u5206\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e<\/p>\n<p>data = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 85},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 90},<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 92},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 88},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 95},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 85}<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u5206\u7ec4<\/strong><\/h2>\n<p>grouped_data = {}<\/p>\n<p>for entry in data:<\/p>\n<p>    subject = entry[&#39;subject&#39;]<\/p>\n<p>    if subject not in grouped_data:<\/p>\n<p>        grouped_data[subject] = []<\/p>\n<p>    grouped_data[subject].append(entry)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>for subject, entries in grouped_data.items():<\/p>\n<p>    print(f&quot;Subject: {subject}&quot;)<\/p>\n<p>    for entry in entries:<\/p>\n<p>        print(entry)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178 <code>grouped_data<\/code>\uff0c\u7136\u540e\u904d\u5386\u6570\u636e\u5217\u8868 <code>data<\/code>\u3002\u5bf9\u4e8e\u6bcf\u4e00\u6761\u6570\u636e\uff0c\u6211\u4eec\u6839\u636e\u5176 <code>subject<\/code> \u5b57\u6bb5\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\u76f8\u5e94\u7684\u952e\u4e0b\u3002\u6700\u540e\uff0c\u6211\u4eec\u904d\u5386\u5b57\u5178\u5e76\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6269\u5c55\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u6570\u636e\u5206\u7ec4\u65f6\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u6269\u5c55\u529f\u80fd\uff0c\u4f8b\u5982\u6309\u591a\u4e2a\u5b57\u6bb5\u8fdb\u884c\u5206\u7ec4\u3001\u7edf\u8ba1\u6bcf\u7ec4\u6570\u636e\u7684\u6570\u91cf\u6216\u6c42\u548c\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u6309\u591a\u4e2a\u5b57\u6bb5\uff08\u5982\u79d1\u76ee\u548c\u59d3\u540d\uff09\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u7edf\u8ba1\u6bcf\u7ec4\u6570\u636e\u7684\u6570\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 85},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 90},<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 92},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 88},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 95},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 85}<\/p>\n<p>]<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u7edf\u8ba1\u6bcf\u7ec4\u6570\u636e\u7684\u6570\u91cf<\/strong><\/h2>\n<p>grouped_data = defaultdict(int)<\/p>\n<p>for entry in data:<\/p>\n<p>    key = (entry[&#39;subject&#39;], entry[&#39;name&#39;])<\/p>\n<p>    grouped_data[key] += 1<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u6570\u636e\u53ca\u6570\u91cf<\/strong><\/h2>\n<p>for key, count in grouped_data.items():<\/p>\n<p>    subject, name = key<\/p>\n<p>    print(f&quot;Subject: {subject}, Name: {name}, Count: {count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>defaultdict<\/code> \u6765\u7b80\u5316\u7edf\u8ba1\u8fc7\u7a0b\uff0c\u5e76\u4e14\u6309 <code>subject<\/code> \u548c <code>name<\/code> \u4e24\u4e2a\u5b57\u6bb5\u8fdb\u884c\u5206\u7ec4\u3002\u6700\u7ec8\u8f93\u51fa\u6bcf\u7ec4\u6570\u636e\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5206\u7ec4<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002\u4f7f\u7528Pandas\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u548c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528Pandas\u5bf9\u5b66\u751f\u6210\u7ee9\u6570\u636e\u6309\u79d1\u76ee\u8fdb\u884c\u5206\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 85},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 90},<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 92},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 88},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 95},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 85}<\/p>\n<p>]<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6309\u79d1\u76ee\u5206\u7ec4<\/strong><\/h2>\n<p>grouped = df.groupby(&#39;subject&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>for subject, group in grouped:<\/p>\n<p>    print(f&quot;Subject: {subject}&quot;)<\/p>\n<p>    print(group)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u4e00\u4e2aPandas DataFrame\uff0c\u7136\u540e\u4f7f\u7528 <code>groupby<\/code> \u65b9\u6cd5\u6309 <code>subject<\/code> \u8fdb\u884c\u5206\u7ec4\u3002\u6700\u540e\uff0c\u6211\u4eec\u904d\u5386\u5206\u7ec4\u5bf9\u8c61\u5e76\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6269\u5c55\u529f\u80fd<\/h3>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5206\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u805a\u5408\u3001\u8fc7\u6ee4\u3001\u8f6c\u6362\u7b49\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5206\u7ec4\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u805a\u5408\u64cd\u4f5c\uff0c\u4f8b\u5982\u8ba1\u7b97\u6bcf\u4e2a\u79d1\u76ee\u7684\u5e73\u5747\u5206\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 85},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 90},<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 92},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 88},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 95},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 85}<\/p>\n<p>]<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6309\u79d1\u76ee\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u5e73\u5747\u5206\u6570<\/strong><\/h2>\n<p>grouped = df.groupby(&#39;subject&#39;)[&#39;score&#39;].mean()<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u5e73\u5747\u5206\u6570<\/strong><\/h2>\n<p>print(grouped)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>groupby<\/code> \u65b9\u6cd5\u6309 <code>subject<\/code> \u5206\u7ec4\uff0c\u5e76\u4f7f\u7528 <code>mean<\/code> \u65b9\u6cd5\u8ba1\u7b97\u6bcf\u4e2a\u79d1\u76ee\u7684\u5e73\u5747\u5206\u6570\u3002\u6700\u7ec8\u8f93\u51fa\u6bcf\u4e2a\u79d1\u76ee\u7684\u5e73\u5747\u5206\u6570\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><p>Pandas\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u591a\u7ea7\u7d22\u5f15\u3001\u5206\u7ec4\u540e\u7684\u6570\u636e\u8f6c\u6362\u7b49\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u591a\u7ea7\u7d22\u5f15\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u5728\u5206\u7ec4\u540e\u7684\u6570\u636e\u4e0a\u5e94\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 85},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 90},<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 92},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 88},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 95},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 85}<\/p>\n<p>]<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6309\u79d1\u76ee\u548c\u59d3\u540d\u8fdb\u884c\u591a\u7ea7\u7d22\u5f15\u5206\u7ec4<\/strong><\/h2>\n<p>grouped = df.groupby([&#39;subject&#39;, &#39;name&#39;])<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u8ba1\u7b97\u5206\u6570\u7684\u6807\u51c6\u5316\u5206\u6570<\/strong><\/h2>\n<p>def standardize(x):<\/p>\n<p>    return (x - x.mean()) \/ x.std()<\/p>\n<h2><strong>\u5728\u5206\u7ec4\u540e\u7684\u6570\u636e\u4e0a\u5e94\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>standardized_scores = grouped[&#39;score&#39;].apply(standardize)<\/p>\n<h2><strong>\u8f93\u51fa\u6807\u51c6\u5316\u540e\u7684\u5206\u6570<\/strong><\/h2>\n<p>print(standardized_scores)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>groupby<\/code> \u65b9\u6cd5\u6309 <code>subject<\/code> \u548c <code>name<\/code> \u8fdb\u884c\u591a\u7ea7\u7d22\u5f15\u5206\u7ec4\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570 <code>standardize<\/code> \u6765\u8ba1\u7b97\u5206\u6570\u7684\u6807\u51c6\u5316\u5206\u6570\u3002\u6700\u540e\uff0c\u6211\u4eec\u5728\u5206\u7ec4\u540e\u7684\u6570\u636e\u4e0a\u5e94\u7528\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u5e76\u8f93\u51fa\u6807\u51c6\u5316\u540e\u7684\u5206\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528itertools.groupby\u8fdb\u884c\u6570\u636e\u5206\u7ec4<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p><code>itertools.groupby<\/code> \u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u9002\u7528\u4e8e\u5bf9\u6392\u5e8f\u540e\u7684\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u3002\u5b83\u7684\u4f7f\u7528\u8981\u6c42\u6570\u636e\u662f\u6309\u7167\u9700\u8981\u5206\u7ec4\u7684\u952e\u8fdb\u884c\u6392\u5e8f\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528 <code>itertools.groupby<\/code> \u5bf9\u5b66\u751f\u6210\u7ee9\u6570\u636e\u6309\u79d1\u76ee\u8fdb\u884c\u5206\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 85},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 90},<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 92},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 88},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 95},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 85}<\/p>\n<p>]<\/p>\n<h2><strong>\u6392\u5e8f\u6570\u636e<\/strong><\/h2>\n<p>data.sort(key=lambda x: x[&#39;subject&#39;])<\/p>\n<h2><strong>\u4f7f\u7528itertools.groupby\u8fdb\u884c\u5206\u7ec4<\/strong><\/h2>\n<p>grouped = itertools.groupby(data, key=lambda x: x[&#39;subject&#39;])<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>for subject, group in grouped:<\/p>\n<p>    print(f&quot;Subject: {subject}&quot;)<\/p>\n<p>    for entry in group:<\/p>\n<p>        print(entry)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bf9\u6570\u636e\u6309 <code>subject<\/code> \u5b57\u6bb5\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528 <code>itertools.groupby<\/code> \u8fdb\u884c\u5206\u7ec4\u3002\u6700\u540e\uff0c\u904d\u5386\u5206\u7ec4\u5bf9\u8c61\u5e76\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6269\u5c55\u529f\u80fd<\/h3>\n<\/p>\n<p><p><code>itertools.groupby<\/code> \u9002\u7528\u4e8e\u7b80\u5355\u7684\u5206\u7ec4\u64cd\u4f5c\uff0c\u4f46\u4e0d\u5982Pandas\u90a3\u6837\u529f\u80fd\u5f3a\u5927\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 <code>itertools.groupby<\/code> \u548c <code>defaultdict<\/code> \u7ed3\u5408\uff0c\u6309\u591a\u4e2a\u5b57\u6bb5\u8fdb\u884c\u5206\u7ec4\u5e76\u7edf\u8ba1\u6bcf\u7ec4\u6570\u636e\u7684\u6570\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>from collections import defaultdict<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 85},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 90},<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 92},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 88},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 95},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 85}<\/p>\n<p>]<\/p>\n<h2><strong>\u6392\u5e8f\u6570\u636e<\/strong><\/h2>\n<p>data.sort(key=lambda x: (x[&#39;subject&#39;], x[&#39;name&#39;]))<\/p>\n<h2><strong>\u4f7f\u7528itertools.groupby\u8fdb\u884c\u5206\u7ec4<\/strong><\/h2>\n<p>grouped = itertools.groupby(data, key=lambda x: (x[&#39;subject&#39;], x[&#39;name&#39;]))<\/p>\n<h2><strong>\u4f7f\u7528defaultdict\u7edf\u8ba1\u6bcf\u7ec4\u6570\u636e\u7684\u6570\u91cf<\/strong><\/h2>\n<p>grouped_data = defaultdict(int)<\/p>\n<p>for key, group in grouped:<\/p>\n<p>    for entry in group:<\/p>\n<p>        grouped_data[key] += 1<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u6570\u636e\u53ca\u6570\u91cf<\/strong><\/h2>\n<p>for key, count in grouped_data.items():<\/p>\n<p>    subject, name = key<\/p>\n<p>    print(f&quot;Subject: {subject}, Name: {name}, Count: {count}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5bf9\u6570\u636e\u6309 <code>subject<\/code> \u548c <code>name<\/code> \u5b57\u6bb5\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u4f7f\u7528 <code>itertools.groupby<\/code> \u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u4f7f\u7528 <code>defaultdict<\/code> \u7edf\u8ba1\u6bcf\u7ec4\u6570\u636e\u7684\u6570\u91cf\u3002\u6700\u7ec8\u8f93\u51fa\u6bcf\u7ec4\u6570\u636e\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528SQLAlchemy\u8fdb\u884c\u6570\u636e\u5206\u7ec4<\/h2>\n<\/p>\n<p><h3>1\u3001\u57fa\u672c\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>SQLAlchemy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684SQL\u5de5\u5177\u5305\u548c\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668\uff08ORM\uff09\uff0c\u9002\u7528\u4e8e\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u64cd\u4f5c\u3002\u4f7f\u7528SQLAlchemy\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u548c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u4f7f\u7528SQLAlchemy\u5bf9\u5b66\u751f\u6210\u7ee9\u6570\u636e\u6309\u79d1\u76ee\u8fdb\u884c\u5206\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, Column, Integer, String, select<\/p>\n<p>from sqlalchemy.ext.declarative import declarative_base<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u5f15\u64ce<\/strong><\/h2>\n<p>engine = create_engine(&#39;sqlite:\/\/\/:memory:&#39;, echo=True)<\/p>\n<h2><strong>\u521b\u5efa\u57fa\u7c7b<\/strong><\/h2>\n<p>Base = declarative_base()<\/p>\n<h2><strong>\u5b9a\u4e49\u5b66\u751f\u6210\u7ee9\u8868<\/strong><\/h2>\n<p>class StudentScore(Base):<\/p>\n<p>    __tablename__ = &#39;student_scores&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True)<\/p>\n<p>    name = Column(String)<\/p>\n<p>    subject = Column(String)<\/p>\n<p>    score = Column(Integer)<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>Base.metadata.create_all(engine)<\/p>\n<h2><strong>\u521b\u5efa\u4f1a\u8bdd<\/strong><\/h2>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<h2><strong>\u63d2\u5165\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 85},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 90},<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 92},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 88},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 95},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 85}<\/p>\n<p>]<\/p>\n<p>for entry in data:<\/p>\n<p>    student_score = StudentScore(entry)<\/p>\n<p>    session.add(student_score)<\/p>\n<p>session.commit()<\/p>\n<h2><strong>\u4f7f\u7528SQLAlchemy\u8fdb\u884c\u5206\u7ec4\u67e5\u8be2<\/strong><\/h2>\n<p>stmt = select(StudentScore.subject, StudentScore.name, StudentScore.score).order_by(StudentScore.subject)<\/p>\n<p>result = session.execute(stmt).fetchall()<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u5178\u8fdb\u884c\u5206\u7ec4<\/strong><\/h2>\n<p>grouped_data = {}<\/p>\n<p>for entry in result:<\/p>\n<p>    subject = entry[0]<\/p>\n<p>    if subject not in grouped_data:<\/p>\n<p>        grouped_data[subject] = []<\/p>\n<p>    grouped_data[subject].append(entry)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>for subject, entries in grouped_data.items():<\/p>\n<p>    print(f&quot;Subject: {subject}&quot;)<\/p>\n<p>    for entry in entries:<\/p>\n<p>        print(entry)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528SQLAlchemy\u521b\u5efa\u4e00\u4e2a\u5185\u5b58\u6570\u636e\u5e93\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u5b66\u751f\u6210\u7ee9\u8868\u3002\u7136\u540e\u63d2\u5165\u793a\u4f8b\u6570\u636e\uff0c\u5e76\u4f7f\u7528SQLAlchemy\u7684\u67e5\u8be2\u529f\u80fd\u6309 <code>subject<\/code> \u8fdb\u884c\u5206\u7ec4\u3002\u6700\u540e\uff0c\u5c06\u67e5\u8be2\u7ed3\u679c\u6309 <code>subject<\/code> \u5b57\u6bb5\u5206\u7ec4\u5e76\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6269\u5c55\u529f\u80fd<\/h3>\n<\/p>\n<p><p>SQLAlchemy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u67e5\u8be2\u548c\u805a\u5408\u529f\u80fd\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528SQLAlchemy\u8fdb\u884c\u5206\u7ec4\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u79d1\u76ee\u7684\u5e73\u5747\u5206\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sqlalchemy import create_engine, Column, Integer, String, func<\/p>\n<p>from sqlalchemy.ext.declarative import declarative_base<\/p>\n<p>from sqlalchemy.orm import sessionmaker<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u5e93\u5f15\u64ce<\/strong><\/h2>\n<p>engine = create_engine(&#39;sqlite:\/\/\/:memory:&#39;, echo=True)<\/p>\n<h2><strong>\u521b\u5efa\u57fa\u7c7b<\/strong><\/h2>\n<p>Base = declarative_base()<\/p>\n<h2><strong>\u5b9a\u4e49\u5b66\u751f\u6210\u7ee9\u8868<\/strong><\/h2>\n<p>class StudentScore(Base):<\/p>\n<p>    __tablename__ = &#39;student_scores&#39;<\/p>\n<p>    id = Column(Integer, primary_key=True)<\/p>\n<p>    name = Column(String)<\/p>\n<p>    subject = Column(String)<\/p>\n<p>    score = Column(Integer)<\/p>\n<h2><strong>\u521b\u5efa\u8868<\/strong><\/h2>\n<p>Base.metadata.create_all(engine)<\/p>\n<h2><strong>\u521b\u5efa\u4f1a\u8bdd<\/strong><\/h2>\n<p>Session = sessionmaker(bind=engine)<\/p>\n<p>session = Session()<\/p>\n<h2><strong>\u63d2\u5165\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 85},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 90},<\/p>\n<p>    {&#39;name&#39;: &#39;Alice&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 92},<\/p>\n<p>    {&#39;name&#39;: &#39;Bob&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 88},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;Math&#39;, &#39;score&#39;: 95},<\/p>\n<p>    {&#39;name&#39;: &#39;Charlie&#39;, &#39;subject&#39;: &#39;English&#39;, &#39;score&#39;: 85}<\/p>\n<p>]<\/p>\n<p>for entry in data:<\/p>\n<p>    student_score = StudentScore(entry)<\/p>\n<p>    session.add(student_score)<\/p>\n<p>session.commit()<\/p>\n<h2><strong>\u4f7f\u7528SQLAlchemy\u8fdb\u884c\u5206\u7ec4\u67e5\u8be2\uff0c\u5e76\u8ba1\u7b97\u5e73\u5747\u5206\u6570<\/strong><\/h2>\n<p>stmt = session.query(StudentScore.subject, func.avg(StudentScore.score)).group_by(StudentScore.subject)<\/p>\n<p>result = session.execute(stmt).fetchall()<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u7ec4\u540e\u7684\u5e73\u5747\u5206\u6570<\/strong><\/h2>\n<p>for entry in result:<\/p>\n<p>    print(f&quot;Subject: {entry[0]}, Average Score: {entry[1]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528SQLAlchemy\u7684\u805a\u5408\u51fd\u6570 <code>func.avg<\/code> \u8ba1\u7b97\u6bcf\u4e2a\u79d1\u76ee\u7684\u5e73\u5747\u5206\u6570\uff0c\u5e76\u4f7f\u7528 <code>group_by<\/code> \u65b9\u6cd5\u6309 <code>subject<\/code> \u5206\u7ec4\u3002\u6700\u7ec8\u8f93\u51fa\u6bcf\u4e2a\u79d1\u76ee\u7684\u5e73\u5747\u5206\u6570\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5bf9\u6570\u636e\u8fdb\u884c\u5206<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u8f7b\u677e\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u3002\u901a\u8fc7<code>groupby()<\/code>\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u7684\u5217\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5206\u7ec4\u5e94\u7528\u805a\u5408\u51fd\u6570\uff0c\u4f8b\u5982\u6c42\u548c\u3001\u5e73\u5747\u503c\u6216\u8ba1\u6570\u7b49\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {\n    &#39;\u7c7b\u522b&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;A&#39;, &#39;B&#39;, &#39;A&#39;],\n    &#39;\u503c&#39;: [10, 20, 30, 40, 50]\n}\ndf = pd.DataFrame(data)\ngrouped = df.groupby(&#39;\u7c7b\u522b&#39;).sum()\nprint(grouped)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u6bcf\u4e2a\u7c7b\u522b\u7684\u503c\u603b\u548c\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u6709\u54ea\u4e9b\u5de5\u5177\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u5206\u7ec4\uff1f<\/strong><br \/>\u9664\u4e86<code>pandas<\/code>\u5916\uff0cPython\u4e2d\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u548c<code>itertools<\/code>\u7b49\u5e93\u6765\u5b9e\u73b0\u6570\u636e\u5206\u7ec4\u3002<code>numpy<\/code>\u9002\u5408\u5904\u7406\u6570\u503c\u6570\u7ec4\uff0c\u800c<code>itertools.groupby()<\/code>\u5219\u53ef\u4ee5\u7528\u4e8e\u5bf9\u5df2\u6392\u5e8f\u7684\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u3002\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53d6\u51b3\u4e8e\u60a8\u7684\u6570\u636e\u7c7b\u578b\u548c\u5177\u4f53\u9700\u6c42\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5206\u7ec4\u540e\u7684\u6570\u636e\uff1f<\/strong><br \/>\u4e00\u65e6\u60a8\u5bf9\u6570\u636e\u8fdb\u884c\u4e86\u5206\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u805a\u5408\u51fd\u6570\u6765\u8ba1\u7b97\u5404\u7ec4\u7684\u7edf\u8ba1\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>.mean()<\/code>\u3001<code>.count()<\/code>\u6216<code>.agg()<\/code>\u7b49\u65b9\u6cd5\u6765\u83b7\u53d6\u6bcf\u4e2a\u5206\u7ec4\u7684\u5e73\u5747\u503c\u3001\u8ba1\u6570\u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u7edf\u8ba1\u4fe1\u606f\u3002\u8fd9\u4f7f\u5f97\u60a8\u80fd\u591f\u5feb\u901f\u4e86\u89e3\u6570\u636e\u7684\u6574\u4f53\u8d8b\u52bf\u548c\u7279\u5f81\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u5206\u7ec4\u8f93\u51fa\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5b57\u5178\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4f7f\u7528iterto [&hellip;]","protected":false},"author":3,"featured_media":1159466,"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\/1159456"}],"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=1159456"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159456\/revisions"}],"predecessor-version":[{"id":1159469,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159456\/revisions\/1159469"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159466"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}