{"id":198,"date":"2023-12-04T21:46:14","date_gmt":"2023-12-04T13:46:14","guid":{"rendered":"https:\/\/www.daogu.work\/?p=198"},"modified":"2023-12-04T21:46:56","modified_gmt":"2023-12-04T13:46:56","slug":"198","status":"publish","type":"post","link":"https:\/\/daogu.work\/?p=198","title":{"rendered":"Python\u6570\u636e\u7c7b\u522b\u8865\u5168"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u80cc\u666f<\/h2>\n\n\n\n<p>\u4ece\u4e8b\u98ce\u63a7\u5de5\u4f5c\uff0c\u7ecf\u5e38\u8981\u7edf\u8ba1\u98ce\u9669\u7b49\u7ea7\u8fd9\u4e2a\u6807\u7b7e\uff0c\u4e5f\u53ef\u80fd\u9700\u8981\u7edf\u8ba1\u4e0d\u540c\u98ce\u9669\u7b49\u7ea7\u5728\u4e0d\u540c\u5e73\u53f0\u4e0b\u7684\u9891\u7387\uff0c\u4e5f\u5c31\u662f\u51fa\u73b0\u6b21\u6570\uff0c\u4ee5\u6b64\u6765\u4f5c\u4e3a\u7b56\u7565\u6709\u6548\u6027\u8bc4\u4f30\u7684\u4e00\u4e2a\u6307\u6807\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u5e38\u89c1\u7684\u573a\u666f\u662f\uff1a\u5f53\u4f60\u9700\u8981\u4f5c\u56fe\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u67d0\u4e00\u5929\uff0c\u6216\u8005\u4e00\u76f4\u4ee5\u6765\u90fd\u6ca1\u6709\u67d0\u4e00\u4e2a\u98ce\u9669\u7b49\u7ea7\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f5c\u56fe\u4e5f\u597d\uff0c\u4f5c\u8868\u4e5f\u597d\uff0c\u90fd\u4f1a\u51fa\u73b0\u4e00\u4e2a\u65ad\u5c42\u3002<br>\u6bd4\u5982\u753b\u51fa\u6765\u7684\u56fe\u5f62\u662f\u8fd9\u4e2a\u6837\u5b50\u7684\uff1a<\/p>\n\n\n\n<p>\u6570\u636e\u662f\u6211\u968f\u673a\u751f\u6210\u7684\uff0c\u60f3\u8868\u8fbe\u7684\u4e00\u70b9\u5c31\u662f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>WEB\u7aef\u67093\u6761\u7ebf\uff0c\u4f46\u662fAPP\u7aef\u53ea\u67092\u6761\u84dd\u8272\u7684\u7ebf\uff0cAPP\u7aef\u5c11\u4e86\u4e00\u4e2arisklevel\u4e3a1\u7684\u6570\u636e\u3002<br><img decoding=\"async\" src=\"https:\/\/img.daogu.work\/images\/2023\/12\/04\/chrome_JVAHkLjuKA.png\" alt=\"chrome_JVAHkLjuKA.png\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u95ee\u9898\u63d0\u51fa<\/h2>\n\n\n\n<p>\u5176\u5b9e\uff0c\u8fd9\u4e2a\u5728\u7ecf\u6d4e\u5b66\u4e2d\u8f83\u9762\u677f\u6570\u636e\uff0c\u65e0\u8bba\u5728\u505a\u56de\u5f52\u5206\u6790\u8fd8\u662f\u5efa\u6a21\u7684\u65f6\u5019\uff0c\u4e00\u822c\u90fd\u9700\u8981\u8865\u6210\u4e00\u4e2along\u578b\u7684\u6570\u636e\u3002<br>\u8fd9\u4e2a\u6570\u636e\uff0c\u5c31\u662f\u7c7b\u4f3c\u4e0b\u9762\u8fd9\u79cd\u5f62\u5f0f\u3002<br><code>year,variablename,value<\/code><\/p>\n<\/blockquote>\n\n\n\n<p>\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\uff0c\u5c06\u7f3a\u5931\u7684\u98ce\u9669\u7b49\u7ea7\u7ed9\u4ed6\u8865\u4e0a\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u4e2a\u6839\u636edataframe\u8fdb\u884c\u88650\u7684\u65b9\u6848\u51fd\u6570<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u610f\u70b9<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6570\u636e\u91cc\u9762\uff0c\u5220\u9664\u6389\u4f60\u4e0d\u9700\u8981\u7684\u5217\uff0c\u4fdd\u7559\u4f60\u9700\u8981\u7684\u5217\u5c31\u884c<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import pandas as pd\nfrom itertools import product\n\ndef expand_dataframe(df, expand_cols):\n    # \u7b2c\u4e00\u6b65\uff0c\u627e\u51fa\u53bb\u91cd\u540e\u7684\u552f\u4e00\u503c\n    unique_values = &#91;df&#91;col].unique() for col in expand_cols]\n    # \u7b2c\u4e8c\u6b65\uff0c\u521b\u5efa\u7ec4\u5408dataframe\n    all_combinations = pd.DataFrame(list(product(*unique_values)), columns=expand_cols)\n    # \u7b2c\u4e09\u6b65\uff0c\u4e0e\u539f\u59cb\u6570\u636e\u5408\u5e76\n    expanded_df = pd.merge(all_combinations, df, on=expand_cols, how='left')\n    # \u7b2c\u56db\u6b65\uff0c\u8865\u503c\n    expanded_df.fillna(0,inplace=True)\n    return expanded_df<\/code><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u751f\u6210\u6570\u636e\u7684\u4ee3\u7801\u548c\u5176\u4ed6\u4fe1\u606f<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n<code>import pandas as pd\nimport numpy as np\n\n# \u751f\u62101\u4e2a\u661f\u671f\u7684\u65e5\u671f\u53d8\u91cf\ndates = pd.date_range(start='2023-12-01', periods=7)\n# \u751f\u6210\u4f5c\u56fedataframe\n# \u751f\u6210\u98ce\u9669\u7b49\u7ea7\u4e3a1\uff0c2\uff0c3\uff0c\u4e14channel \u4e3aAPP\u6216\u8005WEB\u7684\u6570\u636e\uff0c\u5e76\u4e14channel\u4e3aAPP\u4e14\u98ce\u9669\u7b49\u7ea7\u4e3a1\u7684\u6570\u636e\u4e0d\u751f\u6210\ndata = &#91;]\nfor date in dates:\n    for risk_level in &#91;1,2, 3]:\n        for channel in &#91;'APP', 'WEB']:\n            if risk_level == 1 and channel=='APP':\n                continue\n            num = np.random.randint(10, 90)\n            data.append(&#91;date, risk_level, num, channel])\n\n# \u751f\u6210df\ndf = pd.DataFrame(data, columns=&#91;'date', 'risklevel', 'num',  'channel'])<\/code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code><\/code><\/pre>\n\n\n\n<p>df\u7684\u524d10\u884c\u6570\u636e\u5982\u4e0b\uff1a<br><img decoding=\"async\" src=\"https:\/\/img.daogu.work\/images\/2023\/12\/04\/chrome_C1meNHPVBC.png\" alt=\"\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f5c\u56fe\u7684\u4ee3\u7801<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import matplotlib.pyplot as plt\nimport seaborn as sns\nplt.style.use('ggplot')\nplt.figure()\nsns.lineplot(data=df,x='date',y='num',hue='channel',style='risklevel',markers=True,dashes=False,linewidth=2.5)\nplt.show()<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u4ece\u4e8b\u98ce\u63a7\u5de5\u4f5c\uff0c\u7ecf\u5e38\u8981\u7edf\u8ba1\u98ce\u9669\u7b49\u7ea7\u8fd9\u4e2a\u6807\u7b7e\uff0c\u4e5f\u53ef\u80fd\u9700\u8981\u7edf\u8ba1\u4e0d\u540c\u98ce\u9669\u7b49\u7ea7\u5728\u4e0d\u540c\u5e73\u53f0\u4e0b\u7684\u9891\u7387\uff0c\u4e5f\u5c31\u662f\u51fa\u73b0\u6b21\u6570\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[7,42,43],"class_list":["post-198","post","type-post","status-publish","format-standard","hentry","category-risk-control-talking","tag-python","tag-42","tag-43"],"_links":{"self":[{"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/posts\/198","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/daogu.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=198"}],"version-history":[{"count":2,"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions"}],"predecessor-version":[{"id":200,"href":"https:\/\/daogu.work\/index.php?rest_route=\/wp\/v2\/posts\/198\/revisions\/200"}],"wp:attachment":[{"href":"https:\/\/daogu.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/daogu.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/daogu.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}