{"id":1077757,"date":"2025-01-08T12:05:14","date_gmt":"2025-01-08T04:05:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1077757.html"},"modified":"2025-01-08T12:05:16","modified_gmt":"2025-01-08T04:05:16","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%ae%a1%e7%ae%97%e5%87%a0%e5%b9%b4%e5%86%85%e5%b9%b3%e5%9d%87-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1077757.html","title":{"rendered":"\u5982\u4f55\u7528python\u8ba1\u7b97\u51e0\u5e74\u5185\u5e73\u5747"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181636\/9f4a7707-16a8-4f21-a0ca-c721d30ea8cb.webp\" alt=\"\u5982\u4f55\u7528python\u8ba1\u7b97\u51e0\u5e74\u5185\u5e73\u5747\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u8ba1\u7b97\u51e0\u5e74\u5185\u5e73\u5747<\/strong><\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u7528\u6765\u6267\u884c\u5404\u79cd\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c\u5305\u62ec\u8ba1\u7b97\u51e0\u5e74\u5185\u7684\u5e73\u5747\u503c\u3002<strong>\u4f7f\u7528Python\u8ba1\u7b97\u51e0\u5e74\u5185\u5e73\u5747\u503c\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Pandas\u5e93\u5904\u7406\u6570\u636e\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u5904\u7406\u6570\u636e\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u9ad8\u6548\u5730\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u5e76\u4e14\u5176\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u975e\u5e38\u5f3a\u5927\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8ba1\u7b97\u51e0\u5e74\u5185\u7684\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Pandas\u5e93\u5904\u7406\u6570\u636e<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u7684\u5f00\u6e90Python\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Pandas\u8ba1\u7b97\u51e0\u5e74\u5185\u5e73\u5747\u503c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5e74\u4efd\u548c\u6570\u636e\u7684DataFrame<\/strong><\/h2>\n<p>data = {&#39;Year&#39;: [2017, 2018, 2019, 2020, 2021],<\/p>\n<p>        &#39;Value&#39;: [10, 15, 10, 20, 25]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u503c<\/strong><\/h2>\n<p>average_value = df[&#39;Value&#39;].mean()<\/p>\n<p>print(&quot;\u5e73\u5747\u503c:&quot;, average_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u5e74\u4efd\u548c\u6570\u636e\u7684DataFrame\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u7684<code>mean()<\/code>\u51fd\u6570\u8ba1\u7b97\u4e86\u51e0\u5e74\u7684\u5e73\u5747\u503c\u3002<strong>Pandas\u5e93\u7684\u5f3a\u5927\u4e4b\u5904\u5728\u4e8e\u5176\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u548c\u9ad8\u6548\u5904\u7406\u5927\u6570\u636e\u96c6\u7684\u80fd\u529b<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\u548c\u5f3a\u5927\u7684\u6570\u7ec4\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528NumPy\u8ba1\u7b97\u51e0\u5e74\u5185\u5e73\u5747\u503c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>values = np.array([10, 15, 10, 20, 25])<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u503c<\/strong><\/h2>\n<p>average_value = np.mean(values)<\/p>\n<p>print(&quot;\u5e73\u5747\u503c:&quot;, average_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684NumPy\u6570\u7ec4\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528NumPy\u7684<code>mean()<\/code>\u51fd\u6570\u8ba1\u7b97\u4e86\u51e0\u5e74\u7684\u5e73\u5747\u503c\u3002<strong>NumPy\u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528Pandas\u548cNumPy\u5e93\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8ba1\u7b97\u51e0\u5e74\u5185\u7684\u5e73\u5747\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_average(data):<\/p>\n<p>    total = sum(data)<\/p>\n<p>    count = len(data)<\/p>\n<p>    average = total \/ count<\/p>\n<p>    return average<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684\u5217\u8868<\/strong><\/h2>\n<p>values = [10, 15, 10, 20, 25]<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u503c<\/strong><\/h2>\n<p>average_value = calculate_average(values)<\/p>\n<p>print(&quot;\u5e73\u5747\u503c:&quot;, average_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>calculate_average<\/code>\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8fd4\u56de\u5e73\u5747\u503c\u3002\u7136\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684\u5217\u8868\uff0c\u5e76\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8ba1\u7b97\u4e86\u51e0\u5e74\u7684\u5e73\u5747\u503c\u3002<strong>\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u4fee\u6539<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8ba1\u7b97\u6bcf\u5e74\u7684\u5e73\u5747\u6e29\u5ea6\u6216\u6bcf\u5e74\u7684\u5e73\u5747\u9500\u552e\u989d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Pandas\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u548c\u6570\u636e\u7684DataFrame<\/strong><\/h2>\n<p>data = {&#39;Date&#39;: [&#39;2017-01-01&#39;, &#39;2018-01-01&#39;, &#39;2019-01-01&#39;, &#39;2020-01-01&#39;, &#39;2021-01-01&#39;],<\/p>\n<p>        &#39;Value&#39;: [10, 15, 10, 20, 25]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5c06\u65e5\u671f\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u7c7b\u578b<\/strong><\/h2>\n<p>df[&#39;Date&#39;] = pd.to_datetime(df[&#39;Date&#39;])<\/p>\n<h2><strong>\u8bbe\u7f6e\u65e5\u671f\u5217\u4e3a\u7d22\u5f15<\/strong><\/h2>\n<p>df.set_index(&#39;Date&#39;, inplace=True)<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u5e74\u7684\u5e73\u5747\u503c<\/strong><\/h2>\n<p>yearly_average = df.resample(&#39;Y&#39;).mean()<\/p>\n<p>print(&quot;\u6bcf\u5e74\u7684\u5e73\u5747\u503c:\\n&quot;, yearly_average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u548c\u6570\u636e\u7684DataFrame\u3002\u63a5\u7740\uff0c\u6211\u4eec\u5c06\u65e5\u671f\u5217\u8f6c\u6362\u4e3a\u65e5\u671f\u65f6\u95f4\u7c7b\u578b\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u7d22\u5f15\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u7684<code>resample()<\/code>\u51fd\u6570\u6309\u5e74\u91cd\u65b0\u91c7\u6837\u6570\u636e\uff0c\u5e76\u8ba1\u7b97\u6bcf\u5e74\u7684\u5e73\u5747\u503c\u3002<strong>\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u65f6\uff0cPandas\u5e93\u7684<code>to_datetime()<\/code>\u548c<code>resample()<\/code>\u51fd\u6570\u975e\u5e38\u6709\u7528<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u7f3a\u5931\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5b9e\u9645\u6570\u636e\u65f6\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u7f3a\u5931\u6570\u636e\u7684\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u7f3a\u5931\u6570\u636e\u5e76\u8ba1\u7b97\u5e73\u5747\u503c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5e74\u4efd\u548c\u6570\u636e\u7684DataFrame\uff0c\u5176\u4e2d\u5305\u542b\u7f3a\u5931\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;Year&#39;: [2017, 2018, 2019, 2020, 2021],<\/p>\n<p>        &#39;Value&#39;: [10, None, 10, 20, 25]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u586b\u5145\u7f3a\u5931\u6570\u636e<\/strong><\/h2>\n<p>df[&#39;Value&#39;].fillna(df[&#39;Value&#39;].mean(), inplace=True)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u503c<\/strong><\/h2>\n<p>average_value = df[&#39;Value&#39;].mean()<\/p>\n<p>print(&quot;\u5e73\u5747\u503c:&quot;, average_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u5e74\u4efd\u548c\u6570\u636e\u7684DataFrame\uff0c\u5176\u4e2d\u5305\u542b\u7f3a\u5931\u6570\u636e\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u7684<code>fillna()<\/code>\u51fd\u6570\u586b\u5145\u7f3a\u5931\u6570\u636e\uff0c\u5e76\u8ba1\u7b97\u51e0\u5e74\u7684\u5e73\u5747\u503c\u3002<strong>\u5904\u7406\u7f3a\u5931\u6570\u636e\u65f6\uff0c\u4f7f\u7528Pandas\u7684<code>fillna()<\/code>\u51fd\u6570\u975e\u5e38\u65b9\u4fbf<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5927\u6570\u636e\u96c6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pandas\u7684\u5206\u5757\u8bfb\u53d6\u529f\u80fd\uff0c\u4ee5\u8282\u7701\u5185\u5b58\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5904\u7406\u5927\u6570\u636e\u96c6\u5e76\u8ba1\u7b97\u51e0\u5e74\u5185\u5e73\u5747\u503c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5927\u6570\u636e\u96c6\u7684CSV\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file_path = &#39;large_dataset.csv&#39;<\/p>\n<h2><strong>\u521d\u59cb\u5316\u53d8\u91cf<\/strong><\/h2>\n<p>total_sum = 0<\/p>\n<p>total_count = 0<\/p>\n<h2><strong>\u5206\u5757\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>for chunk in pd.read_csv(file_path, chunksize=10000):<\/p>\n<p>    total_sum += chunk[&#39;Value&#39;].sum()<\/p>\n<p>    total_count += chunk[&#39;Value&#39;].count()<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u503c<\/strong><\/h2>\n<p>average_value = total_sum \/ total_count<\/p>\n<p>print(&quot;\u5e73\u5747\u503c:&quot;, average_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u6307\u5b9a\u4e86\u4e00\u4e2a\u5305\u542b\u5927\u6570\u636e\u96c6\u7684CSV\u6587\u4ef6\u8def\u5f84\u3002\u63a5\u7740\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e86\u603b\u548c\u548c\u603b\u8ba1\u6570\u53d8\u91cf\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u7684\u5206\u5757\u8bfb\u53d6\u529f\u80fd\u6309\u5757\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u7d2f\u52a0\u6bcf\u5757\u6570\u636e\u7684\u603b\u548c\u548c\u603b\u8ba1\u6570\u3002\u6700\u540e\uff0c\u6211\u4eec\u8ba1\u7b97\u4e86\u51e0\u5e74\u7684\u5e73\u5747\u503c\u3002<strong>\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u4f7f\u7528Pandas\u7684\u5206\u5757\u8bfb\u53d6\u529f\u80fd\u53ef\u4ee5\u663e\u8457\u8282\u7701\u5185\u5b58<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528SQL\u67e5\u8be2<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u7684\u6570\u636e\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8be2\u6765\u8ba1\u7b97\u51e0\u5e74\u5185\u7684\u5e73\u5747\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528SQLite\u6570\u636e\u5e93\u8ba1\u7b97\u51e0\u5e74\u5185\u5e73\u5747\u503c\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aSQLite\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5e74\u4efd\u548c\u6570\u636e\u7684\u8868<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;<\/p>\n<p>CREATE TABLE IF NOT EXISTS data (<\/p>\n<p>    Year INTEGER,<\/p>\n<p>    Value REAL<\/p>\n<p>)<\/p>\n<p>&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&#39;INSERT INTO data (Year, Value) VALUES (2017, 10)&#39;)<\/p>\n<p>cursor.execute(&#39;INSERT INTO data (Year, Value) VALUES (2018, 15)&#39;)<\/p>\n<p>cursor.execute(&#39;INSERT INTO data (Year, Value) VALUES (2019, 10)&#39;)<\/p>\n<p>cursor.execute(&#39;INSERT INTO data (Year, Value) VALUES (2020, 20)&#39;)<\/p>\n<p>cursor.execute(&#39;INSERT INTO data (Year, Value) VALUES (2021, 25)&#39;)<\/p>\n<p>conn.commit()<\/p>\n<h2><strong>\u6267\u884cSQL\u67e5\u8be2\u8ba1\u7b97\u5e73\u5747\u503c<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT AVG(Value) FROM data&#39;)<\/p>\n<p>average_value = cursor.fetchone()[0]<\/p>\n<p>print(&quot;\u5e73\u5747\u503c:&quot;, average_value)<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2aSQLite\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u5e74\u4efd\u548c\u6570\u636e\u7684\u8868\u3002\u63a5\u7740\uff0c\u6211\u4eec\u63d2\u5165\u4e86\u4e00\u4e9b\u6570\u636e\uff0c\u5e76\u6267\u884cSQL\u67e5\u8be2\u8ba1\u7b97\u5e73\u5747\u503c\u3002\u6700\u540e\uff0c\u6211\u4eec\u5173\u95ed\u4e86\u6570\u636e\u5e93\u8fde\u63a5\u3002<strong>\u4f7f\u7528SQL\u67e5\u8be2\u8ba1\u7b97\u5e73\u5747\u503c\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u6570\u636e\u5e93\u4e2d\u6267\u884c\u590d\u6742\u7684\u67e5\u8be2\u64cd\u4f5c<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528Matplotlib\u5e93\u8fdb\u884c\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u51e0\u5e74\u5185\u5e73\u5747\u503c\u540e\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u7ed3\u679c\u8fdb\u884c\u53ef\u89c6\u5316\u5c55\u793a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Matplotlib\u5e93\u8fdb\u884c\u53ef\u89c6\u5316\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5e74\u4efd\u548c\u6570\u636e\u7684DataFrame<\/strong><\/h2>\n<p>data = {&#39;Year&#39;: [2017, 2018, 2019, 2020, 2021],<\/p>\n<p>        &#39;Value&#39;: [10, 15, 10, 20, 25]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u5e74\u7684\u5e73\u5747\u503c<\/strong><\/h2>\n<p>yearly_average = df.groupby(&#39;Year&#39;).mean()<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(yearly_average.index, yearly_average[&#39;Value&#39;], marker=&#39;o&#39;)<\/p>\n<p>plt.xlabel(&#39;Year&#39;)<\/p>\n<p>plt.ylabel(&#39;Average Value&#39;)<\/p>\n<p>plt.title(&#39;Yearly Average Value&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u5e74\u4efd\u548c\u6570\u636e\u7684DataFrame\uff0c\u5e76\u8ba1\u7b97\u4e86\u6bcf\u5e74\u7684\u5e73\u5747\u503c\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u4e86\u4e00\u4e2a\u6298\u7ebf\u56fe\uff0c\u5c55\u793a\u6bcf\u5e74\u7684\u5e73\u5747\u503c\u53d8\u5316\u60c5\u51b5\u3002<strong>\u4f7f\u7528Matplotlib\u5e93\u8fdb\u884c\u53ef\u89c6\u5316\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u5c55\u793a\u6570\u636e\u7684\u53d8\u5316\u8d8b\u52bf<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528Scikit-learn\u5e93\u8fdb\u884c\u9884\u6d4b<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u9884\u6d4b\u672a\u6765\u51e0\u5e74\u7684\u5e73\u5747\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Scikit-learn\u5e93\u8fdb\u884c\u9884\u6d4b\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<p>from sklearn.linear_model import LinearRegression<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5e74\u4efd\u548c\u6570\u636e\u7684DataFrame<\/strong><\/h2>\n<p>data = {&#39;Year&#39;: [2017, 2018, 2019, 2020, 2021],<\/p>\n<p>        &#39;Value&#39;: [10, 15, 10, 20, 25]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u51c6\u5907\u8bad\u7ec3\u6570\u636e<\/strong><\/h2>\n<p>X = df[&#39;Year&#39;].values.reshape(-1, 1)<\/p>\n<p>y = df[&#39;Value&#39;].values<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(X, y)<\/p>\n<h2><strong>\u9884\u6d4b\u672a\u6765\u51e0\u5e74\u7684\u5e73\u5747\u503c<\/strong><\/h2>\n<p>future_years = np.array([2022, 2023, 2024, 2025]).reshape(-1, 1)<\/p>\n<p>predicted_values = model.predict(future_years)<\/p>\n<p>print(&quot;\u9884\u6d4b\u7684\u5e73\u5747\u503c:&quot;, predicted_values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u5e74\u4efd\u548c\u6570\u636e\u7684DataFrame\uff0c\u5e76\u51c6\u5907\u4e86\u8bad\u7ec3\u6570\u636e\u3002\u63a5\u7740\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u7ebf\u6027\u56de\u5f52\u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u8bad\u7ec3\u6570\u636e\u8bad\u7ec3\u6a21\u578b\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u9884\u6d4b\u4e86\u672a\u6765\u51e0\u5e74\u7684\u5e73\u5747\u503c\u3002<strong>\u4f7f\u7528Scikit-learn\u5e93\u8fdb\u884c\u9884\u6d4b\uff0c\u53ef\u4ee5\u4e3a\u672a\u6765\u7684\u51b3\u7b56\u63d0\u4f9b\u6570\u636e\u652f\u6301<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u8ba1\u7b97\u51e0\u5e74\u5185\u5e73\u5747\u503c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u5904\u7406\u6570\u636e\u3001\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3001\u5904\u7406\u7f3a\u5931\u6570\u636e\u3001\u5904\u7406\u5927\u6570\u636e\u96c6\u3001\u4f7f\u7528SQL\u67e5\u8be2\u3001\u4f7f\u7528Matplotlib\u5e93\u8fdb\u884c\u53ef\u89c6\u5316\u3001\u4f7f\u7528Scikit-learn\u5e93\u8fdb\u884c\u9884\u6d4b\u7b49\u3002<strong>\u4e0d\u540c\u7684\u65b9\u6cd5\u5404\u6709\u4f18\u52bf\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Python\u5728\u6570\u636e\u5206\u6790\u548c\u8ba1\u7b97\u65b9\u9762\u7684\u5f3a\u5927\u529f\u80fd\u3002\u65e0\u8bba\u662f\u5904\u7406\u5c0f\u6570\u636e\u96c6\u8fd8\u662f\u5927\u6570\u636e\u96c6\uff0cPython\u90fd\u6709\u76f8\u5e94\u7684\u5de5\u5177\u548c\u5e93\u53ef\u4ee5\u4f7f\u7528\u3002\u5e0c\u671b\u672c\u6587\u80fd\u4e3a\u8bfb\u8005\u63d0\u4f9b\u6709\u4ef7\u503c\u7684\u53c2\u8003\uff0c\u5e2e\u52a9\u5927\u5bb6\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u66f4\u597d\u5730\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u591a\u4e2a\u5e74\u4efd\u7684\u6570\u636e\u5e73\u5747\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u6216Pandas\u5e93\u6765\u8ba1\u7b97\u591a\u4e2a\u5e74\u4efd\u7684\u6570\u636e\u5e73\u5747\u503c\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u6240\u9700\u7684\u5e93\u3002\u4f7f\u7528Pandas\u65f6\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aDataFrame\uff0c\u5c06\u6bcf\u5e74\u7684\u6570\u636e\u5b58\u50a8\u5728\u5217\u4e2d\uff0c\u7136\u540e\u4f7f\u7528<code>.mean()<\/code>\u65b9\u6cd5\u8ba1\u7b97\u5e73\u5747\u503c\u3002\u4f7f\u7528NumPy\u65f6\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u4e3a\u6570\u7ec4\uff0c\u5229\u7528<code>np.mean()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u5747\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<p><strong>Python\u662f\u5426\u652f\u6301\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u5e73\u5747\u8ba1\u7b97\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u975e\u5e38\u9002\u5408\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u4f7f\u7528Pandas\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u65f6\u95f4\u6233\u4f5c\u4e3a\u7d22\u5f15\uff0c\u7136\u540e\u8fdb\u884c\u5206\u7ec4\u8ba1\u7b97\u5e73\u5747\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6309\u5e74\u3001\u6708\u6216\u65e5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u65f6\u95f4\u6bb5\u7684\u5e73\u5747\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u6709\u6548\u5730\u5206\u6790\u8d8b\u52bf\u548c\u53d8\u5316\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u5904\u7406\u7f3a\u5931\u503c\u540e\u518d\u8ba1\u7b97\u5e73\u5747\u503c\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5728\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u65f6\uff0c\u7f3a\u5931\u503c\u662f\u5e38\u89c1\u7684\u95ee\u9898\u3002Pandas\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u7f3a\u5931\u503c\uff0c\u6bd4\u5982\u4f7f\u7528<code>fillna()<\/code>\u6765\u586b\u5145\u7f3a\u5931\u503c\u6216\u4f7f\u7528<code>dropna()<\/code>\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u3002\u5728\u5904\u7406\u5b8c\u7f3a\u5931\u503c\u540e\uff0c\u7528\u6237\u53ef\u4ee5\u5b89\u5168\u5730\u8ba1\u7b97\u5e73\u5747\u503c\uff0c\u786e\u4fdd\u7ed3\u679c\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u8ba1\u7b97\u51e0\u5e74\u5185\u5e73\u5747 Python\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u7528\u6765\u6267\u884c\u5404\u79cd\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c\u5305\u62ec\u8ba1 [&hellip;]","protected":false},"author":3,"featured_media":1077764,"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\/1077757"}],"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=1077757"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077757\/revisions"}],"predecessor-version":[{"id":1077766,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1077757\/revisions\/1077766"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1077764"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1077757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1077757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1077757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}