{"id":1155253,"date":"2025-01-13T18:01:11","date_gmt":"2025-01-13T10:01:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1155253.html"},"modified":"2025-01-13T18:01:14","modified_gmt":"2025-01-13T10:01:14","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e5%b9%b3%e5%9d%87%e6%88%90%e7%bb%a9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1155253.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25193914\/4e2df598-4444-4414-9ebf-edb4a779abf0.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u6c42\u548c\u3001\u8ba1\u7b97\u957f\u5ea6\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7b49\u65b9\u6cd5\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\u3001\u4f7f\u7528Numpy\u5e93\u8fdb\u884c\u8ba1\u7b97\u3002<\/strong> \u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528Python\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6c42\u548c\u4e0e\u8ba1\u7b97\u957f\u5ea6<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u57fa\u7840\u7684\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u6c42\u548c\u548c\u8ba1\u7b97\u957f\u5ea6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5b66\u751f\u6210\u7ee9\u7684\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u6b65\u9aa4\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u6c42\u548c\u3002<\/li>\n<li>\u4f7f\u7528<code>len()<\/code>\u51fd\u6570\u8ba1\u7b97\u5217\u8868\u957f\u5ea6\u3002<\/li>\n<li>\u5c06\u603b\u548c\u9664\u4ee5\u957f\u5ea6\u5f97\u5230\u5e73\u5747\u6210\u7ee9\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">scores = [85, 90, 78, 92, 88]<\/p>\n<p>total = sum(scores)<\/p>\n<p>count = len(scores)<\/p>\n<p>average = total \/ count<\/p>\n<p>print(f&quot;Average score: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> <code>sum()<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u7684\u548c\uff0c<code>len()<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u5217\u8868\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\u3002\u5c06\u603b\u548c\u9664\u4ee5\u5143\u7d20\u4e2a\u6570\uff0c\u5373\u53ef\u5f97\u5230\u5e73\u5747\u6210\u7ee9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u51fd\u6570\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u7b5b\u9009\u548c\u5904\u7406\u6570\u636e\uff0c\u7136\u540e\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">scores = [85, 90, 78, 92, 88]<\/p>\n<p>average = sum(scores) \/ len(scores)<\/p>\n<p>print(f&quot;Average score: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u5229\u7528\u4e86<code>sum()<\/code>\u548c<code>len()<\/code>\u51fd\u6570\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Numpy\u5e93<\/h3>\n<\/p>\n<p><p>Numpy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u4fbf\u6377\u7684\u51fd\u6570\u6765\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002\u4f7f\u7528Numpy\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Numpy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528Numpy\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>scores = [85, 90, 78, 92, 88]<\/p>\n<p>average = np.mean(scores)<\/p>\n<p>print(f&quot;Average score: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Numpy\u7684<code>mean()<\/code>\u51fd\u6570\u76f4\u63a5\u8fd4\u56de\u6570\u7ec4\u7684\u5e73\u5747\u503c\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5305\u542bNaN\u503c\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u4e2d\u53ef\u80fd\u5305\u542bNaN\u503c\uff08Not a Number\uff09\uff0c\u8fd9\u4f1a\u5f71\u54cd\u5e73\u5747\u6210\u7ee9\u7684\u8ba1\u7b97\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Numpy\u7684<code>nanmean()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5305\u542bNaN\u503c\u7684\u6570\u636e\u7684\u5e73\u5747\u6210\u7ee9\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>scores = [85, 90, 78, np.nan, 88]<\/p>\n<p>average = np.nanmean(scores)<\/p>\n<p>print(f&quot;Average score: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>nanmean()<\/code>\u51fd\u6570\u4f1a\u5ffd\u7565NaN\u503c\uff0c\u8ba1\u7b97\u6709\u6548\u6570\u636e\u7684\u5e73\u5747\u6210\u7ee9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u6570\u636e\u3002\u4f7f\u7528Pandas\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\u4e5f\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528Pandas\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>scores = [85, 90, 78, 92, 88]<\/p>\n<p>df = pd.DataFrame(scores, columns=[&#39;Scores&#39;])<\/p>\n<p>average = df[&#39;Scores&#39;].mean()<\/p>\n<p>print(f&quot;Average score: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pandas\u7684<code>mean()<\/code>\u51fd\u6570\u540c\u6837\u53ef\u4ee5\u8ba1\u7b97\u5217\u7684\u5e73\u5747\u503c\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u591a\u79d1\u6210\u7ee9\u7684\u5e73\u5747\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u8ba1\u7b97\u591a\u4e2a\u79d1\u76ee\u7684\u5e73\u5747\u6210\u7ee9\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;Math&#39;: [85, 90, 78, 92, 88],<\/p>\n<p>    &#39;English&#39;: [80, 85, 88, 90, 87],<\/p>\n<p>    &#39;Science&#39;: [78, 82, 89, 94, 85]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>average = df.mean()<\/p>\n<p>print(average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u5f97\u5230\u6bcf\u4e2a\u79d1\u76ee\u7684\u5e73\u5747\u6210\u7ee9\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u4e0d\u540c\u6743\u91cd\u7684\u6210\u7ee9<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u4e0d\u540c\u79d1\u76ee\u7684\u6210\u7ee9\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u6743\u91cd\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u52a0\u6743\u5e73\u5747\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u603b\u5e73\u5747\u6210\u7ee9\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u6743\u91cd\uff1a<\/p>\n<\/p>\n<ul>\n<li>Math: 0.3<\/li>\n<li>English: 0.3<\/li>\n<li>Science: 0.4<\/li>\n<\/ul>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u8ba1\u7b97\u52a0\u6743\u5e73\u5747\u6210\u7ee9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;Math&#39;: [85, 90, 78, 92, 88],<\/p>\n<p>    &#39;English&#39;: [80, 85, 88, 90, 87],<\/p>\n<p>    &#39;Science&#39;: [78, 82, 89, 94, 85]<\/p>\n<p>}<\/p>\n<p>weights = {&#39;Math&#39;: 0.3, &#39;English&#39;: 0.3, &#39;Science&#39;: 0.4}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>weighted_average = (df * pd.Series(weights)).sum(axis=1).mean()<\/p>\n<p>print(f&quot;Weighted average score: {weighted_average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u5c06\u6210\u7ee9\u4e0e\u6743\u91cd\u76f8\u4e58\uff0c\u7136\u540e\u6c42\u548c\u5f97\u5230\u52a0\u6743\u5e73\u5747\u6210\u7ee9\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u53ef\u80fd\u5b58\u50a8\u5728\u66f4\u590d\u6742\u7684\u7ed3\u6784\u4e2d\uff0c\u6bd4\u5982\u5d4c\u5957\u5b57\u5178\u6216\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u6765\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_average(data):<\/p>\n<p>    if isinstance(data, dict):<\/p>\n<p>        return {k: calculate_average(v) for k, v in data.items()}<\/p>\n<p>    elif isinstance(data, list):<\/p>\n<p>        return sum(data) \/ len(data)<\/p>\n<p>    else:<\/p>\n<p>        return data<\/p>\n<p>data = {<\/p>\n<p>    &#39;Student1&#39;: [85, 90, 78],<\/p>\n<p>    &#39;Student2&#39;: [92, 88, 80],<\/p>\n<p>    &#39;Student3&#39;: [78, 82, 89]<\/p>\n<p>}<\/p>\n<p>average_scores = calculate_average(data)<\/p>\n<p>print(average_scores)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5904\u7406\u5d4c\u5957\u5b57\u5178\u6216\u5217\u8868\u7684\u6570\u636e\u7ed3\u6784\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u5b66\u751f\u7684\u5e73\u5747\u6210\u7ee9\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u5927\u6570\u636e\u96c6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u5de5\u5177\u548c\u65b9\u6cd5\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u4f7f\u7528Dask\u5e93\u6765\u5904\u7406\u5927\u6570\u636e\u96c6\u7684\u5e73\u5747\u6210\u7ee9\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Dask\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install dask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f7f\u7528Dask\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dask.dataframe as dd<\/p>\n<p>data = {<\/p>\n<p>    &#39;Math&#39;: [85, 90, 78, 92, 88],<\/p>\n<p>    &#39;English&#39;: [80, 85, 88, 90, 87],<\/p>\n<p>    &#39;Science&#39;: [78, 82, 89, 94, 85]<\/p>\n<p>}<\/p>\n<p>df = dd.from_pandas(pd.DataFrame(data), npartitions=2)<\/p>\n<p>average = df.mean().compute()<\/p>\n<p>print(average)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Dask\u53ef\u4ee5\u5e76\u884c\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f18\u5316\u8ba1\u7b97\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\uff0c\u4f18\u5316\u8ba1\u7b97\u6027\u80fd\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5411\u91cf\u5316\u64cd\u4f5c\uff1a<\/strong> \u907f\u514d\u4f7f\u7528\u5faa\u73af\uff0c\u5c3d\u91cf\u4f7f\u7528\u5411\u91cf\u5316\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\uff1a<\/strong> \u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\u3002<\/li>\n<li><strong>\u51cf\u5c11\u6570\u636e\u590d\u5236\uff1a<\/strong> \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6570\u636e\u590d\u5236\uff0c\u5c3d\u91cf\u5728\u539f\u5730\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff1a<\/strong> \u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982Numpy\u6570\u7ec4\u3001Pandas DataFrame\u7b49\u3002<\/li>\n<\/ol>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>scores = np.array([85, 90, 78, 92, 88])<\/p>\n<p>average = np.mean(scores)<\/p>\n<p>print(f&quot;Average score: {average}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5229\u7528Numpy\u7684\u9ad8\u6548\u8ba1\u7b97\u529f\u80fd\uff0c\u63d0\u9ad8\u8ba1\u7b97\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u5904\u7406\u5f02\u5e38\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u4e2d\u53ef\u80fd\u5305\u542b\u5f02\u5e38\u503c\uff08outliers\uff09\uff0c\u8fd9\u4f1a\u5f71\u54cd\u5e73\u5747\u6210\u7ee9\u7684\u8ba1\u7b97\u3002\u53ef\u4ee5\u4f7f\u7528\u4e2d\u4f4d\u6570\uff08median\uff09\u6765\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\uff0c\u51cf\u5c11\u5f02\u5e38\u503c\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>scores = [85, 90, 78, 92, 88, 1000]  # 1000 is an outlier<\/p>\n<p>median = np.median(scores)<\/p>\n<p>print(f&quot;Median score: {median}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e2d\u4f4d\u6570\u53ef\u4ee5\u51cf\u5c11\u5f02\u5e38\u503c\u5bf9\u5e73\u5747\u6210\u7ee9\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\uff0c\u5e76\u5904\u7406\u5404\u79cd\u590d\u6742\u60c5\u51b5\u3002\u6700\u57fa\u7840\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>sum()<\/code>\u548c<code>len()<\/code>\u51fd\u6570\u6c42\u548c\u548c\u8ba1\u7b97\u957f\u5ea6\uff0cNumpy\u548cPandas\u5e93\u63d0\u4f9b\u4e86\u66f4\u52a0\u4fbf\u6377\u548c\u9ad8\u6548\u7684\u51fd\u6570\u6765\u5904\u7406\u6570\u636e\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528Dask\u5e93\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97\uff0c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002\u5904\u7406\u5305\u542bNaN\u503c\u3001\u4e0d\u540c\u6743\u91cd\u3001\u590d\u6742\u6570\u636e\u7ed3\u6784\u548c\u5f02\u5e38\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u548c\u51fd\u6570\u6765\u8ba1\u7b97\u51c6\u786e\u7684\u5e73\u5747\u6210\u7ee9\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6210\u7ee9\u6570\u636e\u4ee5\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6765\u5b58\u50a8\u6210\u7ee9\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7\u5185\u7f6e\u7684<code>sum()<\/code>\u51fd\u6570\u548c<code>len()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u5c06\u6210\u7ee9\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\uff0c\u4f7f\u7528<code>sum()<\/code>\u8ba1\u7b97\u603b\u5206\uff0c\u518d\u7528<code>len()<\/code>\u8ba1\u7b97\u6210\u7ee9\u6570\u91cf\uff0c\u6700\u540e\u5c06\u603b\u5206\u9664\u4ee5\u6570\u91cf\u83b7\u53d6\u5e73\u5747\u6210\u7ee9\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u6765\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u4f7f\u7528NumPy\u5e93\u7684<code>mean()<\/code>\u51fd\u6570\u53ef\u4ee5\u66f4\u52a0\u65b9\u4fbf\u5730\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\u3002\u9996\u5148\u9700\u8981\u5c06\u6210\u7ee9\u6570\u636e\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u7136\u540e\u8c03\u7528<code>np.mean()<\/code>\u51fd\u6570\u5373\u53ef\u3002\u4f7f\u7528NumPy\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u80fd\u591f\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\uff0c\u5e76\u63d0\u4f9b\u66f4\u9ad8\u6548\u7684\u8ba1\u7b97\u6027\u80fd\u3002<\/p>\n<p><strong>\u5982\u679c\u6210\u7ee9\u4e2d\u5305\u542b\u7f3a\u5931\u503c\uff0c\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542b\u7f3a\u5931\u503c\u7684\u6210\u7ee9\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>pandas<\/code>\u5e93\u3002<code>pandas<\/code>\u63d0\u4f9b\u4e86<code>dropna()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5220\u9664\u7f3a\u5931\u503c\uff0c\u6216\u8005\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u586b\u8865\u7f3a\u5931\u503c\u3002\u5220\u9664\u6216\u586b\u8865\u7f3a\u5931\u503c\u540e\uff0c\u518d\u8fdb\u884c\u5e73\u5747\u6210\u7ee9\u7684\u8ba1\u7b97\uff0c\u53ef\u4ee5\u786e\u4fdd\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u6c42\u548c\u3001\u8ba1\u7b97\u957f\u5ea6\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u7b49\u65b9\u6cd5\u8ba1\u7b97\u5e73\u5747\u6210\u7ee9\u3001\u4f7f\u7528Numpy\u5e93\u8fdb\u884c\u8ba1\u7b97\u3002 \u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb [&hellip;]","protected":false},"author":3,"featured_media":1155264,"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\/1155253"}],"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=1155253"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155253\/revisions"}],"predecessor-version":[{"id":1155267,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155253\/revisions\/1155267"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1155264"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1155253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1155253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1155253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}