{"id":158815,"date":"2020-09-16T11:57:03","date_gmt":"2020-09-16T03:57:03","guid":{"rendered":"http:\/\/4563.org\/?p=158815"},"modified":"2020-09-16T11:57:03","modified_gmt":"2020-09-16T03:57:03","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-pandas-%e4%bc%98%e9%9b%85%e6%8b%bc%e8%a3%85-k-%e7%ba%bf%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=158815","title":{"rendered":"\u5982\u4f55\u7528 pandas \u4f18\u96c5\u62fc\u88c5 k \u7ebf\u6570\u636e"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5982\u4f55\u7528 pandas \u4f18\u96c5\u62fc\u88c5 k \u7ebf\u6570\u636e               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yellowtail <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u65b9\u6cd5\u4e00\u4f7f\u7528 resample \u6309\u7167\u7f51\u4e0a\u6848\u4f8b\u5199\u5f97\uff0c\u5f88\u7b80\u77ed\uff0c\u4e0d\u8fc7 apply \u7ed9\u5b57\u5178\u53c2\u6570\u7684\u64cd\u4f5c\u6ca1\u6709\u770b\u61c2\u3002\u800c\u4e14\u53ef\u80fd\u662f\u56e0\u4e3a\u4e2d\u5348\u4f11\u5e02\u7684\u539f\u56e0\uff0c\u8d85\u8fc7\u516d\u5341\u5206\u949f\u4ee5\u4e0a\uff0c\u53d6\u6837\u4f1a\u51fa\u95ee\u9898\uff0c\u4e00\u5929\u4f1a\u51fa\u73b0\u4e94\u6839\u4e00\u5c0f\u65f6 k \u7ebf\uff08\u4e00\u5929\u56db\u4e2a\u4ea4\u6613\u65f6\uff09 \u5199\u6cd5\u4e3a ohlc_dict ={ &#8216;o&#8217;:&#8217;first&#8217;,<br \/> &#8216;h&#8217;:&#8217;max&#8217;,<br \/> &#8216;l&#8217;:&#8217;min&#8217;,<br \/> &#8216;c&#8217;: &#8216;last&#8217;<br \/> } dft = dft.resample(period, closed=&#8217;right&#8217;,label = &#8216;right&#8217;).apply(ohlc_dict).dropna()<\/p>\n<p>\u65b9\u6cd5\u4e8c\u662f\u7528 cut dfx =pd.DataFrame()<\/p>\n<p>df[&#8216;tst&#8217;] = pd.cut(df.index,right=False,bins=range(len(df))[::120])<\/p>\n<p>dfx[&#8216;o&#8217;]=df.groupby(&#8216;tst&#8217;)[&#8216;o&#8217;].first()<\/p>\n<p>dfx[&#8216;c&#8217;]=df.groupby(&#8216;tst&#8217;)[&#8216;c&#8217;].last()<\/p>\n<p>dfx[&#8216;h&#8217;]=df.groupby(&#8216;tst&#8217;)[&#8216;h&#8217;].max()<\/p>\n<p>dfx[&#8216;l&#8217;]=df.groupby(&#8216;tst&#8217;)[&#8216;l&#8217;].min()<\/p>\n<p>dfx[&#8216;trade_date&#8217;]=df.groupby(&#8216;tst&#8217;)[&#8216;trade_date&#8217;].last() \u611f\u89c9\u592a\u4e11\u4e86&#8230;\u5e0c\u671b\u80fd\u7ed9\u70b9\u4f18\u5316\u610f\u89c1<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3407648\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yellowtail <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/imgchr.com\/i\/wTGMOf resample \u65b9\u6cd5 60 \u5206\u949f\u4ee5\u4e0a\u7684\u9519\u8bef                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3407649\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : volvo007 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             resample \u51fd\u6570\u63a5\u53d7\u5b57\u5178\u53c2\u6570\uff0ckey \u4e3a\u5217\u540d\u5e76\u4f53\u73b0\u5728\u8fd4\u56de\u7684 df \u91cc\uff0cvalue \u4e3a\u5bf9\u5e94\u5217\u9700\u8981\u6267\u884c\u7684\u51fd\u6570<\/p>\n<p>dict of axis labels -&gt; functions, function names or list of such.<\/p>\n<p>https:\/\/pandas.pydata.org\/pandas-docs\/stable\/reference\/api\/pandas.core.resample.Resampler.apply.html#pandas.core.resample.Resampler.apply                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528 pandas \u4f18\u96c5\u62fc\u88c5 k&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/158815"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=158815"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/158815\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=158815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=158815"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=158815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}