{"id":480,"date":"2025-06-10T21:04:43","date_gmt":"2025-06-10T13:04:43","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/480.html"},"modified":"2025-06-10T21:04:43","modified_gmt":"2025-06-10T13:04:43","slug":"pandas-%e6%95%b0%e6%8d%ae%e6%a1%86%e7%9a%84-value_counts-%e6%96%b9%e6%b3%95%ef%bc%9a%e9%80%9a%e8%bf%87%e7%a4%ba%e4%be%8b%e8%a7%a3%e6%9e%90","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/480.html","title":{"rendered":"Pandas \u6570\u636e\u6846\u7684 value_counts() \u65b9\u6cd5\uff1a\u901a\u8fc7\u793a\u4f8b\u89e3\u6790"},"content":{"rendered":"<p><body><\/p>\n<h3>&#20171;&#32461;<\/h3>\n<p>&#29066;&#29483;&#26159;Python&#30340;&#24320;&#28304;&#25968;&#25454;&#25805;&#20316;&#21644;&#20998;&#26512;&#24211;&#65292;&#25552;&#20379;&#29992;&#20110;&#22788;&#29702;&#25968;&#20540;&#34920;&#26684;&#21644;&#26102;&#38388;&#24207;&#21015;&#30340;&#25968;&#25454;&#32467;&#26500;&#20197;&#21450;&#30456;&#24212;&#30340;&#25805;&#20316;&#21151;&#33021;&#12290;&#22312;&#23427;&#30340;&#22810;&#21151;&#33021;&#29305;&#24615;&#20013;&#65292;<code>DataFrame.value_counts()<\/code>&#36825;&#26159;&#19968;&#31181;&#38750;&#24120;&#37325;&#35201;&#30340;&#25968;&#25454;&#20998;&#26512;&#26041;&#27861;&#65292;&#20351;&#29992;&#25143;&#33021;&#22815;&#35745;&#31639;DataFrame&#25110;Series&#20013;&#30340;&#21807;&#19968;&#20540;&#39057;&#29575;&#12290;&#26412;&#25945;&#31243;&#23558;&#28145;&#20837;&#25506;&#35752;&#36825;&#19968;&#20027;&#39064;&#12290;<code>value_counts()<\/code>&#26041;&#27861;&#65292;&#36890;&#36807;&#36880;&#27493;&#22797;&#26434;&#30340;&#20363;&#23376;&#26469;&#28436;&#31034;&#20854;&#24212;&#29992;&#12290;<\/p>\n<h3>&#22522;&#30784;&#29702;&#35770;<\/h3>\n<p>&#22312;&#25105;&#20204;&#28145;&#20837;&#25506;&#35752;&#31034;&#20363;&#20043;&#21069;&#65292;&#29702;&#35299;&#20160;&#20040;&#26159;&#33267;&#20851;&#37325;&#35201;&#12290;<code>value_counts()<\/code>&#35813;&#26041;&#27861;&#36820;&#22238;&#19968;&#20010;Series&#65292;&#20854;&#20013;&#21253;&#21547;&#21807;&#19968;&#20540;&#30340;&#35745;&#25968;&#12290;&#27492;&#20989;&#25968;&#22312;&#25506;&#32034;&#24615;&#25968;&#25454;&#20998;&#26512;&#20013;&#38750;&#24120;&#26377;&#29992;&#65292;&#20351;&#25105;&#20204;&#33021;&#22815;&#24555;&#36895;&#35782;&#21035;&#39057;&#29575;&#20998;&#24067;&#12290;<\/p>\n<p>&#39318;&#20808;&#65292;&#35753;&#25105;&#20204;&#21019;&#24314;&#19968;&#20010;&#22522;&#26412;&#30340;pandas DataFrame&#65306;<\/p>\n<pre><code>import pandas as pd\n\n# Sample DataFrame\ndata = {'color': ['blue', 'green', 'red', 'blue', 'green']}\ndataframe = pd.DataFrame(data)\n<\/code><\/pre>\n<p>&#29616;&#22312;&#65292;&#25105;&#20204;&#26469;&#25171;&#30005;&#35805;&#12290;<code>value_counts()<\/code>&#22312;&ldquo;&#39068;&#33394;&rdquo;&#21015;&#19978;&#30340;&#26041;&#27861;&#65306;<\/p>\n<pre><code>print(dataframe['color'].value_counts())\n<\/code><\/pre>\n<p>&#22909;&#30340;&#65292;&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<\/p>\n<pre><code>blue     2\ngreen    2\nred      1\nName: color, dtype: int64\n<\/code><\/pre>\n<p>&#22914;&#22270;&#25152;&#31034;&#65292;&#21487;&#20197;&#30475;&#35265;&#27599;&#31181;&#39068;&#33394;&#20986;&#29616;&#30340;&#39057;&#29575;&#65306;&#34013;&#33394;&#21644;&#32511;&#33394;&#21508;&#20986;&#29616;&#20004;&#27425;&#65292;&#32780;&#32418;&#33394;&#21482;&#20986;&#29616;&#19968;&#27425;&#12290;<\/p>\n<h3>&#23450;&#21046;&#21270;<code>value_counts()<\/code><\/h3>\n<p>&#23545;&#19981;&#36215;&#65292;&#25105;&#19981;&#26126;&#30333;&#20320;&#30340;&#24847;&#24605;&#12290;<code>value_counts()<\/code>&#26041;&#27861;&#25552;&#20379;&#20102;&#22810;&#31181;&#21442;&#25968;&#26469;&#23450;&#21046;&#20854;&#36755;&#20986;&#65292;&#20363;&#22914;<code>sort<\/code>&#22909;&#30340;&#65292;&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<code>ascending<\/code>&#21644;&#20320;&#26377;&#20160;&#20040;&#20107;&#24773;&#38656;&#35201;&#24110;&#21161;&#21527;&#65311;<code>normalize<\/code>&#35753;&#25105;&#20204;&#30475;&#30475;&#22914;&#20309;&#25226;&#36825;&#20123;&#24212;&#29992;&#21040;&#25105;&#20204;&#30340;&#25968;&#25454;&#19978;&#65306;<\/p>\n<pre><code>print(dataframe['color'].value_counts(sort=True, ascending=True))\n<\/code><\/pre>\n<p>&#29616;&#22312;&#65292;&#36755;&#20986;&#23558;&#20250;&#25353;&#39068;&#33394;&#20986;&#29616;&#30340;&#27425;&#25968;&#20174;&#23569;&#21040;&#22810;&#25490;&#24207;&#65306;<\/p>\n<pre><code>red      1\nblue     2\ngreen    2\nName: color, dtype: int64\n<\/code><\/pre>\n<p>&#25265;&#27465;&#65292;&#25105;&#19981;&#22826;&#26126;&#30333;&#24744;&#30340;&#24847;&#24605;&#65292;&#35831;&#37325;&#26032;&#25551;&#36848;&#19968;&#19979;&#12290;<code>normalize=True<\/code>&#21487;&#20197;&#24471;&#21040;&#30456;&#23545;&#39057;&#29575;&#65306;<\/p>\n<pre><code>print(dataframe['color'].value_counts(normalize=True))\n<\/code><\/pre>\n<p>&#36825;&#37324;&#26174;&#31034;&#20102;&#27599;&#20010;&#39068;&#33394;&#30340;&#27604;&#20363;&#65306;<\/p>\n<pre><code>blue     0.4\ngreen    0.4\nred      0.2\nName: color, dtype: int64\n<\/code><\/pre>\n<h3>&#27491;&#22312;&#20351;&#29992;&#12290;<code>value_counts()<\/code>&#22312;&#22810;&#20010;&#21015;&#19978;<\/h3>\n<p>&#24456;&#25265;&#27465;&#65292;<code>value_counts()<\/code>&#19981;&#33021;&#30452;&#25509;&#29992;&#20110;DataFrame&#23545;&#35937;&#22312;&#22810;&#20010;&#21015;&#19978;&#35745;&#25968;&#12290;&#20294;&#26159;&#65292;&#20320;&#21487;&#20197;&#36890;&#36807;&#23558;&#24863;&#20852;&#36259;&#30340;&#37096;&#20998;&#30340;&#21015;&#32452;&#21512;&#21040;&#19968;&#20010;Series&#25110;&#20351;&#29992;&#24037;&#20316;&#27969;&#26469;&#23454;&#29616;&#12290;&#35753;&#25105;&#20204;&#25506;&#32034;&#19968;&#31181;&#31616;&#21333;&#30340;&#26041;&#27861;&#26469;&#24212;&#29992;&#12290;<code>value_counts()<\/code>&#22312;&#22810;&#20010;&#21015;&#19978;&#20351;&#29992;<code>melt()<\/code>&#20320;&#22909;&#65281;&#26377;&#20160;&#20040;&#25105;&#21487;&#20197;&#24110;&#21161;&#24744;&#30340;&#21527;&#65311;<\/p>\n<pre><code># Assuming the same DataFrame 'dataframe'\ndataframe['number'] = [1, 2, 1, 1, 3]  # Add a new column\n\nmelted_df = pd.melt(dataframe)\nprint(melted_df['value'].value_counts())\n<\/code><\/pre>\n<p>&#35813;&#26041;&#27861;&#23454;&#36136;&#19978;&#37325;&#26032;&#26500;&#36896;&#25104;DataFrame&#65292;&#20351;&#24471;<code>value_counts()<\/code>&#36866;&#29992;&#12290;&#36755;&#20986;&#23558;&#26159;&#25152;&#26377;&#21015;&#20540;&#30340;&#32452;&#21512;&#35745;&#25968;&#12290;<\/p>\n<h3>&#39640;&#32423;&#31034;&#20363;&#65306;&#25353;&#32452;&#20998;&#32452;<code>value_counts()<\/code><\/h3>\n<p>&#21478;&#19968;&#20010;&#24378;&#22823;&#30340;pandas&#21151;&#33021;&#26159;&#20351;&#29992;groupby()&#23545;&#25968;&#25454;&#36827;&#34892;&#20998;&#32452;&#12290;<code>groupby()<\/code>&#26041;&#27861;&#65292;&#21487;&#20197;&#19982;&#20854;&#20182;&#26041;&#27861;&#32467;&#21512;&#20351;&#29992;&#12290;<code>value_counts()<\/code>&#20026;&#20102;&#36827;&#34892;&#26356;&#22797;&#26434;&#30340;&#20998;&#26512;&#65292;&#25105;&#20204;&#26469;&#30475;&#19968;&#20010;&#20998;&#32452;&#20540;&#35745;&#25968;&#30340;&#20363;&#23376;&#65306;<\/p>\n<pre><code># More complex DataFrame\ncomplex_data = {'color': ['blue', 'green', 'red', 'blue', 'green', 'green'],\n                'shape': ['circle', 'triangle', 'circle', 'square', 'square', 'circle']}\ncomplex_df = pd.DataFrame(complex_data)\n\ngb = complex_df.groupby('color')['shape'].value_counts()\nprint(gb)\n<\/code><\/pre>\n<p>&#35813;&#25805;&#20316;&#36890;&#36807;&ldquo;&#39068;&#33394;&rdquo;&#21015;&#23545;DataFrame&#36827;&#34892;&#20998;&#32452;&#65292;&#28982;&#21518;&#24212;&#29992;&#12290;<code>value_counts()<\/code>&#22312;&#27599;&#20010;&#32452;&#30340;&#8217;&#24418;&#29366;&#8217;&#21015;&#19978;&#12290;&#32467;&#26524;&#26159;&#19968;&#20010;&#26174;&#31034;&#27599;&#31181;&#39068;&#33394;&#30340;&#25968;&#37327;&#30340;&#22810;&#32500;&#32034;&#24341;Series&#12290;<\/p>\n<h3>&#32467;&#35770;&#12290;<\/h3>\n<p>&#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#38382;&#39064;&#27809;&#26377;&#23436;&#20840;&#25552;&#20379;&#32473;&#25105;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;&#35831;&#37325;&#26032;&#25551;&#36848;&#24744;&#24819;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<code>value_counts()<\/code>pandas&#30340;method&#26159;&#19968;&#20010;&#22810;&#21151;&#33021;&#24037;&#20855;&#65292;&#29992;&#20110;&#22312;Series&#20013;&#35745;&#25968;&#21807;&#19968;&#30340;&#20540;&#12290;&#22312;&#36825;&#31687;&#25945;&#31243;&#20013;&#65292;&#25105;&#20204;&#25506;&#35752;&#20102;&#22914;&#20309;&#21033;&#29992;&#36825;&#20010;&#26041;&#27861;&#30340;&#19981;&#21516;&#26041;&#24335;&#65292;&#20174;&#31616;&#21333;&#30340;&#39057;&#29575;&#35745;&#25968;&#21040;&#26356;&#39640;&#32423;&#30340;&#24212;&#29992;&#65292;&#21253;&#25324;&#25968;&#25454;&#20998;&#32452;&#21644;&#25805;&#20316;&#12290;&#36890;&#36807;&#25484;&#25569;&#36825;&#39033;&#25216;&#33021;&#65292;&#20320;&#21487;&#20197;&#26356;&#22909;&#22320;&#22788;&#29702;&#25968;&#25454;&#24182;&#20174;&#20013;&#25552;&#21462;&#26377;&#20215;&#20540;&#30340;&#20449;&#24687;&#12290;<code>value_counts()<\/code>&#24403;&#28982;&#65292;&#20320;&#21487;&#20197;&#36890;&#36807;&#22686;&#24378;&#25968;&#25454;&#20998;&#26512;&#36807;&#31243;&#26469;&#33719;&#21462;&#23545;&#25968;&#25454;&#38598;&#30340;&#26356;&#28145;&#23618;&#27425;&#29702;&#35299;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#29066;&#29483;&#26159;Python&#30340;&#24320;&#28304;&#25968;&#25454;&#25805;&#20316;&#21644;&#20998;&#26512;&#24211;&#65292;&#25552;&#20379;&#038;..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-480","my1js","type-my1js","status-publish","hentry","my1js2nav-jcyy"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=480"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=480"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=480"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}