{"id":840,"date":"2024-11-07T14:16:23","date_gmt":"2024-11-07T06:16:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/840\/"},"modified":"2024-11-07T14:16:23","modified_gmt":"2024-11-07T06:16:23","slug":"%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8b-pandas-dataframe-%e4%b8%ad%e6%98%af%e5%90%a6%e5%ad%98%e5%9c%a8%e9%97%b4%e9%9a%94%e4%b8%a4%e4%b8%aa%e6%9c%88%e4%bb%a5%e4%b8%8a%e7%9a%84%e8%ae%b0%e5%bd%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/840\/","title":{"rendered":"\u5982\u4f55\u68c0\u6d4b pandas DataFrame \u4e2d\u662f\u5426\u5b58\u5728\u95f4\u9694\u4e24\u4e2a\u6708\u4ee5\u4e0a\u7684\u8bb0\u5f55\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1068\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172999261386238.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172999261386238.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172999261386238-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172999261386238-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172999261386238-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"\u5982\u4f55\u68c0\u6d4b pandas DataFrame \u4e2d\u662f\u5426\u5b58\u5728\u95f4\u9694\u4e24\u4e2a\u6708\u4ee5\u4e0a\u7684\u8bb0\u5f55\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u68c0\u6d4b pandas DataFrame \u4e2d\u662f\u5426\u5b58\u5728\u95f4\u9694\u4e24\u4e2a\u6708\u4ee5\u4e0a\u7684\u8bb0\u5f55\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u68c0\u6d4b pandas dataframe \u4e2d\u95f4\u9694\u4e24\u4e2a\u6708\u4ee5\u4e0a\u7684\u8bb0\u5f55<\/strong><\/p>\n<p>\u5728\u5904\u7406\u65f6\u5e8f\u6570\u636e\u65f6\uff0c\u5224\u65ad\u6570\u636e\u4e4b\u95f4\u7684\u65e5\u671f\u95f4\u9694\u53ef\u80fd\u81f3\u5173\u91cd\u8981\u3002\u6bd4\u5982\uff0c\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u4fe1\u606f\u7684 dataframe\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>name<\/th>\n<th>date<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>foo<\/td>\n<td>2022-01-01<\/td>\n<\/tr>\n<tr>\n<td>foo<\/td>\n<td>2022-01-23<\/td>\n<\/tr>\n<tr>\n<td>foo<\/td>\n<td>2022-03-01<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u60f3\u77e5\u9053 dataframe \u4e2d\u662f\u5426\u6709\u95f4\u9694\u4e24\u4e2a\u6708\u4ee5\u4e0a\u7684\u8bb0\u5f55\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u8ba1\u7b97\u76f8\u90bb\u8bb0\u5f55\u4e4b\u95f4\u7684\u5929\u6570\u5dee\uff1a<\/strong><\/li>\n<\/ol>\n<pre>df['day'] = (df.date.shift(-1) - df.date).dt.days<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u5728 dataframe \u4e2d\u4e3a\u6bcf\u4e00\u884c\u521b\u5efa\u4e00\u4e2a\u201cday\u201d\u5217\uff0c\u5305\u542b\u4e0e\u524d\u4e00\u884c\u65e5\u671f\u7684\u5dee\u503c\uff08\u4ee5\u5929\u4e3a\u5355\u4f4d\uff09\u3002<\/p>\n<ol>\n<li><strong>\u5224\u65ad\u662f\u5426\u5b58\u5728\u95f4\u9694\u8d85\u8fc7\u4e24\u4e2a\u6708\uff0860 \u5929\uff09\u7684\u8bb0\u5f55\uff1a<\/strong><\/li>\n<\/ol>\n<pre>(df.day &gt; 60).any()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a dataframe \u4e2d\u662f\u5426\u5b58\u5728\u4efb\u4f55\u95f4\u9694\u8d85\u8fc7 60 \u5929\u7684\u8bb0\u5f55\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u68c0\u6d4b pandas DataFrame \u4e2d\u662f\u5426\u5b58\u5728\u95f4\u9694\u4e24\u4e2a\u6708\u4ee5\u4e0a\u7684\u8bb0\u5f55\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>\u5982\u4f55\u68c0\u6d4b pandas dataframe \u4e2d\u95f4\u9694\u4e24\u4e2a\u6708\u4ee5\u4e0a\u7684\u8bb0\u5f55 \u5728\u5904\u7406\u65f6\u5e8f\u6570\u636e\u65f6\uff0c\u5224\u65ad\u6570\u636e\u4e4b\u95f4\u7684\u65e5\u671f\u95f4\u9694\u53ef\u80fd\u81f3\u5173\u91cd\u8981\u3002\u6bd4\u5982\uff0c\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u4fe1\u606f\u7684 dataframe\uff1a name date foo 2022-01-01 foo 2022-01-23 foo 2022-03-01 \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u60f3\u77e5\u9053 dataframe \u4e2d\u662f\u5426\u6709\u95f4\u9694\u4e24\u4e2a\u6708\u4ee5\u4e0a\u7684\u8bb0\u5f55\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u8ba1\u7b97\u76f8\u90bb\u8bb0\u5f55\u4e4b\u95f4\u7684\u5929\u6570\u5dee\uff1a df[&#8216;day&#8217;] = (df.date.shift(-1) &#8211; df.date).dt.days \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u5728 dataframe \u4e2d\u4e3a\u6bcf\u4e00\u884c\u521b\u5efa\u4e00\u4e2a\u201cday\u201d\u5217\uff0c\u5305\u542b\u4e0e\u524d\u4e00\u884c\u65e5\u671f\u7684\u5dee\u503c\uff08\u4ee5\u5929\u4e3a\u5355\u4f4d\uff09\u3002 \u5224\u65ad\u662f\u5426\u5b58\u5728\u95f4\u9694\u8d85\u8fc7\u4e24\u4e2a\u6708\uff0860 \u5929\uff09\u7684\u8bb0\u5f55\uff1a (df.day &gt; 60).any() \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u6307\u793a dataframe \u4e2d\u662f\u5426\u5b58\u5728\u4efb\u4f55\u95f4\u9694\u8d85\u8fc7 60 \u5929\u7684\u8bb0\u5f55\u3002 \u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u68c0\u6d4b pandas DataFrame \u4e2d\u662f\u5426\u5b58\u5728\u95f4\u9694\u4e24\u4e2a\u6708\u4ee5\u4e0a\u7684\u8bb0\u5f55\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},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-840","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/840","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=840"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/840\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}