{"id":50409,"date":"2024-12-03T11:05:19","date_gmt":"2024-12-03T03:05:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50409\/"},"modified":"2024-12-03T11:05:19","modified_gmt":"2024-12-03T03:05:19","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-pandas-%e8%8e%b7%e5%8f%96%e5%bd%93%e5%89%8d%e8%a1%8c%e5%80%bc%e4%b9%8b%e5%90%8e%e6%89%80%e6%9c%89%e6%af%94%e5%bd%93%e5%89%8d%e8%a1%8c%e5%80%bc%e5%a4%a7%e7%9a%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50409\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 Pandas \u83b7\u53d6\u5f53\u524d\u884c\u503c\u4e4b\u540e\u6240\u6709\u6bd4\u5f53\u524d\u884c\u503c\u5927\u7684\u6570\u636e\u4e2a\u6570\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 Pandas \u83b7\u53d6\u5f53\u524d\u884c\u503c\u4e4b\u540e\u6240\u6709\u6bd4\u5f53\u524d\u884c\u503c\u5927\u7684\u6570\u636e\u4e2a\u6570\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731988296673c0b48642b2.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 Pandas \u83b7\u53d6\u5f53\u524d\u884c\u503c\u4e4b\u540e\u6240\u6709\u6bd4\u5f53\u524d\u884c\u503c\u5927\u7684\u6570\u636e\u4e2a\u6570\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 Pandas \u83b7\u53d6\u5f53\u524d\u884c\u503c\u4e4b\u540e\u6240\u6709\u6bd4\u5f53\u524d\u884c\u503c\u5927\u7684\u6570\u636e\u4e2a\u6570\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 pandas \u83b7\u53d6\u6bd4\u5f53\u524d\u884c\u503c\u5927\u7684\u6570\u636e\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u83b7\u53d6\u6bd4\u5f53\u524d\u884c\u503c\u5927\u7684\u6570\u636e\u5e76\u8ba1\u7b97\u5176\u4e2a\u6570\u4f5c\u4e3a\u65b0\u5217\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528 pandas \u63d0\u4f9b\u7684\u9ad8\u6548\u77e9\u9635\u8ba1\u7b97\u529f\u80fd\u3002<\/p>\n<p>\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li><strong>\u5b9a\u4e49\u6761\u4ef6\u51fd\u6570\uff1a<\/strong>\u4f7f\u7528 lambda \u51fd\u6570\u5b9a\u4e49\u4e00\u4e2a\u6761\u4ef6\u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad\u6570\u7ec4\u4e2d\u5143\u7d20\u662f\u5426\u5927\u4e8e\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002<\/li>\n<\/ol>\n<pre>count_larger = lambda x: np.sum(x &gt; x[-1])<\/pre>\n<ol>\n<li><strong>\u5e94\u7528\u6761\u4ef6\u51fd\u6570\uff1a<\/strong>\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u904d\u5386 dataframe \u7684\u6240\u6709\u884c\uff0c\u5e94\u7528\u6761\u4ef6\u51fd\u6570\u5230 col3 \u5217\u7684\u503c\u4e0a\uff0c\u5e76\u8ba1\u7b97\u7d2f\u79ef\u548c\u3002<\/li>\n<\/ol>\n<pre>df['col4'] = [count_larger(df[:i + 1]['col3'].values) for i in range(len(df))]<\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\uff0c\u5b83\u907f\u514d\u4e86\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5faa\u73af\u67e5\u627e\uff0c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u5230 o(1)\u3002\u5b83\u7279\u522b\u9002\u5408\u4e8e\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u80fd\u591f\u4ee5\u9ad8\u6548\u7684\u65b9\u5f0f\u83b7\u5f97\u6240\u9700\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Pandas \u83b7\u53d6\u5f53\u524d\u884c\u503c\u4e4b\u540e\u6240\u6709\u6bd4\u5f53\u524d\u884c\u503c\u5927\u7684\u6570\u636e\u4e2a\u6570\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 Pandas \u83b7\u53d6\u5f53\u524d\u884c\u503c\u4e4b\u540e\u6240\u6709\u6bd4\u5f53\u524d\u884c\u503c\u5927\u7684\u6570\u636e\u4e2a\u6570\uff1f \u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9\u6587\u7ae0\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01 \u5982\u4f55\u4f7f\u7528 pandas \u83b7\u53d6\u6bd4\u5f53\u524d\u884c\u503c\u5927\u7684\u6570\u636e\uff1f \u4e3a\u4e86\u89e3\u51b3\u83b7\u53d6\u6bd4\u5f53\u524d\u884c\u503c\u5927\u7684\u6570\u636e\u5e76\u8ba1\u7b97\u5176\u4e2a\u6570\u4f5c\u4e3a\u65b0\u5217\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528 pandas \u63d0\u4f9b\u7684\u9ad8\u6548\u77e9\u9635\u8ba1\u7b97\u529f\u80fd\u3002 \u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a \u5b9a\u4e49\u6761\u4ef6\u51fd\u6570\uff1a\u4f7f\u7528 lambda \u51fd\u6570\u5b9a\u4e49\u4e00\u4e2a\u6761\u4ef6\u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad\u6570\u7ec4\u4e2d\u5143\u7d20\u662f\u5426\u5927\u4e8e\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u3002 count_larger = lambda x: np.sum(x &gt; x[-1]) \u5e94\u7528\u6761\u4ef6\u51fd\u6570\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u904d\u5386 dataframe \u7684\u6240\u6709\u884c\uff0c\u5e94\u7528\u6761\u4ef6\u51fd\u6570\u5230 col3 \u5217\u7684\u503c\u4e0a\uff0c\u5e76\u8ba1\u7b97\u7d2f\u79ef\u548c\u3002 df[&#8216;col4&#8217;] = [count_larger(df[:i + 1][&#8216;col3&#8217;].values) for i in range(len(df))] \u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u5728\u4e8e\uff0c\u5b83\u907f\u514d\u4e86\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5faa\u73af\u67e5\u627e\uff0c\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4f4e\u5230 o(1)\u3002\u5b83\u7279\u522b\u9002\u5408\u4e8e\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u80fd\u591f\u4ee5\u9ad8\u6548\u7684\u65b9\u5f0f\u83b7\u5f97\u6240\u9700\u7684\u7ed3\u679c\u3002 \u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 Pandas \u83b7\u53d6\u5f53\u524d\u884c\u503c\u4e4b\u540e\u6240\u6709\u6bd4\u5f53\u524d\u884c\u503c\u5927\u7684\u6570\u636e\u4e2a\u6570\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/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-50409","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50409","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=50409"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50409\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}