{"id":3124,"date":"2017-11-14T20:36:00","date_gmt":"2017-11-14T12:36:00","guid":{"rendered":"https:\/\/shanlin.info\/?p=3124"},"modified":"2021-04-25T20:37:36","modified_gmt":"2021-04-25T12:37:36","slug":"pandas%e9%87%8c%e7%9a%84%e6%97%a5%e6%9c%9f%e8%bd%ac%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/shanlin.info\/?p=3124","title":{"rendered":"Pandas\u91cc\u7684\u65e5\u671f\u8f6c\u5b57\u7b26\u4e32"},"content":{"rendered":"\n<p>Pandas\u5bf9\u65f6\u95f4\u5e8f\u5217\u7684\u5904\u7406\u53ef\u4ee5\u8bf4\u662f\u529f\u80fd\u7e41\u591a\uff0c\u4f46\u611f\u89c9\u627e\u904d\u4e86\u5ea6\u5a18\uff0c\u4e5f\u6ca1\u6709\u53d1\u73b0\u4e00\u4e2a\u80fd\u628a\u65e5\u671f\u5904\u7406\u6210\u201c2017-11\u201d\u8fd9\u6837\u5230\u6708\u4efd\u683c\u5f0f\u7684\u65b9\u6cd5\u3002\u540e\u6765\u53d1\u73b0\u4e86\u4e00\u4e2astrftime\u65b9\u6cd5\uff0c\u5f88\u7b80\u5355\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df&#91;'time'].strftime('%Y-%m')\n\n#\u4ee5\u4e0b\u662f\u683c\u5f0f\u5b9a\u4e49\n\u4ee3\u7801  \u8bf4\u660e\n%Y  4\u4f4d\u6570\u7684\u5e74\n%y  2\u4f4d\u6570\u7684\u5e74\n%m  2\u4f4d\u6570\u7684\u6708&#91;01,12]\n%d  2\u4f4d\u6570\u7684\u65e5&#91;01\uff0c31]\n%H  \u65f6\uff0824\u5c0f\u65f6\u5236\uff09&#91;00,23]\n%l  \u65f6\uff0812\u5c0f\u65f6\u5236\uff09&#91;01,12]\n%M  2\u4f4d\u6570\u7684\u5206&#91;00,59]\n%S  \u79d2&#91;00,61]\u6709\u95f0\u79d2\u7684\u5b58\u5728\n%w  \u7528\u6574\u6570\u8868\u793a\u7684\u661f\u671f\u51e0&#91;0\uff08\u661f\u671f\u5929\uff09\uff0c6]\n%F  %Y-%m-%d\u7b80\u5199\u5f62\u5f0f\u4f8b\u5982\uff0c2017-06-27\n%D  %m\/%d\/%y\u7b80\u5199\u5f62\u5f0f\n<\/code><\/pre>\n\n\n\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>strtime=&#91;'2000-01-31', '2000-02-29', '2000-03-31', '2000-04-30',\n               '2000-05-31', '2000-06-30', '2000-07-31', '2000-08-31',\n               '2000-09-30', '2000-10-31']\nindex=pd.to_datetime(strtime)\ntime=pd.to_datetime(strtime)\ndata=list(range(1,11))\ndf=pd.DataFrame(&#91;time,data],columns=index,index=&#91;'time','data']).T\n\n#\u7d22\u5f15\u548ctime\u5b57\u6bb5\u5747\u4e3aTimestamp\u683c\u5f0f\u6570\u636e\ntype(index&#91;0])\nOut&#91;59]: pandas.tslib.Timestamp\ntype(df.index&#91;0])\nOut&#91;60]: pandas.tslib.Timestamp\ntype(df.time&#91;0])\nOut&#91;61]: pandas.tslib.Timestamp\n\n#\u5148\u6765\u770b\u7d22\u5f15\nmt_time=df.index.strftime('%Y-%m')\nmt_time\nOut&#91;64]: \narray(&#91;'2000-01', '2000-02', '2000-03', '2000-04', '2000-05', '2000-06',\n       '2000-07', '2000-08', '2000-09', '2000-10'], \n      dtype='&lt;U7')\ntype(mt_time&#91;0])\nOut&#91;65]: numpy.str_\n\n#\u9488\u5bf9\u5217\uff08\u76f4\u63a5\u64cd\u4f5c\u8fd8\u4e0d\u6e05\u695a\u600e\u4e48\u64cd\u4f5c\uff09\ntime_list=&#91;x.strftime('%Y-%m') for x in df&#91;'time']]\ntime_list\nOut&#91;76]: \n&#91;'2000-01',\n '2000-02',\n '2000-03',\n '2000-04',\n '2000-05',\n '2000-06',\n '2000-07',\n '2000-08',\n '2000-09',\n '2000-10']\n\n#\u518d\u6309\u6708\u4efd\u6c42\u548c\ndf&#91;'data'].groupby(time_list).sum()\nOut&#91;77]: \n2000-01     1\n2000-02     2\n2000-03     3\n2000-04     4\n2000-05     5\n2000-06     6\n2000-07     7\n2000-08     8\n2000-09     9\n2000-10    10\nName: data, dtype: int64\n\n<\/code><\/pre>\n\n\n\n<p>\u6709\u7684\u65f6\u5019\uff0c\u771f\u7684\u662f\u4e3a\u4e86\u6240\u8c13\u7684\u806a\u660e\uff08\u627e\u5230\u6700\u4f18\u96c5\u7684\u65b9\u6cd5\u8ba1\u7b97\u7ed3\u679c\uff09\u7684\u529e\u6cd5\u7ede\u5c3d\u8111\u6c41\uff08\u6bd4\u5982\u5728groupby\u7684\u65f6\u5019\u600e\u4e48\u76f4\u63a5\u7528strftime\u5904\u7406\uff09\uff0c\u800c\u653e\u5f03\u4e86\u53ef\u66ff\u4ee3\u7684\u4e11\u964b\u7684\u529e\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pandas\u5bf9\u65f6\u95f4\u5e8f\u5217\u7684\u5904\u7406\u53ef\u4ee5\u8bf4\u662f\u529f\u80fd\u7e41\u591a\uff0c\u4f46\u611f\u89c9\u627e\u904d\u4e86\u5ea6\u5a18\uff0c\u4e5f\u6ca1\u6709\u53d1\u73b0\u4e00\u4e2a\u80fd\u628a\u65e5\u671f\u5904\u7406\u6210\u201c2017-11\u201d<\/p><\/div>\n<div class=\"blog-btn\"><a href=\"https:\/\/shanlin.info\/?p=3124\" class=\"home-blog-btn\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/3124"}],"collection":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3124"}],"version-history":[{"count":1,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/3124\/revisions"}],"predecessor-version":[{"id":3125,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/3124\/revisions\/3125"}],"wp:attachment":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}