{"id":1037834,"date":"2024-12-31T12:18:30","date_gmt":"2024-12-31T04:18:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1037834.html"},"modified":"2024-12-31T12:18:33","modified_gmt":"2024-12-31T04:18:33","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e7%bb%99%e6%95%b0%e6%8d%ae%e5%88%86%e5%8c%ba%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1037834.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e2d\u7ed9\u6570\u636e\u5206\u533a\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/30b24fd7-fd1c-4871-8be4-3b6f27a7dfbb.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5728python\u4e2d\u7ed9\u6570\u636e\u5206\u533a\u95f4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7ed9\u6570\u636e\u5206\u533a\u95f4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528pandas.cut\u51fd\u6570\u3001\u4f7f\u7528numpy.digitize\u51fd\u6570\u3001\u4f7f\u7528\u624b\u52a8\u65b9\u6cd5\u521b\u5efa\u5206\u533a\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528pandas.cut\u51fd\u6570<\/strong> \u662f\u6700\u5e38\u7528\u4e14\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002pandas.cut\u51fd\u6570\u5141\u8bb8\u4f60\u5c06\u4e00\u7ef4\u6570\u7ec4\u6216Series\u6839\u636e\u6307\u5b9a\u7684\u5206\u533a\u8fb9\u754c\u5206\u6210\u51e0\u4e2a\u533a\u95f4\uff0c\u5e76\u5c06\u6bcf\u4e2a\u503c\u5206\u914d\u5230\u76f8\u5e94\u7684\u533a\u95f4\u4e2d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas.cut\u51fd\u6570\u8be6\u89e3<\/strong>\uff1a<\/p>\n<p>pandas.cut\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7279\u522b\u9002\u5408\u4e8e\u5c06\u8fde\u7eed\u6570\u636e\u5206\u6210\u79bb\u6563\u7684\u533a\u95f4\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u6570\u7ec4\u6216Series\uff0c\u4ee5\u53ca\u4e00\u4e2a\u5206\u533a\u8fb9\u754c\u5217\u8868\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u5206\u7c7b\u53d8\u91cf\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u81ea\u52a8\u751f\u6210\u7b49\u5bbd\u533a\u95f4\uff0c\u6216\u8005\u81ea\u5b9a\u4e49\u533a\u95f4\u8fb9\u754c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528pandas.cut\u51fd\u6570<\/p>\n<\/p>\n<p><p>pandas.cut\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7279\u522b\u9002\u5408\u4e8e\u5c06\u8fde\u7eed\u6570\u636e\u5206\u6210\u79bb\u6563\u7684\u533a\u95f4\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u6570\u7ec4\u6216Series\uff0c\u4ee5\u53ca\u4e00\u4e2a\u5206\u533a\u8fb9\u754c\u5217\u8868\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u5206\u7c7b\u53d8\u91cf\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u81ea\u52a8\u751f\u6210\u7b49\u5bbd\u533a\u95f4\uff0c\u6216\u8005\u81ea\u5b9a\u4e49\u533a\u95f4\u8fb9\u754c\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = [1, 7, 5, 4, 6, 8, 10, 15, 20, 25]<\/p>\n<p>bins = [0, 5, 10, 15, 20, 25]<\/p>\n<p>labels = [&#39;0-5&#39;, &#39;6-10&#39;, &#39;11-15&#39;, &#39;16-20&#39;, &#39;21-25&#39;]<\/p>\n<h2><strong>\u4f7f\u7528cut\u51fd\u6570\u8fdb\u884c\u5206\u533a<\/strong><\/h2>\n<p>categorized_data = pd.cut(data, bins=bins, labels=labels)<\/p>\n<p>print(categorized_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86pandas\u5e93\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u7ec4\u6570\u636e\u548c\u5206\u533a\u8fb9\u754c\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528cut\u51fd\u6570\u5c06\u6570\u636e\u5206\u533a\uff0c\u5e76\u5c06\u6bcf\u4e2a\u503c\u5206\u914d\u5230\u76f8\u5e94\u7684\u533a\u95f4\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u5206\u7c7b\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528numpy.digitize\u51fd\u6570<\/p>\n<\/p>\n<p><p>numpy.digitize\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u6570\u636e\u5206\u533a\u3002\u4e0epandas.cut\u7c7b\u4f3c\uff0c\u5b83\u5c06\u6570\u636e\u5206\u914d\u5230\u6307\u5b9a\u7684\u533a\u95f4\u4e2d\u3002\u4e0d\u540c\u7684\u662f\uff0cnumpy.digitize\u51fd\u6570\u8fd4\u56de\u7684\u662f\u6bcf\u4e2a\u503c\u6240\u5728\u533a\u95f4\u7684\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = [1, 7, 5, 4, 6, 8, 10, 15, 20, 25]<\/p>\n<p>bins = [0, 5, 10, 15, 20, 25]<\/p>\n<h2><strong>\u4f7f\u7528digitize\u51fd\u6570\u8fdb\u884c\u5206\u533a<\/strong><\/h2>\n<p>categorized_data = np.digitize(data, bins)<\/p>\n<p>print(categorized_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5bfc\u5165\u4e86numpy\u5e93\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u7ec4\u6570\u636e\u548c\u5206\u533a\u8fb9\u754c\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528digitize\u51fd\u6570\u5c06\u6570\u636e\u5206\u533a\uff0c\u5e76\u5c06\u6bcf\u4e2a\u503c\u5206\u914d\u5230\u76f8\u5e94\u7684\u533a\u95f4\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u5206\u7c7b\u540e\u7684\u6570\u636e\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u624b\u52a8\u65b9\u6cd5\u521b\u5efa\u5206\u533a<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u8981\u66f4\u7075\u6d3b\u5730\u63a7\u5236\u5206\u533a\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u624b\u52a8\u7f16\u5199\u4ee3\u7801\u8fdb\u884c\u5206\u533a\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4e8e\u4e00\u4e9b\u7279\u6b8a\u7684\u5206\u533a\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">data = [1, 7, 5, 4, 6, 8, 10, 15, 20, 25]<\/p>\n<p>bins = [0, 5, 10, 15, 20, 25]<\/p>\n<p>labels = [&#39;0-5&#39;, &#39;6-10&#39;, &#39;11-15&#39;, &#39;16-20&#39;, &#39;21-25&#39;]<\/p>\n<p>def manual_cut(data, bins, labels):<\/p>\n<p>    categorized_data = []<\/p>\n<p>    for value in data:<\/p>\n<p>        for i in range(len(bins) - 1):<\/p>\n<p>            if bins[i] &lt; value &lt;= bins[i + 1]:<\/p>\n<p>                categorized_data.append(labels[i])<\/p>\n<p>                break<\/p>\n<p>    return categorized_data<\/p>\n<p>categorized_data = manual_cut(data, bins, labels)<\/p>\n<p>print(categorized_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u7ec4\u6570\u636e\u3001\u5206\u533a\u8fb9\u754c\u548c\u6807\u7b7e\u3002\u63a5\u7740\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u51fd\u6570manual_cut\uff0c\u7528\u4e8e\u5c06\u6570\u636e\u5206\u914d\u5230\u76f8\u5e94\u7684\u533a\u95f4\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u8c03\u7528\u8be5\u51fd\u6570\u5e76\u6253\u5370\u5206\u7c7b\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408\u4f7f\u7528pandas\u4e0enumpy\u8fdb\u884c\u9ad8\u7ea7\u5206\u533a<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u4f7f\u7528pandas\u548cnumpy\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5206\u533a\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6839\u636e\u67d0\u4e9b\u7279\u5b9a\u6761\u4ef6\u52a8\u6001\u751f\u6210\u5206\u533a\u8fb9\u754c\uff0c\u7136\u540e\u4f7f\u7528\u8fd9\u4e9b\u8fb9\u754c\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u533a\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<p>data = pd.Series([1, 7, 5, 4, 6, 8, 10, 15, 20, 25])<\/p>\n<h2><strong>\u52a8\u6001\u751f\u6210\u5206\u533a\u8fb9\u754c<\/strong><\/h2>\n<p>bin_edges = np.histogram_bin_edges(data, bins=&#39;auto&#39;)<\/p>\n<h2><strong>\u4f7f\u7528cut\u51fd\u6570\u8fdb\u884c\u5206\u533a<\/strong><\/h2>\n<p>categorized_data = pd.cut(data, bins=bin_edges)<\/p>\n<p>print(categorized_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86pandas\u548cnumpy\u5e93\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u7ec4\u6570\u636e\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528numpy\u7684histogram_bin_edges\u51fd\u6570\u52a8\u6001\u751f\u6210\u5206\u533a\u8fb9\u754c\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528pandas\u7684cut\u51fd\u6570\u5c06\u6570\u636e\u5206\u533a\uff0c\u5e76\u6253\u5370\u5206\u7c7b\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u5206\u533a<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u9884\u5b9a\u4e49\u7684\u5206\u533a\u65b9\u6cd5\u53ef\u80fd\u4e0d\u80fd\u6ee1\u8db3\u6240\u6709\u9700\u6c42\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u8fdb\u884c\u5206\u533a\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u6839\u636e\u67d0\u4e2a\u590d\u6742\u7684\u903b\u8f91\u6761\u4ef6\u6765\u51b3\u5b9a\u6bcf\u4e2a\u6570\u636e\u70b9\u7684\u5206\u533a\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.Series([1, 7, 5, 4, 6, 8, 10, 15, 20, 25])<\/p>\n<p>def custom_bin(value):<\/p>\n<p>    if value &lt;= 5:<\/p>\n<p>        return &#39;0-5&#39;<\/p>\n<p>    elif value &lt;= 10:<\/p>\n<p>        return &#39;6-10&#39;<\/p>\n<p>    elif value &lt;= 15:<\/p>\n<p>        return &#39;11-15&#39;<\/p>\n<p>    elif value &lt;= 20:<\/p>\n<p>        return &#39;16-20&#39;<\/p>\n<p>    else:<\/p>\n<p>        return &#39;21-25&#39;<\/p>\n<h2><strong>\u4f7f\u7528apply\u65b9\u6cd5\u8fdb\u884c\u5206\u533a<\/strong><\/h2>\n<p>categorized_data = data.apply(custom_bin)<\/p>\n<p>print(categorized_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u7ec4\u6570\u636e\u3002\u63a5\u7740\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570custom_bin\uff0c\u7528\u4e8e\u6839\u636e\u67d0\u4e2a\u590d\u6742\u7684\u903b\u8f91\u6761\u4ef6\u6765\u51b3\u5b9a\u6bcf\u4e2a\u6570\u636e\u70b9\u7684\u5206\u533a\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528pandas\u7684apply\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u533a\uff0c\u5e76\u6253\u5370\u5206\u7c7b\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528pandas.qcut\u8fdb\u884c\u5206\u4f4d\u6570\u5206\u533a<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u6570\u636e\u7684\u5206\u4f4d\u6570\u6765\u8fdb\u884c\u5206\u533a\u3002pandas.qcut\u51fd\u6570\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6210\u6307\u5b9a\u6570\u91cf\u7684\u5206\u4f4d\u6570\u533a\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.Series([1, 7, 5, 4, 6, 8, 10, 15, 20, 25])<\/p>\n<h2><strong>\u4f7f\u7528qcut\u51fd\u6570\u8fdb\u884c\u5206\u4f4d\u6570\u5206\u533a<\/strong><\/h2>\n<p>categorized_data = pd.qcut(data, q=4, labels=[&#39;Q1&#39;, &#39;Q2&#39;, &#39;Q3&#39;, &#39;Q4&#39;])<\/p>\n<p>print(categorized_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u7ec4\u6570\u636e\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528pandas\u7684qcut\u51fd\u6570\u5c06\u6570\u636e\u5206\u6210\u56db\u4e2a\u5206\u4f4d\u6570\u533a\u95f4\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u533a\u95f4\u5206\u914d\u6807\u7b7e\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u5206\u7c7b\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5904\u7406\u7f3a\u5931\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u4e2d\u53ef\u80fd\u5305\u542b\u7f3a\u5931\u503c\u3002\u5728\u8fdb\u884c\u5206\u533a\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u8fd9\u4e9b\u7f3a\u5931\u503c\uff0c\u4ee5\u907f\u514d\u5728\u5206\u533a\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = pd.Series([1, 7, 5, 4, 6, 8, 10, 15, 20, 25, None])<\/p>\n<p>bins = [0, 5, 10, 15, 20, 25]<\/p>\n<p>labels = [&#39;0-5&#39;, &#39;6-10&#39;, &#39;11-15&#39;, &#39;16-20&#39;, &#39;21-25&#39;]<\/p>\n<h2><strong>\u4f7f\u7528cut\u51fd\u6570\u8fdb\u884c\u5206\u533a\uff0c\u5e76\u5904\u7406\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>categorized_data = pd.cut(data, bins=bins, labels=labels, include_lowest=True)<\/p>\n<p>print(categorized_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u7ec4\u5305\u542b\u7f3a\u5931\u503c\u7684\u6570\u636e\u548c\u5206\u533a\u8fb9\u754c\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528pandas\u7684cut\u51fd\u6570\u5c06\u6570\u636e\u5206\u533a\uff0c\u5e76\u901a\u8fc7include_lowest\u53c2\u6570\u5904\u7406\u7f3a\u5931\u503c\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u5206\u7c7b\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u53ef\u89c6\u5316\u5206\u533a\u7ed3\u679c<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5206\u533a\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53ef\u89c6\u5316\u5de5\u5177\u5bf9\u5206\u533a\u7ed3\u679c\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u4f8b\u5982\uff0c\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u76f4\u65b9\u56fe\u6216\u7bb1\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>data = pd.Series([1, 7, 5, 4, 6, 8, 10, 15, 20, 25])<\/p>\n<p>bins = [0, 5, 10, 15, 20, 25]<\/p>\n<h2><strong>\u4f7f\u7528cut\u51fd\u6570\u8fdb\u884c\u5206\u533a<\/strong><\/h2>\n<p>categorized_data = pd.cut(data, bins=bins)<\/p>\n<h2><strong>\u7ed8\u5236\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>categorized_data.value_counts().plot(kind=&#39;bar&#39;)<\/p>\n<p>plt.xlabel(&#39;\u533a\u95f4&#39;)<\/p>\n<p>plt.ylabel(&#39;\u9891\u6570&#39;)<\/p>\n<p>plt.title(&#39;\u6570\u636e\u5206\u533a\u7ed3\u679c&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u7ec4\u6570\u636e\u548c\u5206\u533a\u8fb9\u754c\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528pandas\u7684cut\u51fd\u6570\u5c06\u6570\u636e\u5206\u533a\uff0c\u5e76\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u76f4\u65b9\u56fe\u3002\u6700\u540e\uff0c\u6211\u4eec\u663e\u793a\u5206\u533a\u7ed3\u679c\u7684\u76f4\u65b9\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u7ed9\u6570\u636e\u5206\u533a\u95f4\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u4ece\u6700\u5e38\u7528\u7684pandas.cut\u51fd\u6570\u5230\u9ad8\u7ea7\u7684\u81ea\u5b9a\u4e49\u51fd\u6570\u548c\u53ef\u89c6\u5316\u65b9\u6cd5\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u636e\u5206\u533a\u95f4\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u6570\u636e\u5206\u533a\u95f4\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e2d\u7684<code>cut<\/code>\u548c<code>qcut<\/code>\u51fd\u6570\u3002<code>cut<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u6570\u636e\u5206\u6210\u56fa\u5b9a\u5927\u5c0f\u7684\u533a\u95f4\uff0c\u800c<code>qcut<\/code>\u5219\u662f\u6839\u636e\u6837\u672c\u7684\u5206\u4f4d\u6570\u8fdb\u884c\u5206\u533a\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u6839\u636e\u9700\u6c42\u5212\u5206\u6570\u636e\u3002<\/p>\n<p><strong>\u4f7f\u7528<code>pandas<\/code>\u5e93\u8fdb\u884c\u6570\u636e\u5206\u533a\u95f4\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>\u7528\u6237\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u4f7f\u7528<code>pandas<\/code>\u5e93\u8fdb\u884c\u6570\u636e\u5206\u533a\u95f4\uff1a<\/p>\n<ol>\n<li>\u5bfc\u5165<code>pandas<\/code>\u5e93\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684<code>DataFrame<\/code>\u6216<code>Series<\/code>\u3002<\/li>\n<li>\u4f7f\u7528<code>pd.cut()<\/code>\u6216<code>pd.qcut()<\/code>\u51fd\u6570\uff0c\u6307\u5b9a\u5206\u533a\u7684\u8fb9\u754c\u6216\u5206\u4f4d\u6570\u3002<\/li>\n<li>\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u8bbe\u7f6e\u6807\u7b7e\uff0c\u4ece\u800c\u66f4\u597d\u5730\u7406\u89e3\u6bcf\u4e2a\u533a\u95f4\u4ee3\u8868\u7684\u542b\u4e49\u3002<\/li>\n<\/ol>\n<p><strong>\u5728\u6570\u636e\u5206\u533a\u95f4\u540e\uff0c\u5982\u4f55\u5206\u6790\u6bcf\u4e2a\u533a\u95f4\u7684\u6570\u636e\uff1f<\/strong><\/p>\n<p>\u5728\u6570\u636e\u5206\u533a\u95f4\u540e\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528<code>groupby<\/code>\u51fd\u6570\u5bf9\u5206\u533a\u540e\u7684\u6570\u636e\u8fdb\u884c\u8fdb\u4e00\u6b65\u5206\u6790\u3002\u901a\u8fc7\u5206\u7ec4\uff0c\u53ef\u4ee5\u8ba1\u7b97\u6bcf\u4e2a\u533a\u95f4\u7684\u7edf\u8ba1\u91cf\uff0c\u5982\u5747\u503c\u3001\u6807\u51c6\u5dee\u3001\u8ba1\u6570\u7b49\u3002\u8fd9\u5c06\u5e2e\u52a9\u7528\u6237\u6d1e\u5bdf\u6570\u636e\u7684\u5206\u5e03\u7279\u5f81\u548c\u8d8b\u52bf\u3002\u540c\u65f6\uff0c\u7528\u6237\u8fd8\u53ef\u4ee5\u53ef\u89c6\u5316\u8fd9\u4e9b\u7ed3\u679c\uff0c\u4f7f\u7528<code>matplotlib<\/code>\u6216<code>seaborn<\/code>\u5e93\u7ed8\u5236\u76f4\u65b9\u56fe\u6216\u7bb1\u7ebf\u56fe\uff0c\u4ee5\u4fbf\u66f4\u52a0\u76f4\u89c2\u5730\u7406\u89e3\u6570\u636e\u5206\u5e03\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5b9e\u9645\u5e94\u7528\u573a\u666f\u9700\u8981\u5728Python\u4e2d\u8fdb\u884c\u6570\u636e\u5206\u533a\u95f4\uff1f<\/strong><\/p>\n<p>\u6570\u636e\u5206\u533a\u95f4\u5728\u591a\u4e2a\u9886\u57df\u90fd\u6709\u5b9e\u9645\u5e94\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u5728\u5e02\u573a\u5206\u6790\u4e2d\uff0c\u4f01\u4e1a\u53ef\u4ee5\u6839\u636e\u5ba2\u6237\u7684\u6d88\u8d39\u6c34\u5e73\u5c06\u5ba2\u6237\u5206\u6210\u4e0d\u540c\u7684\u7fa4\u4f53\uff0c\u4ee5\u5236\u5b9a\u9488\u5bf9\u6027\u7684\u8425\u9500\u7b56\u7565\u3002<\/li>\n<li>\u5728\u6559\u80b2\u9886\u57df\uff0c\u6559\u5e08\u53ef\u4ee5\u6839\u636e\u5b66\u751f\u7684\u8003\u8bd5\u6210\u7ee9\u5c06\u5b66\u751f\u5206\u4e3a\u4e0d\u540c\u7684\u5b66\u4e60\u6c34\u5e73\uff0c\u4ee5\u4fbf\u5b9e\u65bd\u5dee\u5f02\u5316\u6559\u5b66\u3002<\/li>\n<li>\u5728\u91d1\u878d\u5206\u6790\u4e2d\uff0c\u6295\u8d44\u8005\u53ef\u4ee5\u6839\u636e\u8d44\u4ea7\u7684\u98ce\u9669\u6536\u76ca\u7279\u5f81\u5c06\u4e0d\u540c\u7684\u6295\u8d44\u4ea7\u54c1\u5206\u7ec4\uff0c\u5e2e\u52a9\u505a\u51fa\u66f4\u660e\u667a\u7684\u6295\u8d44\u51b3\u7b56\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed9\u6570\u636e\u5206\u533a\u95f4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528pandas.cut\u51fd\u6570\u3001\u4f7f\u7528numpy.digitize\u51fd\u6570\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1037848,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037834"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1037834"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037834\/revisions"}],"predecessor-version":[{"id":1037852,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1037834\/revisions\/1037852"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1037848"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1037834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1037834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1037834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}