{"id":1111508,"date":"2025-01-08T17:29:58","date_gmt":"2025-01-08T09:29:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1111508.html"},"modified":"2025-01-08T17:30:00","modified_gmt":"2025-01-08T09:30:00","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%ae%97%e5%b9%b3%e5%9d%87%e5%b7%a5%e8%b5%84%e6%94%b6%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1111508.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u7b97\u5e73\u5747\u5de5\u8d44\u6536\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073930\/56d75dba-b8f3-43c5-b3df-80a4f43f7765.webp\" alt=\"python\u4e2d\u5982\u4f55\u7b97\u5e73\u5747\u5de5\u8d44\u6536\u5165\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u6536\u5165\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u548clen()\u3001\u4f7f\u7528pandas\u5e93\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528pandas\u5e93\u662f\u6700\u63a8\u8350\u7684\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528pandas\u5e93\u6765\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u6536\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Python\u5185\u7f6e\u51fd\u6570sum()\u548clen()<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570<code>sum()<\/code>\u548c<code>len()<\/code>\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5de5\u8d44\u6536\u5165\u7684\u5e73\u5747\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u7b80\u5355\u7684\u6570\u636e\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e<\/p>\n<p>salaries = [50000, 60000, 75000, 80000, 90000]<\/p>\n<h2><strong>\u8ba1\u7b97\u603b\u548c<\/strong><\/h2>\n<p>total_salary = sum(salaries)<\/p>\n<h2><strong>\u8ba1\u7b97\u6570\u91cf<\/strong><\/h2>\n<p>num_of_salaries = len(salaries)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44<\/strong><\/h2>\n<p>average_salary = total_salary \/ num_of_salaries<\/p>\n<p>print(f&quot;\u5e73\u5747\u5de5\u8d44\u662f: {average_salary}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u4ee5\u4e0a\u4ee3\u7801\u901a\u8fc7\u5c06\u6240\u6709\u5de5\u8d44\u76f8\u52a0\u5e76\u9664\u4ee5\u5de5\u8d44\u7684\u6570\u91cf\uff0c\u8ba1\u7b97\u51fa\u5e73\u5747\u5de5\u8d44\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u63a5\uff0c\u4f46\u5bf9\u5927\u578b\u6570\u636e\u96c6\u6216\u9700\u8981\u590d\u6742\u6570\u636e\u5904\u7406\u7684\u60c5\u51b5\u5e76\u4e0d\u9002\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5f3a\u5927\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\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;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;, &#39;Edward&#39;],<\/p>\n<p>        &#39;Salary&#39;: [50000, 60000, 75000, 80000, 90000]}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44<\/strong><\/h2>\n<p>average_salary = df[&#39;Salary&#39;].mean()<\/p>\n<p>print(f&quot;\u5e73\u5747\u5de5\u8d44\u662f: {average_salary}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002\u9996\u5148\uff0c\u901a\u8fc7\u5b57\u5178\u521b\u5efa\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528pandas\u63d0\u4f9b\u7684<code>mean()<\/code>\u51fd\u6570\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\uff0c\u5982\u7b5b\u9009\u3001\u5206\u7ec4\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\uff0c\u8fd9\u5141\u8bb8\u60a8\u6dfb\u52a0\u989d\u5916\u7684\u903b\u8f91\u6216\u6570\u636e\u9a8c\u8bc1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_average_salary(salaries):<\/p>\n<p>    if not salaries:<\/p>\n<p>        return 0<\/p>\n<p>    total_salary = sum(salaries)<\/p>\n<p>    num_of_salaries = len(salaries)<\/p>\n<p>    return total_salary \/ num_of_salaries<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>salaries = [50000, 60000, 75000, 80000, 90000]<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44<\/strong><\/h2>\n<p>average_salary = calculate_average_salary(salaries)<\/p>\n<p>print(f&quot;\u5e73\u5747\u5de5\u8d44\u662f: {average_salary}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u81ea\u5b9a\u4e49\u51fd\u6570<code>calculate_average_salary<\/code>\u9996\u5148\u68c0\u67e5\u8f93\u5165\u5217\u8868\u662f\u5426\u4e3a\u7a7a\uff0c\u7136\u540e\u8ba1\u7b97\u603b\u5de5\u8d44\u548c\u5de5\u8d44\u6570\u91cf\uff0c\u6700\u540e\u8fd4\u56de\u5e73\u5747\u5de5\u8d44\u3002\u8fd9\u79cd\u65b9\u6cd5\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u7684\u529f\u80fd\u6216\u6570\u636e\u5904\u7406\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5927\u578b\u6570\u636e\u96c6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u4f7f\u7528pandas\u5e93\u548cDataFrame\u5bf9\u8c61\u662f\u6700\u6709\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4eceCSV\u6587\u4ef6\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;salaries.csv&#39;)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44<\/strong><\/h2>\n<p>average_salary = df[&#39;Salary&#39;].mean()<\/p>\n<p>print(f&quot;\u5e73\u5747\u5de5\u8d44\u662f: {average_salary}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u901a\u8fc7\u4eceCSV\u6587\u4ef6\u52a0\u8f7d\u6570\u636e\uff0c\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u548c\u5206\u6790\u5927\u578b\u6570\u636e\u96c6\u3002pandas\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u8bfb\u53d6\u6570\u636e\u7684\u65b9\u6cd5\uff0c\u5982<code>read_csv()<\/code>\u3001<code>read_excel()<\/code>\u7b49\uff0c\u4f7f\u6570\u636e\u52a0\u8f7d\u548c\u5904\u7406\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6570\u636e\u9884\u5904\u7406\u548c\u6e05\u6d17<\/h3>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u548c\u6e05\u6d17\uff0c\u4f8b\u5982\u5904\u7406\u7f3a\u5931\u503c\u548c\u5f02\u5e38\u503c\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;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;, &#39;Edward&#39;],<\/p>\n<p>        &#39;Salary&#39;: [50000, 60000, None, 80000, 90000]}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5904\u7406\u7f3a\u5931\u503c\uff08\u4f8b\u5982\uff0c\u7528\u5e73\u5747\u503c\u586b\u5145\uff09<\/strong><\/h2>\n<p>df[&#39;Salary&#39;].fillna(df[&#39;Salary&#39;].mean(), inplace=True)<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44<\/strong><\/h2>\n<p>average_salary = df[&#39;Salary&#39;].mean()<\/p>\n<p>print(f&quot;\u5e73\u5747\u5de5\u8d44\u662f: {average_salary}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u6570\u636e\u9884\u5904\u7406\u548c\u6e05\u6d17\u662f\u6570\u636e\u5206\u6790\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u901a\u8fc7\u5904\u7406\u7f3a\u5931\u503c\u548c\u5f02\u5e38\u503c\uff0c\u53ef\u4ee5\u786e\u4fdd\u8ba1\u7b97\u7ed3\u679c\u7684\u51c6\u786e\u6027\u548c\u53ef\u9760\u6027\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>fillna()<\/code>\u51fd\u6570\u5c06\u7f3a\u5931\u503c\u586b\u5145\u4e3a\u5217\u7684\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5206\u7ec4\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u6309\u7ec4\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\uff0c\u4f8b\u5982\u6309\u90e8\u95e8\u6216\u804c\u4f4d\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;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;, &#39;Edward&#39;],<\/p>\n<p>        &#39;Department&#39;: [&#39;HR&#39;, &#39;Engineering&#39;, &#39;HR&#39;, &#39;Engineering&#39;, &#39;HR&#39;],<\/p>\n<p>        &#39;Salary&#39;: [50000, 60000, 75000, 80000, 90000]}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6309\u90e8\u95e8\u5206\u7ec4\u5e76\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44<\/strong><\/h2>\n<p>average_salary_by_department = df.groupby(&#39;Department&#39;)[&#39;Salary&#39;].mean()<\/p>\n<p>print(average_salary_by_department)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u4f7f\u7528pandas\u5e93\u7684<code>groupby()<\/code>\u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u5730\u6309\u7ec4\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6309\u90e8\u95e8\u5206\u7ec4\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u90e8\u95e8\u7684\u5e73\u5747\u5de5\u8d44\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u5206\u7ec4\u5206\u6790\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u53ef\u89c6\u5316\u5e73\u5747\u5de5\u8d44<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u5e73\u5747\u5de5\u8d44\uff0c\u53ef\u4ee5\u4f7f\u7528\u53ef\u89c6\u5316\u5e93\u5982matplotlib\u6216seaborn\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;, &#39;David&#39;, &#39;Edward&#39;],<\/p>\n<p>        &#39;Department&#39;: [&#39;HR&#39;, &#39;Engineering&#39;, &#39;HR&#39;, &#39;Engineering&#39;, &#39;HR&#39;],<\/p>\n<p>        &#39;Salary&#39;: [50000, 60000, 75000, 80000, 90000]}<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6309\u90e8\u95e8\u5206\u7ec4\u5e76\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44<\/strong><\/h2>\n<p>average_salary_by_department = df.groupby(&#39;Department&#39;)[&#39;Salary&#39;].mean().reset_index()<\/p>\n<h2><strong>\u53ef\u89c6\u5316<\/strong><\/h2>\n<p>sns.barplot(x=&#39;Department&#39;, y=&#39;Salary&#39;, data=average_salary_by_department)<\/p>\n<p>plt.title(&#39;Average Salary by Department&#39;)<\/p>\n<p>plt.xlabel(&#39;Department&#39;)<\/p>\n<p>plt.ylabel(&#39;Average Salary&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u4f7f\u7528seaborn\u548cmatplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u67f1\u72b6\u56fe\u6765\u5c55\u793a\u5404\u90e8\u95e8\u7684\u5e73\u5747\u5de5\u8d44\u3002\u901a\u8fc7\u53ef\u89c6\u5316\uff0c\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u5206\u6790\u6570\u636e\u5e76\u53d1\u73b0\u6f5c\u5728\u7684\u8d8b\u52bf\u548c\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5de5\u8d44\u6570\u636e\u53ef\u80fd\u5b58\u50a8\u5728\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u4f8b\u5982\u5d4c\u5957\u7684\u5b57\u5178\u6216JSON\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u5d4c\u5957\u5b57\u5178\u6570\u636e<\/strong><\/h2>\n<p>data = &#39;&#39;&#39;{<\/p>\n<p>    &quot;employees&quot;: [<\/p>\n<p>        {&quot;name&quot;: &quot;Alice&quot;, &quot;salary&quot;: 50000},<\/p>\n<p>        {&quot;name&quot;: &quot;Bob&quot;, &quot;salary&quot;: 60000},<\/p>\n<p>        {&quot;name&quot;: &quot;Charlie&quot;, &quot;salary&quot;: 75000},<\/p>\n<p>        {&quot;name&quot;: &quot;David&quot;, &quot;salary&quot;: 80000},<\/p>\n<p>        {&quot;name&quot;: &quot;Edward&quot;, &quot;salary&quot;: 90000}<\/p>\n<p>    ]<\/p>\n<p>}&#39;&#39;&#39;<\/p>\n<h2><strong>\u89e3\u6790JSON\u6570\u636e<\/strong><\/h2>\n<p>parsed_data = json.loads(data)<\/p>\n<h2><strong>\u63d0\u53d6\u5de5\u8d44\u4fe1\u606f<\/strong><\/h2>\n<p>salaries = [employee[&#39;salary&#39;] for employee in parsed_data[&#39;employees&#39;]]<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44<\/strong><\/h2>\n<p>average_salary = sum(salaries) \/ len(salaries)<\/p>\n<p>print(f&quot;\u5e73\u5747\u5de5\u8d44\u662f: {average_salary}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u5728\u5904\u7406\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528json\u5e93\u89e3\u6790\u6570\u636e\uff0c\u5e76\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4ece\u5d4c\u5957\u7684\u5b57\u5178\u6570\u636e\u4e2d\u63d0\u53d6\u5de5\u8d44\u4fe1\u606f\uff0c\u5e76\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u6765\u81eaAPI\u6216\u5176\u4ed6\u590d\u6742\u6570\u636e\u6e90\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u6570\u503c\u6570\u636e\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>salaries = np.array([50000, 60000, 75000, 80000, 90000])<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44<\/strong><\/h2>\n<p>average_salary = np.mean(salaries)<\/p>\n<p>print(f&quot;\u5e73\u5747\u5de5\u8d44\u662f: {average_salary}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u548c\u8ba1\u7b97\u6570\u503c\u6570\u636e\u3002\u901a\u8fc7\u5c06\u5de5\u8d44\u6570\u636e\u8f6c\u6362\u4e3aNumPy\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>mean()<\/code>\u51fd\u6570\u5feb\u901f\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u503c\u8ba1\u7b97\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u6536\u5165\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u57fa\u672c\u4efb\u52a1\u4e4b\u4e00\u3002\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001pandas\u5e93\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u590d\u6742\u5ea6\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u5de5\u8d44\u6570\u636e\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\u5de5\u8d44\u6536\u5165\u7684\u5e73\u5747\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u6765\u5b58\u50a8\u591a\u4e2a\u5de5\u8d44\u6536\u5165\uff0c\u5e76\u5229\u7528\u5185\u7f6e\u51fd\u6570\u8ba1\u7b97\u5e73\u5747\u503c\u3002\u9996\u5148\uff0c\u5c06\u6240\u6709\u5de5\u8d44\u6536\u5165\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u8ba1\u7b97\u603b\u6536\u5165\uff0c\u518d\u9664\u4ee5\u5217\u8868\u7684\u957f\u5ea6\uff0c\u83b7\u5f97\u5e73\u5747\u5de5\u8d44\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">salaries = [3000, 4000, 5000, 6000]\naverage_salary = sum(salaries) \/ len(salaries)\nprint(average_salary)\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff0cPandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aDataFrame\uff0c\u5c06\u5de5\u8d44\u6536\u5165\u5b58\u50a8\u4e3a\u4e00\u5217\uff0c\u7136\u540e\u4f7f\u7528<code>mean()<\/code>\u65b9\u6cd5\u6765\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;salaries&#39;: [3000, 4000, 5000, 6000]}\ndf = pd.DataFrame(data)\naverage_salary = df[&#39;salaries&#39;].mean()\nprint(average_salary)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u7f3a\u5931\u503c\u7684\u5de5\u8d44\u6570\u636e\u4ee5\u8ba1\u7b97\u5e73\u5747\u503c\uff1f<\/strong><br \/>\u5728\u5de5\u8d44\u6570\u636e\u4e2d\uff0c\u7f3a\u5931\u503c\u662f\u5e38\u89c1\u7684\u95ee\u9898\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u4e2d\u7684<code>dropna()<\/code>\u65b9\u6cd5\u5728\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u4e4b\u524d\u5220\u9664\u7f3a\u5931\u503c\uff0c\u6216\u8005\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u586b\u5145\u7f3a\u5931\u503c\u3002\u4ee5\u4e0b\u662f\u5904\u7406\u7f3a\u5931\u503c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndata = {&#39;salaries&#39;: [3000, None, 5000, 6000]}\ndf = pd.DataFrame(data)\naverage_salary = df[&#39;salaries&#39;].dropna().mean()  # \u5220\u9664\u7f3a\u5931\u503c\nprint(average_salary)\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u786e\u4fdd\u60a8\u80fd\u591f\u51c6\u786e\u5730\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u6536\u5165\uff0c\u5373\u4f7f\u5728\u6570\u636e\u4e0d\u5b8c\u6574\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u5e73\u5747\u5de5\u8d44\u6536\u5165\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570sum()\u548clen()\u3001\u4f7f\u7528pandas\u5e93\u3001\u7f16\u5199\u81ea\u5b9a [&hellip;]","protected":false},"author":3,"featured_media":1111510,"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\/1111508"}],"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=1111508"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111508\/revisions"}],"predecessor-version":[{"id":1111511,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111508\/revisions\/1111511"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1111510"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1111508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1111508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1111508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}