{"id":959931,"date":"2024-12-27T03:45:15","date_gmt":"2024-12-26T19:45:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/959931.html"},"modified":"2024-12-27T03:45:16","modified_gmt":"2024-12-26T19:45:16","slug":"python%e5%a6%82%e4%bd%95%e7%bb%9f%e8%ae%a1%e5%87%ba%e7%8e%b0%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/959931.html","title":{"rendered":"python\u5982\u4f55\u7edf\u8ba1\u51fa\u73b0\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102559\/ee21c0b9-61c7-4d57-9281-6eb7ae83f152.webp\" alt=\"python\u5982\u4f55\u7edf\u8ba1\u51fa\u73b0\u4e2a\u6570\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/>\u5728Python\u4e2d\uff0c\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\uff1a<strong>\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u3001collections\u5e93\u7684Counter\u7c7b\u3001pandas\u5e93\u7684value_counts()\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u662f\u6700\u57fa\u672c\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u904d\u5386\u5217\u8868\u6216\u5b57\u7b26\u4e32\uff0c\u5c06\u5143\u7d20\u4f5c\u4e3a\u952e\uff0c\u51fa\u73b0\u6b21\u6570\u4f5c\u4e3a\u503c\u8fdb\u884c\u5b58\u50a8\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u7edf\u8ba1\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u3002\u540c\u65f6\uff0c\u4f7f\u7528collections\u5e93\u7684Counter\u7c7b\u662f\u4e00\u79cd\u66f4\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e13\u95e8\u7684\u7c7b\u6765\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\uff0c\u4e14\u4f7f\u7528\u7b80\u5355\u3001\u529f\u80fd\u5f3a\u5927\u3002\u6b64\u5916\uff0cpandas\u5e93\u7684value_counts()\u51fd\u6570\u5219\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u9886\u57df\u4e2d\u7684\u6570\u636e\u7edf\u8ba1\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u662f\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u7684\u6700\u57fa\u7840\u65b9\u6cd5\u3002\u901a\u8fc7\u904d\u5386\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5c06\u6bcf\u4e2a\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff0c\u51fa\u73b0\u6b21\u6570\u4f5c\u4e3a\u503c\u8fdb\u884c\u5b58\u50a8\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u4e14\u9002\u7528\u4e8e\u5404\u79cd\u7c7b\u578b\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\uff0c\u7528\u4e8e\u5b58\u50a8\u7edf\u8ba1\u7ed3\u679c\u3002\u7136\u540e\uff0c\u904d\u5386\u5217\u8868\u6216\u5b57\u7b26\u4e32\u7b49\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u5143\u7d20\uff0c\u68c0\u67e5\u5176\u662f\u5426\u5728\u5b57\u5178\u4e2d\u3002\u5982\u679c\u5b58\u5728\uff0c\u5219\u5c06\u5176\u5bf9\u5e94\u7684\u503c\u52a01\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u5c06\u5176\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\uff0c\u5e76\u5c06\u503c\u8bbe\u4e3a1\u3002\u904d\u5386\u5b8c\u6210\u540e\uff0c\u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u5373\u4e3a\u5143\u7d20\u53ca\u5176\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;]<\/p>\n<p>count_dict = {}<\/p>\n<p>for item in data:<\/p>\n<p>    if item in count_dict:<\/p>\n<p>        count_dict[item] += 1<\/p>\n<p>    else:<\/p>\n<p>        count_dict[item] = 1<\/p>\n<p>print(count_dict)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u7c7b\u578b\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u4f46\u5f53\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528collections\u5e93\u7684Counter\u7c7b<\/p>\n<\/p>\n<p><p>Python\u7684collections\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3aCounter\u7684\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u3002Counter\u7c7b\u662f\u5b57\u5178\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u7528\u4e8e\u8ba1\u6570\u5bf9\u8c61\uff0c\u80fd\u591f\u5f88\u65b9\u4fbf\u5730\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fdb\u884c\u7edf\u8ba1\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Counter\u7c7b\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4f20\u5165Counter\u7684\u6784\u9020\u51fd\u6570\u5373\u53ef\u3002Counter\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u4e3a\u5143\u7d20\uff0c\u503c\u4e3a\u5143\u7d20\u51fa\u73b0\u7684\u6b21\u6570\u3002Counter\u7c7b\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u989d\u5916\u7684\u65b9\u6cd5\uff0c\u5982most_common()\uff0c\u7528\u4e8e\u83b7\u53d6\u51fa\u73b0\u9891\u7387\u6700\u9ad8\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>data = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;]<\/p>\n<p>counter = Counter(data)<\/p>\n<p>print(counter)<\/p>\n<p>print(counter.most_common(2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528Counter\u7c7b\u4e0d\u4ec5\u4ee3\u7801\u7b80\u6d01\uff0c\u800c\u4e14\u6027\u80fd\u8f83\u9ad8\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528pandas\u5e93\u7684value_counts()\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u9886\u57df\uff0cpandas\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\u3002\u5bf9\u4e8eSeries\u5bf9\u8c61\uff0cpandas\u63d0\u4f9b\u4e86value_counts()\u51fd\u6570\uff0c\u7528\u4e8e\u7edf\u8ba1\u6bcf\u4e2a\u503c\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528value_counts()\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5c06\u5176\u5e94\u7528\u4e8eSeries\u5bf9\u8c61\u5373\u53ef\u3002\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2aSeries\uff0c\u5176\u4e2d\u7d22\u5f15\u4e3a\u5143\u7d20\uff0c\u503c\u4e3a\u51fa\u73b0\u6b21\u6570\u3002value_counts()\u51fd\u6570\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u53c2\u6570\uff0c\u53ef\u7528\u4e8e\u5b9a\u5236\u7edf\u8ba1\u7ed3\u679c\uff0c\u5982normalize\u53c2\u6570\u7528\u4e8e\u8fd4\u56de\u76f8\u5bf9\u9891\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.Series([&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;])<\/p>\n<p>counts = data.value_counts()<\/p>\n<p>print(counts)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>value_counts()\u51fd\u6570\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u9886\u57df\uff0c\u80fd\u591f\u5feb\u901f\u7edf\u8ba1\u6570\u636e\uff0c\u5e76\u4e0epandas\u7684\u5176\u4ed6\u529f\u80fd\u65e0\u7f1d\u96c6\u6210\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6bd4\u8f83\u4e0e\u9009\u62e9<\/p>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b57\u5178\u7edf\u8ba1<\/strong>\uff1a\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\uff0c\u4ee3\u7801\u7b80\u5355\u6613\u61c2\u3002\u7f3a\u70b9\u662f\u9700\u8981\u624b\u52a8\u5904\u7406\u7edf\u8ba1\u903b\u8f91\uff0c\u5f53\u6570\u636e\u91cf\u5927\u65f6\u6027\u80fd\u8f83\u5dee\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Counter\u7c7b<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u6027\u80fd\u9ad8\uff0c\u4ee3\u7801\u7b80\u6d01\u3002\u63d0\u4f9b\u4e86\u989d\u5916\u7684\u529f\u80fd\uff0c\u5982\u83b7\u53d6\u6700\u9ad8\u9891\u5143\u7d20\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>value_counts()\u51fd\u6570<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u9886\u57df\uff0c\u80fd\u591f\u5feb\u901f\u7edf\u8ba1\u6570\u636e\uff0c\u5e76\u4e0epandas\u7684\u5176\u4ed6\u529f\u80fd\u7ed3\u5408\u4f7f\u7528\u3002\u9002\u5408\u5904\u7406pandas\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u63d0\u9ad8\u4ee3\u7801\u6548\u7387\uff0c\u7b80\u5316\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b9e\u8df5\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u7684\u9700\u6c42\u5e38\u89c1\u4e8e\u6570\u636e\u5206\u6790\u3001\u6587\u672c\u5904\u7406\u3001\u65e5\u5fd7\u5206\u6790\u7b49\u573a\u666f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u4f53\u7684\u5e94\u7528\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6587\u672c\u8bcd\u9891\u7edf\u8ba1<\/strong>\uff1a\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u9886\u57df\uff0c\u7edf\u8ba1\u6587\u672c\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u7684\u51fa\u73b0\u6b21\u6570\u662f\u57fa\u672c\u4efb\u52a1\u4e4b\u4e00\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u5feb\u901f\u5b9e\u73b0\u8bcd\u9891\u7edf\u8ba1\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>text = &quot;Python is great and Python is fun&quot;<\/p>\n<p>words = text.split()<\/p>\n<p>word_count = Counter(words)<\/p>\n<p>print(word_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u65e5\u5fd7\u5206\u6790<\/strong>\uff1a\u5728\u65e5\u5fd7\u5206\u6790\u4e2d\uff0c\u7edf\u8ba1\u6bcf\u4e2aIP\u5730\u5740\u7684\u8bbf\u95ee\u6b21\u6570\u6216\u6bcf\u79cd\u9519\u8bef\u7c7b\u578b\u7684\u51fa\u73b0\u6b21\u6570\u662f\u5e38\u89c1\u4efb\u52a1\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6216Counter\u7c7b\u5b9e\u73b0\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>logs = [<\/p>\n<p>    &quot;192.168.1.1 - Error 404&quot;,<\/p>\n<p>    &quot;192.168.1.2 - Error 500&quot;,<\/p>\n<p>    &quot;192.168.1.1 - Error 404&quot;,<\/p>\n<p>    &quot;192.168.1.3 - Error 404&quot;,<\/p>\n<p>    &quot;192.168.1.2 - Error 500&quot;<\/p>\n<p>]<\/p>\n<p>ip_count = Counter(log.split()[0] for log in logs)<\/p>\n<p>error_count = Counter(log.split(&quot;-&quot;)[1].strip() for log in logs)<\/p>\n<p>print(ip_count)<\/p>\n<p>print(error_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6570\u636e\u5206\u6790<\/strong>\uff1a\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u7edf\u8ba1\u67d0\u5217\u4e2d\u6bcf\u4e2a\u503c\u7684\u51fa\u73b0\u6b21\u6570\u662f\u5e38\u89c1\u9700\u6c42\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u7684value_counts()\u51fd\u6570\u8f7b\u677e\u5b9e\u73b0\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.DataFrame({<\/p>\n<p>    &#39;fruit&#39;: [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;],<\/p>\n<p>    &#39;quantity&#39;: [5, 3, 4, 2, 4, 3]<\/p>\n<p>})<\/p>\n<p>fruit_count = data[&#39;fruit&#39;].value_counts()<\/p>\n<p>print(fruit_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u5e94\u7528\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u770b\u5230\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u7684\u65b9\u6cd5\u5728\u5b9e\u9645\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u80fd\u4f7f\u4efb\u52a1\u5b9e\u73b0\u5f97\u66f4\u52a0\u7b80\u6d01\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7edf\u8ba1\u5217\u8868\u4e2d\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>collections.Counter<\/code>\u7c7b\u6765\u65b9\u4fbf\u5730\u7edf\u8ba1\u5217\u8868\u4e2d\u5404\u4e2a\u5143\u7d20\u7684\u51fa\u73b0\u6b21\u6570\u3002\u9996\u5148\uff0c\u5bfc\u5165Counter\u6a21\u5757\uff0c\u7136\u540e\u4f20\u5165\u4f60\u7684\u5217\u8868\uff0cCounter\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u952e\u662f\u5143\u7d20\uff0c\u503c\u662f\u5bf9\u5e94\u7684\u51fa\u73b0\u6b21\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\nmy_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;]\ncount = Counter(my_list)\nprint(count)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4f60\u5c06\u770b\u5230<code>Counter({&#39;apple&#39;: 3, &#39;banana&#39;: 2, &#39;orange&#39;: 1})<\/code>\uff0c\u8fd9\u8868\u793a&#39;apple&#39;\u51fa\u73b0\u4e863\u6b21\uff0c&#39;banana&#39;\u51fa\u73b0\u4e862\u6b21\uff0c&#39;orange&#39;\u51fa\u73b0\u4e861\u6b21\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5b57\u7b26\u4e32\u4e2d\u7edf\u8ba1\u5404\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u9891\u7387\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u7edf\u8ba1\u5b57\u7b26\u4e32\u4e2d\u5404\u4e2a\u5b57\u7b26\u7684\u51fa\u73b0\u9891\u7387\uff0c\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>collections.Counter<\/code>\u3002\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5217\u8868\u6216\u76f4\u63a5\u4f20\u5165\u5b57\u7b26\u4e32\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from collections import Counter\n\nmy_string = &quot;hello world&quot;\ncount = Counter(my_string)\nprint(count)\n<\/code><\/pre>\n<p>\u6267\u884c\u540e\uff0c\u4f60\u4f1a\u5f97\u5230\u6bcf\u4e2a\u5b57\u7b26\u53ca\u5176\u51fa\u73b0\u6b21\u6570\u7684\u7edf\u8ba1\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5728Pandas\u4e2d\u5982\u4f55\u7edf\u8ba1\u6570\u636e\u6846\u4e2d\u67d0\u5217\u7684\u503c\u51fa\u73b0\u6b21\u6570\uff1f<\/strong><br \/>\u5bf9\u4e8e\u4f7f\u7528Pandas\u5e93\u5904\u7406\u6570\u636e\u6846\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>value_counts()<\/code>\u65b9\u6cd5\u6765\u7edf\u8ba1\u67d0\u4e00\u5217\u4e2d\u5404\u4e2a\u503c\u7684\u51fa\u73b0\u6b21\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;fruits&#39;: [&#39;apple&#39;, &#39;banana&#39;, &#39;apple&#39;, &#39;orange&#39;, &#39;banana&#39;, &#39;apple&#39;]}\ndf = pd.DataFrame(data)\ncount = df[&#39;fruits&#39;].value_counts()\nprint(count)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u6bcf\u79cd\u6c34\u679c\u5728\u6570\u636e\u6846\u4e2d\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u975e\u5e38\u9002\u5408\u8fdb\u884c\u6570\u636e\u5206\u6790\u65f6\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u5728Python\u4e2d\uff0c\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u7684\u5e38\u89c1\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5b57\u5178\u7edf\u8ba1\u3001collections\u5e93\u7684Counte [&hellip;]","protected":false},"author":3,"featured_media":959940,"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\/959931"}],"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=959931"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959931\/revisions"}],"predecessor-version":[{"id":959941,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/959931\/revisions\/959941"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/959940"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=959931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=959931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=959931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}