{"id":914,"date":"2024-11-07T10:05:36","date_gmt":"2024-11-07T02:05:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/914\/"},"modified":"2024-11-07T10:05:36","modified_gmt":"2024-11-07T02:05:36","slug":"pandas-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0-countif-%e5%87%bd%e6%95%b0%e7%bb%9f%e8%ae%a1%e6%af%8f%e8%a1%8c%e5%a4%a7%e4%ba%8e%e6%8c%87%e6%a0%87%e5%80%bc%e7%9a%84%e5%88%97%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/914\/","title":{"rendered":"Pandas \u5982\u4f55\u5b9e\u73b0 COUNTIF \u51fd\u6570\u7edf\u8ba1\u6bcf\u884c\u5927\u4e8e\u6307\u6807\u503c\u7684\u5217\u6570\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-990\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173064276872726.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173064276872726.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173064276872726-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173064276872726-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173064276872726-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"Pandas \u5982\u4f55\u5b9e\u73b0 COUNTIF \u51fd\u6570\u7edf\u8ba1\u6bcf\u884c\u5927\u4e8e\u6307\u6807\u503c\u7684\u5217\u6570\uff1f\u63d2\u56fe\" alt=\"Pandas \u5982\u4f55\u5b9e\u73b0 COUNTIF \u51fd\u6570\u7edf\u8ba1\u6bcf\u884c\u5927\u4e8e\u6307\u6807\u503c\u7684\u5217\u6570\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 pandas \u4e2d\u5982\u4f55\u5b9e\u73b0 countif \u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u60a8\u60f3\u8981\u7edf\u8ba1\u6bcf\u884c\u4e2d\u6bd4\u6307\u5b9a\u6307\u6807\u503c\u5927\u7684\u5217\u7684\u4e2a\u6570\u3002\u6b64\u95ee\u9898\u53ef\u4ee5\u8f7b\u677e\u4f7f\u7528 excel \u4e2d\u7684 countif \u51fd\u6570\u89e3\u51b3\u3002\u4f46\u662f\uff0c\u5728 pandas \u4e2d\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u50cf\u60a8\u63cf\u8ff0\u7684\u56f0\u96be\u3002<\/p>\n<p><strong>\u95ee\u9898<\/strong><\/p>\n<p>\u60a8\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>np.where(data[data.columns[1:]] &gt; data['\u6307\u6807']).count(1)\n(data[data.columns[1:]] &gt; data['\u6307\u6807']).sum()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f46\u8fd9\u4e9b\u4ee3\u7801\u8fd4\u56de 0 \u7684\u7ed3\u679c\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a<\/p>\n<ul>\n<li>np.where \u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u63a9\u7801\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u8ba1\u6570\u3002<\/li>\n<li>sum() \u4ec5\u8ba1\u7b97\u5e03\u5c14\u63a9\u7801\u4e2d\u7684 true \u7684\u6570\u91cf\uff0c\u8be5\u6570\u91cf\u59cb\u7ec8\u4e3a 0 \u6216 1\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u8981\u5b9e\u73b0 countif \u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 apply() \u65b9\u6cd5\u9010\u884c\u5e94\u7528 lambda \u51fd\u6570\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>data['countif'] = data.apply(lambda x : sum(data['X1':'X4'] &gt; data['\u6307\u6807']),axis =1)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>lambda x : sum(data[&#8216;x1&#8242;:&#8217;x4&#8217;] &gt; data[&#8216;\u6307\u6807&#8217;]) \u5b9a\u4e49\u4e86\u4e00\u4e2a lambda \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u83b7\u53d6\u4e00\u884c\u6570\u636e x \u5e76\u8ba1\u7b97\u5927\u4e8e\u6307\u6807\u503c\u7684\u5143\u7d20\u6570\u91cf\u3002<\/li>\n<li>apply() \u9010\u884c\u5e94\u7528 lambda \u51fd\u6570\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u540d\u4e3a &#8216;countif&#8217; \u7684\u65b0\u5217\u4e2d\u3002<\/li>\n<\/ul>\n<p>\u73b0\u5728\uff0cdata[&#8216;countif&#8217;] \u5217\u5305\u542b\u6bcf\u884c\u4e2d\u5927\u4e8e\u6307\u6807\u503c\u7684\u5217\u7684\u4e2a\u6570\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fPandas \u5982\u4f55\u5b9e\u73b0 COUNTIF \u51fd\u6570\u7edf\u8ba1\u6bcf\u884c\u5927\u4e8e\u6307\u6807\u503c\u7684\u5217\u6570\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 pandas \u4e2d\u5982\u4f55\u5b9e\u73b0 countif \u51fd\u6570\uff1f \u60a8\u60f3\u8981\u7edf\u8ba1\u6bcf\u884c\u4e2d\u6bd4\u6307\u5b9a\u6307\u6807\u503c\u5927\u7684\u5217\u7684\u4e2a\u6570\u3002\u6b64\u95ee\u9898\u53ef\u4ee5\u8f7b\u677e\u4f7f\u7528 excel \u4e2d\u7684 countif \u51fd\u6570\u89e3\u51b3\u3002\u4f46\u662f\uff0c\u5728 pandas \u4e2d\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u50cf\u60a8\u63cf\u8ff0\u7684\u56f0\u96be\u3002 \u95ee\u9898 \u60a8\u7684\u4ee3\u7801\u5982\u4e0b\uff1a np.where(data[data.columns[1:]] &gt; data[&#8216;\u6307\u6807&#8217;]).count(1) (data[data.columns[1:]] &gt; data[&#8216;\u6307\u6807&#8217;]).sum() \u767b\u5f55\u540e\u590d\u5236 \u4f46\u8fd9\u4e9b\u4ee3\u7801\u8fd4\u56de 0 \u7684\u7ed3\u679c\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a np.where \u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u63a9\u7801\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u8ba1\u6570\u3002 sum() \u4ec5\u8ba1\u7b97\u5e03\u5c14\u63a9\u7801\u4e2d\u7684 true \u7684\u6570\u91cf\uff0c\u8be5\u6570\u91cf\u59cb\u7ec8\u4e3a 0 \u6216 1\u3002 \u89e3\u51b3\u65b9\u6848 \u8981\u5b9e\u73b0 countif \u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 apply() \u65b9\u6cd5\u9010\u884c\u5e94\u7528 lambda \u51fd\u6570\u3002\u4ee3\u7801\u5982\u4e0b\uff1a data[&#8216;countif&#8217;] = data.apply(lambda x : sum(data[&#8216;X1&#8242;:&#8217;X4&#8217;] &gt; data[&#8216;\u6307\u6807&#8217;]),axis =1) \u767b\u5f55\u540e\u590d\u5236 lambda x : sum(data[&#8216;x1&#8242;:&#8217;x4&#8217;] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-914","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/914","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=914"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/914\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=914"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=914"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=914"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}