{"id":1075954,"date":"2025-01-08T11:49:00","date_gmt":"2025-01-08T03:49:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1075954.html"},"modified":"2025-01-08T11:49:03","modified_gmt":"2025-01-08T03:49:03","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e6%b1%82%e5%b9%b3%e5%9d%87%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1075954.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e2d\u6c42\u5e73\u5747\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180742\/43f96af4-7854-4361-88d7-a21e00e46196.webp\" alt=\"\u5982\u4f55\u5728python\u4e2d\u6c42\u5e73\u5747\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6c42\u5e73\u5747\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528numpy\u5e93\u3001\u4ee5\u53capandas\u5e93\u7b49\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\uff0c\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570sum()\u548clen()\u6765\u8ba1\u7b97\u5e73\u5747\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u548clen()\u6c42\u5e73\u5747\u6570<\/p>\n<p>def calculate_average(numbers):<\/p>\n<p>    return sum(numbers) \/ len(numbers)<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>average = calculate_average(numbers)<\/p>\n<p>print(&quot;Average using built-in functions:&quot;, average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NUMPY\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528numpy\u5e93\u662f\u8ba1\u7b97\u5e73\u5747\u6570\u7684\u53e6\u4e00\u79cd\u5e38\u89c1\u65b9\u6cd5\uff0cnumpy\u5e93\u662fPython\u4e2d\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u6570\u7ec4\u7684\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528numpy\u5e93\u6c42\u5e73\u5747\u6570<\/strong><\/h2>\n<p>numbers = np.array([1, 2, 3, 4, 5])<\/p>\n<p>average = np.mean(numbers)<\/p>\n<p>print(&quot;Average using numpy:&quot;, average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u5b89\u88c5numpy\u5e93\uff1a<\/strong> \u5728\u4f7f\u7528numpy\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528numpy\u7684\u597d\u5904\uff1a<\/strong> numpy\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u5e73\u5747\u6570\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u591a\u7ef4\u6570\u7ec4\uff0c\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u7b49\u3002\u5b83\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u5927\u91cf\u6570\u636e\u7684\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PANDAS\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u5e93\u662fPython\u4e2d\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u4f7f\u7528pandas\u5e93\u4e5f\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8ba1\u7b97\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528pandas\u5e93\u6c42\u5e73\u5747\u6570<\/strong><\/h2>\n<p>numbers = pd.Series([1, 2, 3, 4, 5])<\/p>\n<p>average = numbers.mean()<\/p>\n<p>print(&quot;Average using pandas:&quot;, average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u5b89\u88c5pandas\u5e93\uff1a<\/strong> \u5728\u4f7f\u7528pandas\u5e93\u4e4b\u524d\uff0c\u4e5f\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u7684\u597d\u5904\uff1a<\/strong> pandas\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u5e73\u5747\u6570\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u900f\u89c6\u8868\u3001\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u7b49\u3002\u5b83\u5bf9\u4e8e\u5904\u7406\u590d\u6742\u6570\u636e\u96c6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u7f3a\u5931\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u6570\u636e\u5904\u7406\u4e2d\uff0c\u6570\u636e\u96c6\u53ef\u80fd\u4f1a\u5305\u542b\u7f3a\u5931\u503c\u3002\u5904\u7406\u7f3a\u5931\u503c\u662f\u8ba1\u7b97\u5e73\u5747\u6570\u65f6\u5e38\u89c1\u7684\u4e00\u4e2a\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528numpy\u548cpandas\u5e93\u7684\u5185\u7f6e\u65b9\u6cd5\u6765\u5904\u7406\u7f3a\u5931\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e\u5305\u542b\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>numbers_with_nan = [1, 2, np.nan, 4, 5]<\/p>\n<h2><strong>\u4f7f\u7528numpy\u5904\u7406\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>average_with_nan_numpy = np.nanmean(numbers_with_nan)<\/p>\n<p>print(&quot;Average using numpy with NaN:&quot;, average_with_nan_numpy)<\/p>\n<h2><strong>\u4f7f\u7528pandas\u5904\u7406\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>numbers_series = pd.Series(numbers_with_nan)<\/p>\n<p>average_with_nan_pandas = numbers_series.mean()<\/p>\n<p>print(&quot;Average using pandas with NaN:&quot;, average_with_nan_pandas)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\uff0c\u8fd9\u610f\u5473\u7740\u4e0d\u540c\u7684\u6570\u503c\u5177\u6709\u4e0d\u540c\u7684\u6743\u91cd\u3002\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u6765\u5b9e\u73b0\u52a0\u6743\u5e73\u5747\u6570\u7684\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e\u548c\u5bf9\u5e94\u7684\u6743\u91cd<\/strong><\/h2>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>weights = [0.1, 0.2, 0.3, 0.2, 0.2]<\/p>\n<h2><strong>\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570<\/strong><\/h2>\n<p>weighted_average = np.average(numbers, weights=weights)<\/p>\n<p>print(&quot;Weighted average:&quot;, weighted_average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u52a0\u6743\u5e73\u5747\u6570\uff1a<\/strong> \u52a0\u6743\u5e73\u5747\u6570\u662f\u6307\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u65f6\uff0c\u4e0d\u540c\u7684\u6570\u503c\u5177\u6709\u4e0d\u540c\u7684\u6743\u91cd\u3002\u6bcf\u4e2a\u6570\u503c\u4e58\u4ee5\u5176\u5bf9\u5e94\u7684\u6743\u91cd\uff0c\u7136\u540e\u5c06\u4e58\u79ef\u6c42\u548c\uff0c\u6700\u540e\u9664\u4ee5\u6240\u6709\u6743\u91cd\u7684\u603b\u548c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528numpy\u7684\u597d\u5904\uff1a<\/strong> numpy\u5e93\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012weights\u53c2\u6570\u6765\u6307\u5b9a\u6743\u91cd\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8ba1\u7b97\u79fb\u52a8\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u4e2d\uff0c\u79fb\u52a8\u5e73\u5747\u6570\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u6280\u672f\uff0c\u7528\u4e8e\u5e73\u6ed1\u6570\u636e\u6ce2\u52a8\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u8ba1\u7b97\u79fb\u52a8\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;value&#39;: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u7a97\u53e3\u5927\u5c0f\u4e3a3\u7684\u79fb\u52a8\u5e73\u5747\u6570<\/strong><\/h2>\n<p>df[&#39;moving_average&#39;] = df[&#39;value&#39;].rolling(window=3).mean()<\/p>\n<p>print(&quot;Moving average:\\n&quot;, df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u79fb\u52a8\u5e73\u5747\u6570\uff1a<\/strong> \u79fb\u52a8\u5e73\u5747\u6570\u662f\u4e00\u79cd\u6280\u672f\u5206\u6790\u5de5\u5177\uff0c\u901a\u8fc7\u8ba1\u7b97\u6570\u636e\u96c6\u4e2d\u6307\u5b9a\u7a97\u53e3\u5927\u5c0f\u5185\u6570\u503c\u7684\u5e73\u5747\u6570\uff0c\u6765\u5e73\u6ed1\u6570\u636e\u6ce2\u52a8\u3002\u5e38\u7528\u4e8e\u65f6\u95f4\u5e8f\u5217\u5206\u6790\uff0c\u5982\u80a1\u7968\u4ef7\u683c\u3001\u6c14\u6e29\u53d8\u5316\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u7684\u597d\u5904\uff1a<\/strong> pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65f6\u95f4\u5e8f\u5217\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u79fb\u52a8\u5e73\u5747\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u6307\u5b9a\u7a97\u53e3\u5927\u5c0f\u548c\u5176\u4ed6\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u8ba1\u7b97\u7ec4\u5185\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5206\u7ec4\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u7684\u5e73\u5747\u6570\u3002\u8fd9\u5728\u6570\u636e\u5206\u6790\u548c\u7edf\u8ba1\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e\u5305\u542b\u7ec4\u4fe1\u606f<\/strong><\/h2>\n<p>data = {&#39;group&#39;: [&#39;A&#39;, &#39;A&#39;, &#39;B&#39;, &#39;B&#39;, &#39;C&#39;, &#39;C&#39;],<\/p>\n<p>        &#39;value&#39;: [1, 2, 3, 4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u7684\u5e73\u5747\u6570<\/strong><\/h2>\n<p>grouped_average = df.groupby(&#39;group&#39;)[&#39;value&#39;].mean()<\/p>\n<p>print(&quot;Grouped average:\\n&quot;, grouped_average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u5206\u7ec4\u6570\u636e\uff1a<\/strong> \u5206\u7ec4\u6570\u636e\u662f\u6307\u6570\u636e\u96c6\u4e2d\u5305\u542b\u7ec4\u4fe1\u606f\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u6839\u636e\u7ec4\u4fe1\u606f\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u7ec4\u7684\u7edf\u8ba1\u91cf\uff0c\u5982\u5e73\u5747\u6570\u3001\u603b\u548c\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u7684\u597d\u5904\uff1a<\/strong> pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5206\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u8ba1\u7b97\u3002\u53ef\u4ee5\u4f7f\u7528groupby()\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u4f7f\u7528agg()\u65b9\u6cd5\u8ba1\u7b97\u7edf\u8ba1\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8ba1\u7b97\u591a\u5217\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5305\u542b\u591a\u5217\u6570\u503c\u7684\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u8ba1\u7b97\u6bcf\u5217\u7684\u5e73\u5747\u6570\u3002\u8fd9\u5728\u6570\u636e\u5206\u6790\u548c\u7edf\u8ba1\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e\u5305\u542b\u591a\u5217\u6570\u503c<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>        &#39;B&#39;: [6, 7, 8, 9, 10],<\/p>\n<p>        &#39;C&#39;: [11, 12, 13, 14, 15]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u5217\u7684\u5e73\u5747\u6570<\/strong><\/h2>\n<p>column_average = df.mean()<\/p>\n<p>print(&quot;Column average:\\n&quot;, column_average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u591a\u5217\u6570\u503c\uff1a<\/strong> \u591a\u5217\u6570\u503c\u662f\u6307\u6570\u636e\u96c6\u4e2d\u5305\u542b\u591a\u4e2a\u5217\u7684\u6570\u503c\u60c5\u51b5\u3002\u53ef\u4ee5\u5bf9\u6bcf\u5217\u6570\u503c\u5206\u522b\u8ba1\u7b97\u7edf\u8ba1\u91cf\uff0c\u5982\u5e73\u5747\u6570\u3001\u603b\u548c\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u7684\u597d\u5904\uff1a<\/strong> pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u591a\u5217\u6570\u503c\u8fdb\u884c\u8ba1\u7b97\u3002\u53ef\u4ee5\u4f7f\u7528mean()\u65b9\u6cd5\u8ba1\u7b97\u6bcf\u5217\u7684\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u8ba1\u7b97\u884c\u5185\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5305\u542b\u591a\u884c\u6570\u503c\u7684\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u8ba1\u7b97\u6bcf\u884c\u7684\u5e73\u5747\u6570\u3002\u8fd9\u5728\u6570\u636e\u5206\u6790\u548c\u7edf\u8ba1\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e\u5305\u542b\u591a\u884c\u6570\u503c<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>        &#39;B&#39;: [6, 7, 8, 9, 10],<\/p>\n<p>        &#39;C&#39;: [11, 12, 13, 14, 15]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u884c\u7684\u5e73\u5747\u6570<\/strong><\/h2>\n<p>row_average = df.mean(axis=1)<\/p>\n<p>print(&quot;Row average:\\n&quot;, row_average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u591a\u884c\u6570\u503c\uff1a<\/strong> \u591a\u884c\u6570\u503c\u662f\u6307\u6570\u636e\u96c6\u4e2d\u5305\u542b\u591a\u884c\u7684\u6570\u503c\u60c5\u51b5\u3002\u53ef\u4ee5\u5bf9\u6bcf\u884c\u6570\u503c\u5206\u522b\u8ba1\u7b97\u7edf\u8ba1\u91cf\uff0c\u5982\u5e73\u5747\u6570\u3001\u603b\u548c\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u7684\u597d\u5904\uff1a<\/strong> pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u591a\u884c\u6570\u503c\u8fdb\u884c\u8ba1\u7b97\u3002\u53ef\u4ee5\u4f7f\u7528mean()\u65b9\u6cd5\u5e76\u6307\u5b9aaxis\u53c2\u6570\u8ba1\u7b97\u6bcf\u884c\u7684\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u8ba1\u7b97\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u662f\u79fb\u52a8\u5e73\u5747\u6570\u7684\u4e00\u79cd\u53d8\u4f53\uff0c\u5b83\u901a\u8fc7\u8d4b\u4e88\u4e0d\u540c\u65f6\u95f4\u70b9\u4e0d\u540c\u7684\u6743\u91cd\u6765\u5e73\u6ed1\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u8ba1\u7b97\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;value&#39;: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5b9a\u4e49\u52a0\u6743\u51fd\u6570<\/strong><\/h2>\n<p>def weighted_moving_average(values, weights):<\/p>\n<p>    return sum(values * weights) \/ sum(weights)<\/p>\n<h2><strong>\u8ba1\u7b97\u7a97\u53e3\u5927\u5c0f\u4e3a3\u7684\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570<\/strong><\/h2>\n<p>weights = [0.1, 0.3, 0.6]<\/p>\n<p>df[&#39;weighted_moving_average&#39;] = df[&#39;value&#39;].rolling(window=3).apply(lambda x: weighted_moving_average(x, weights), raw=True)<\/p>\n<p>print(&quot;Weighted moving average:\\n&quot;, df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\uff1a<\/strong> \u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u662f\u901a\u8fc7\u8d4b\u4e88\u4e0d\u540c\u65f6\u95f4\u70b9\u4e0d\u540c\u7684\u6743\u91cd\u6765\u5e73\u6ed1\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528\u52a0\u6743\u51fd\u6570\u8ba1\u7b97\u6bcf\u4e2a\u7a97\u53e3\u5185\u7684\u52a0\u6743\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u7684\u597d\u5904\uff1a<\/strong> pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65f6\u95f4\u5e8f\u5217\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u4f7f\u7528rolling()\u65b9\u6cd5\u521b\u5efa\u6eda\u52a8\u7a97\u53e3\uff0c\u5e76\u4f7f\u7528apply()\u65b9\u6cd5\u5e94\u7528\u52a0\u6743\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u8ba1\u7b97\u6307\u6570\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u6307\u6570\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u662f\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\uff0c\u6743\u91cd\u968f\u7740\u65f6\u95f4\u6307\u6570\u8870\u51cf\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u8ba1\u7b97\u6307\u6570\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;value&#39;: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u6307\u6570\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570<\/strong><\/h2>\n<p>df[&#39;ewm_average&#39;] = df[&#39;value&#39;].ewm(span=3, adjust=False).mean()<\/p>\n<p>print(&quot;Exponentially weighted moving average:\\n&quot;, df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u6307\u6570\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\uff1a<\/strong> \u6307\u6570\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u662f\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\uff0c\u6743\u91cd\u968f\u7740\u65f6\u95f4\u6307\u6570\u8870\u51cf\u3002\u53ef\u4ee5\u4f7f\u7528\u6307\u6570\u52a0\u6743\u51fd\u6570\u8ba1\u7b97\u6bcf\u4e2a\u65f6\u95f4\u70b9\u7684\u52a0\u6743\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u7684\u597d\u5904\uff1a<\/strong> pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65f6\u95f4\u5e8f\u5217\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u6307\u6570\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u4f7f\u7528ewm()\u65b9\u6cd5\u521b\u5efa\u6307\u6570\u52a0\u6743\u7a97\u53e3\uff0c\u5e76\u4f7f\u7528mean()\u65b9\u6cd5\u8ba1\u7b97\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u8ba1\u7b97\u6eda\u52a8\u7a97\u53e3\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u6eda\u52a8\u7a97\u53e3\u5e73\u5747\u6570\u662f\u4e00\u79cd\u6280\u672f\u5206\u6790\u5de5\u5177\uff0c\u901a\u8fc7\u8ba1\u7b97\u6570\u636e\u96c6\u4e2d\u6307\u5b9a\u7a97\u53e3\u5927\u5c0f\u5185\u6570\u503c\u7684\u5e73\u5747\u6570\uff0c\u6765\u5e73\u6ed1\u6570\u636e\u6ce2\u52a8\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u8ba1\u7b97\u6eda\u52a8\u7a97\u53e3\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;value&#39;: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u7a97\u53e3\u5927\u5c0f\u4e3a3\u7684\u6eda\u52a8\u7a97\u53e3\u5e73\u5747\u6570<\/strong><\/h2>\n<p>df[&#39;rolling_average&#39;] = df[&#39;value&#39;].rolling(window=3).mean()<\/p>\n<p>print(&quot;Rolling window average:\\n&quot;, df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u6eda\u52a8\u7a97\u53e3\u5e73\u5747\u6570\uff1a<\/strong> \u6eda\u52a8\u7a97\u53e3\u5e73\u5747\u6570\u662f\u4e00\u79cd\u6280\u672f\u5206\u6790\u5de5\u5177\uff0c\u901a\u8fc7\u8ba1\u7b97\u6570\u636e\u96c6\u4e2d\u6307\u5b9a\u7a97\u53e3\u5927\u5c0f\u5185\u6570\u503c\u7684\u5e73\u5747\u6570\uff0c\u6765\u5e73\u6ed1\u6570\u636e\u6ce2\u52a8\u3002\u5e38\u7528\u4e8e\u65f6\u95f4\u5e8f\u5217\u5206\u6790\uff0c\u5982\u80a1\u7968\u4ef7\u683c\u3001\u6c14\u6e29\u53d8\u5316\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u7684\u597d\u5904\uff1a<\/strong> pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65f6\u95f4\u5e8f\u5217\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8ba1\u7b97\u6eda\u52a8\u7a97\u53e3\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u4f7f\u7528rolling()\u65b9\u6cd5\u521b\u5efa\u6eda\u52a8\u7a97\u53e3\uff0c\u5e76\u4f7f\u7528mean()\u65b9\u6cd5\u8ba1\u7b97\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u8ba1\u7b97\u5206\u4f4d\u6570\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u5206\u4f4d\u6570\u5e73\u5747\u6570\u662f\u6307\u5728\u6570\u636e\u96c6\u4e2d\uff0c\u6839\u636e\u5206\u4f4d\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u8ba1\u7b97\u5206\u4f4d\u6570\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;value&#39;: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5206\u4f4d\u6570<\/strong><\/h2>\n<p>df[&#39;quantile&#39;] = pd.qcut(df[&#39;value&#39;], 4)<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u4e2a\u5206\u4f4d\u6570\u7684\u5e73\u5747\u6570<\/strong><\/h2>\n<p>quantile_average = df.groupby(&#39;quantile&#39;)[&#39;value&#39;].mean()<\/p>\n<p>print(&quot;Quantile average:\\n&quot;, quantile_average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u5206\u4f4d\u6570\u5e73\u5747\u6570\uff1a<\/strong> \u5206\u4f4d\u6570\u5e73\u5747\u6570\u662f\u6307\u5728\u6570\u636e\u96c6\u4e2d\uff0c\u6839\u636e\u5206\u4f4d\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u7684qcut()\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u4f4d\u6570\u5206\u7ec4\uff0c\u5e76\u4f7f\u7528groupby()\u65b9\u6cd5\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u7684\u597d\u5904\uff1a<\/strong> pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u4f4d\u6570\u5206\u7ec4\u548c\u8ba1\u7b97\u7edf\u8ba1\u91cf\u3002\u53ef\u4ee5\u4f7f\u7528qcut()\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u4f4d\u6570\u5206\u7ec4\uff0c\u5e76\u4f7f\u7528groupby()\u65b9\u6cd5\u8ba1\u7b97\u6bcf\u4e2a\u5206\u7ec4\u7684\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u8ba1\u7b97\u622a\u5c3e\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u622a\u5c3e\u5e73\u5747\u6570\u662f\u6307\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u65f6\uff0c\u53bb\u9664\u6570\u636e\u96c6\u4e2d\u6700\u5c0f\u548c\u6700\u5927\u7684\u4e00\u90e8\u5206\u6570\u636e\uff0c\u7136\u540e\u8ba1\u7b97\u5269\u4f59\u6570\u636e\u7684\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u6765\u8ba1\u7b97\u622a\u5c3e\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<h2><strong>\u8ba1\u7b97\u622a\u5c3e\u5e73\u5747\u6570<\/strong><\/h2>\n<p>trimmed_mean = np.mean(numbers[1:-1])<\/p>\n<p>print(&quot;Trimmed mean:&quot;, trimmed_mean)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u622a\u5c3e\u5e73\u5747\u6570\uff1a<\/strong> \u622a\u5c3e\u5e73\u5747\u6570\u662f\u6307\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u65f6\uff0c\u53bb\u9664\u6570\u636e\u96c6\u4e2d\u6700\u5c0f\u548c\u6700\u5927\u7684\u4e00\u90e8\u5206\u6570\u636e\uff0c\u7136\u540e\u8ba1\u7b97\u5269\u4f59\u6570\u636e\u7684\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u7684mean()\u65b9\u6cd5\u8ba1\u7b97\u622a\u5c3e\u540e\u7684\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528numpy\u7684\u597d\u5904\uff1a<\/strong> numpy\u5e93\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u622a\u5c3e\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u901a\u8fc7\u5207\u7247\u64cd\u4f5c\u53bb\u9664\u6570\u636e\u96c6\u4e2d\u6700\u5c0f\u548c\u6700\u5927\u7684\u4e00\u90e8\u5206\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528mean()\u65b9\u6cd5\u8ba1\u7b97\u5269\u4f59\u6570\u636e\u7684\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u8ba1\u7b97\u51e0\u4f55\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u51e0\u4f55\u5e73\u5747\u6570\u662f\u6307\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u65f6\uff0c\u5c06\u6570\u636e\u96c6\u4e2d\u6240\u6709\u6570\u503c\u76f8\u4e58\uff0c\u7136\u540e\u53d6\u4e58\u79ef\u7684n\u6b21\u65b9\u6839\u3002\u53ef\u4ee5\u4f7f\u7528scipy\u5e93\u6765\u8ba1\u7b97\u51e0\u4f55\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.stats import gmean<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u8ba1\u7b97\u51e0\u4f55\u5e73\u5747\u6570<\/strong><\/h2>\n<p>geometric_mean = gmean(numbers)<\/p>\n<p>print(&quot;Geometric mean:&quot;, geometric_mean)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u51e0\u4f55\u5e73\u5747\u6570\uff1a<\/strong> \u51e0\u4f55\u5e73\u5747\u6570\u662f\u6307\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u65f6\uff0c\u5c06\u6570\u636e\u96c6\u4e2d\u6240\u6709\u6570\u503c\u76f8\u4e58\uff0c\u7136\u540e\u53d6\u4e58\u79ef\u7684n\u6b21\u65b9\u6839\u3002\u53ef\u4ee5\u4f7f\u7528scipy\u5e93\u7684gmean()\u65b9\u6cd5\u8ba1\u7b97\u51e0\u4f55\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528scipy\u7684\u597d\u5904\uff1a<\/strong> scipy\u5e93\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u51e0\u4f55\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u4f7f\u7528gmean()\u65b9\u6cd5\u8ba1\u7b97\u6570\u636e\u96c6\u7684\u51e0\u4f55\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u8ba1\u7b97\u8c03\u548c\u5e73\u5747\u6570<\/h3>\n<\/p>\n<p><p>\u8c03\u548c\u5e73\u5747\u6570\u662f\u6307\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u65f6\uff0c\u4f7f\u7528\u6570\u503c\u7684\u5012\u6570\u8ba1\u7b97\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u4f7f\u7528scipy\u5e93\u6765\u8ba1\u7b97\u8c03\u548c\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.stats import hmean<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u8ba1\u7b97\u8c03\u548c\u5e73\u5747\u6570<\/strong><\/h2>\n<p>harmonic_mean = hmean(numbers)<\/p>\n<p>print(&quot;Harmonic mean:&quot;, harmonic_mean)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7ec6\u8282\u63cf\u8ff0\uff1a<\/h4>\n<\/p>\n<p><p><strong>\u8c03\u548c\u5e73\u5747\u6570\uff1a<\/strong> \u8c03\u548c\u5e73\u5747\u6570\u662f\u6307\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u65f6\uff0c\u4f7f\u7528\u6570\u503c\u7684\u5012\u6570\u8ba1\u7b97\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u4f7f\u7528scipy\u5e93\u7684hmean()\u65b9\u6cd5\u8ba1\u7b97\u8c03\u548c\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528scipy\u7684\u597d\u5904\uff1a<\/strong> scipy\u5e93\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u8c03\u548c\u5e73\u5747\u6570\u3002\u53ef\u4ee5\u4f7f\u7528hmean()\u65b9\u6cd5\u8ba1\u7b97\u6570\u636e\u96c6\u7684\u8c03\u548c\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u6c42\u5e73\u5747\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528numpy\u5e93\u3001\u4f7f\u7528pandas\u5e93\u3001\u5904\u7406\u7f3a\u5931\u503c\u3001\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6570\u3001\u8ba1\u7b97\u79fb\u52a8\u5e73\u5747\u6570\u3001\u8ba1\u7b97\u7ec4\u5185\u5e73\u5747\u6570\u3001\u8ba1\u7b97\u591a\u5217\u5e73\u5747\u6570\u3001\u8ba1\u7b97\u884c\u5185\u5e73\u5747\u6570\u3001\u8ba1\u7b97\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u3001\u8ba1\u7b97\u6307\u6570\u52a0\u6743\u79fb\u52a8\u5e73\u5747\u6570\u3001\u8ba1\u7b97\u6eda\u52a8\u7a97\u53e3\u5e73\u5747\u6570\u3001\u8ba1\u7b97\u5206\u4f4d\u6570\u5e73\u5747\u6570\u3001\u8ba1\u7b97\u622a\u5c3e\u5e73\u5747\u6570\u3001\u8ba1\u7b97\u51e0\u4f55\u5e73\u5747\u6570\u3001\u8ba1\u7b97\u8c03\u548c\u5e73\u5747\u6570\u7b49\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u5e73\u5747\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u4e00\u7ec4\u6570\u636e\u7684\u5e73\u5747\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u4e00\u7ec4\u6570\u636e\u7684\u5e73\u5747\u6570\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6216\u7b2c\u4e09\u65b9\u5e93\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5229\u7528<code>sum()<\/code>\u51fd\u6570\u548c<code>len()<\/code>\u51fd\u6570\u7ed3\u5408\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u6570\u5b57\u5217\u8868<code>data = [10, 20, 30]<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>average = sum(data) \/ len(data)<\/code>\u6765\u8ba1\u7b97\u5e73\u5747\u503c\u3002\u6b64\u5916\uff0c\u4f7f\u7528NumPy\u5e93\u7684<code>numpy.mean()<\/code>\u51fd\u6570\u4e5f\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<p><strong>Python\u4e2d\u6c42\u5e73\u5747\u6570\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5728\u8ba1\u7b97\u5e73\u5747\u6570\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u662f\u6570\u5b57\u7c7b\u578b\uff0c\u4f8b\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\u3002\u5982\u679c\u6570\u636e\u4e2d\u5305\u542b\u975e\u6570\u5b57\u7c7b\u578b\uff08\u5982\u5b57\u7b26\u4e32\u6216None\uff09\uff0c\u4f1a\u5bfc\u81f4\u8ba1\u7b97\u51fa\u9519\u3002\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u6216\u8fc7\u6ee4\u51fd\u6570\u6765\u6e05\u7406\u6570\u636e\uff0c\u4f8b\u5982<code>filtered_data = [x for x in data if isinstance(x, (int, float))]<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u53ea\u5bf9\u6709\u6548\u6570\u5b57\u8fdb\u884c\u5e73\u5747\u6570\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u6c42\u5e73\u5747\u6570\uff1f<\/strong><br \/>\u9664\u4e86Python\u5185\u7f6e\u51fd\u6570\u5916\uff0c\u8fd8\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u8ba1\u7b97\u5e73\u5747\u6570\u3002NumPy\u662f\u6700\u5e38\u7528\u7684\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u8ba1\u7b97\u529f\u80fd\uff0c\u4f7f\u7528\u65f6\u53ea\u9700\u5bfc\u5165NumPy\u5e76\u8c03\u7528<code>numpy.mean()<\/code>\u3002Pandas\u5e93\u4e5f\u5f88\u5f3a\u5927\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u6570\u636e\u6846\uff08DataFrame\uff09\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>dataframe.mean()<\/code>\u6765\u8ba1\u7b97\u5404\u5217\u6216\u884c\u7684\u5e73\u5747\u503c\uff0c\u8fd9\u5728\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u5b9e\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6c42\u5e73\u5747\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528numpy\u5e93\u3001\u4ee5\u53capandas\u5e93\u7b49\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd [&hellip;]","protected":false},"author":3,"featured_media":1075962,"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\/1075954"}],"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=1075954"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075954\/revisions"}],"predecessor-version":[{"id":1075964,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075954\/revisions\/1075964"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1075962"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1075954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1075954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1075954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}