{"id":1126344,"date":"2025-01-08T20:00:23","date_gmt":"2025-01-08T12:00:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1126344.html"},"modified":"2025-01-08T20:00:25","modified_gmt":"2025-01-08T12:00:25","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e6%95%b0%e6%8d%ae%e6%af%8f%e4%b8%80%e8%a1%8c%e7%9a%84%e5%9d%87%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1126344.html","title":{"rendered":"python\u5982\u4f55\u6c42\u6570\u636e\u6bcf\u4e00\u884c\u7684\u5747\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090717\/8c1ec45e-8edd-4343-a345-c8bfad7b6135.webp\" alt=\"python\u5982\u4f55\u6c42\u6570\u636e\u6bcf\u4e00\u884c\u7684\u5747\u503c\" \/><\/p>\n<p><p> <strong>Python\u6c42\u6570\u636e\u6bcf\u4e00\u884c\u7684\u5747\u503c<\/strong>\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u548cPandas\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u51fd\u6570\u6765\u8ba1\u7b97\u6570\u636e\u6bcf\u4e00\u884c\u7684\u5747\u503c\u3002<strong>\u4f7f\u7528Pandas\u7684<code>mean<\/code>\u51fd\u6570\u3001\u4f7f\u7528NumPy\u7684<code>mean<\/code>\u51fd\u6570\u3001\u624b\u52a8\u8ba1\u7b97<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u5c55\u5f00\u4f7f\u7528Pandas\u7684<code>mean<\/code>\u51fd\u6570\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Pandas\u7684<code>mean<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pandas\u5e93\u8ba1\u7b97\u6570\u636e\u6bcf\u4e00\u884c\u5747\u503c\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165Pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165Pandas\u5e93\u3002\u5982\u679c\u8fd8\u6ca1\u6709\u5b89\u88c5Pandas\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165Pandas\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u6570\u636e\u6846<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846\uff08DataFrame\uff09\uff0c\u53ef\u4ee5\u4ece\u5b57\u5178\u3001\u5217\u8868\u3001CSV\u6587\u4ef6\u7b49\u591a\u79cd\u6570\u636e\u6e90\u521b\u5efa\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5982\u4e0b\u6240\u793a\u7684\u6570\u636e\u6846\uff1a<\/p>\n<\/p>\n<p><pre><code>   A  B  C<\/p>\n<p>0  1  4  7<\/p>\n<p>1  2  5  8<\/p>\n<p>2  3  6  9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8ba1\u7b97\u6bcf\u4e00\u884c\u7684\u5747\u503c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u7684<code>mean<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u6bcf\u4e00\u884c\u7684\u5747\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">row_means = df.mean(axis=1)<\/p>\n<p>print(row_means)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>axis=1<\/code>\u8868\u793a\u6309\u884c\u8fdb\u884c\u64cd\u4f5c\u3002\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>0    4.0<\/p>\n<p>1    5.0<\/p>\n<p>2    6.0<\/p>\n<p>dtype: float64<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u8868\u793a\u6bcf\u4e00\u884c\u7684\u5747\u503c\u5206\u522b\u4e3a4.0\u30015.0\u548c6.0\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u7684<code>mean<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u8ba1\u7b97\u6570\u636e\u6bcf\u4e00\u884c\u7684\u5747\u503c\u540c\u6837\u9ad8\u6548\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528NumPy\u5e93\u8ba1\u7b97\u6570\u636e\u6bcf\u4e00\u884c\u5747\u503c\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165NumPy\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165NumPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efaNumPy\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = np.array([<\/p>\n<p>    [1, 4, 7],<\/p>\n<p>    [2, 5, 8],<\/p>\n<p>    [3, 6, 9]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8ba1\u7b97\u6bcf\u4e00\u884c\u7684\u5747\u503c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u7684<code>mean<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u6bcf\u4e00\u884c\u7684\u5747\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">row_means = np.mean(data, axis=1)<\/p>\n<p>print(row_means)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>axis=1<\/code>\u8868\u793a\u6309\u884c\u8fdb\u884c\u64cd\u4f5c\u3002\u8f93\u51fa\u7ed3\u679c\u5c06\u662f\uff1a<\/p>\n<\/p>\n<p><pre><code>[4. 5. 6.]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u8868\u793a\u6bcf\u4e00\u884c\u7684\u5747\u503c\u5206\u522b\u4e3a4.0\u30015.0\u548c6.0\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u624b\u52a8\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f7f\u7528\u989d\u5916\u7684\u5e93\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5185\u7f6e\u51fd\u6570\u624b\u52a8\u8ba1\u7b97\u6bcf\u4e00\u884c\u7684\u5747\u503c\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u6570\u636e\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    [1, 4, 7],<\/p>\n<p>    [2, 5, 8],<\/p>\n<p>    [3, 6, 9]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8ba1\u7b97\u6bcf\u4e00\u884c\u7684\u5747\u503c<\/h4>\n<\/p>\n<p><p>\u624b\u52a8\u8ba1\u7b97\u6bcf\u4e00\u884c\u7684\u5747\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">row_means = [sum(row) \/ len(row) for row in data]<\/p>\n<p>print(row_means)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>[4.0, 5.0, 6.0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u8868\u793a\u6bcf\u4e00\u884c\u7684\u5747\u503c\u5206\u522b\u4e3a4.0\u30015.0\u548c6.0\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0Python\u4e2d\u6570\u636e\u6bcf\u4e00\u884c\u5747\u503c\u7684\u8ba1\u7b97\u3002<strong>\u4f7f\u7528Pandas\u7684<code>mean<\/code>\u51fd\u6570\u3001\u4f7f\u7528NumPy\u7684<code>mean<\/code>\u51fd\u6570\u3001\u624b\u52a8\u8ba1\u7b97<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u3002Pandas\u548cNumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u4e14\u7b80\u6d01\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u662f\u5904\u7406\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u800c\u624b\u52a8\u8ba1\u7b97\u5219\u9002\u5408\u7b80\u5355\u7684\u5c0f\u89c4\u6a21\u6570\u636e\u64cd\u4f5c\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728\u4f7f\u7528Python\u65f6\uff0c\u5982\u4f55\u8ba1\u7b97\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u6216\u6570\u636e\u6846\u4e2d\u6bcf\u4e00\u884c\u7684\u5747\u503c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6216Pandas\u5e93\u6765\u8ba1\u7b97\u6bcf\u4e00\u884c\u7684\u5747\u503c\u3002\u5bf9\u4e8eNumPy\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.mean()<\/code>\u51fd\u6570\u5e76\u6307\u5b9a<code>axis=1<\/code>\uff0c\u800c\u5bf9\u4e8ePandas DataFrame\uff0c\u53ef\u4ee5\u4f7f\u7528<code>DataFrame.mean()<\/code>\u65b9\u6cd5\u540c\u6837\u6307\u5b9a<code>axis=1<\/code>\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u80fd\u6709\u6548\u5730\u5904\u7406\u6570\u636e\u5e76\u8fd4\u56de\u6bcf\u4e00\u884c\u7684\u5747\u503c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u662f\u5426\u53ef\u4ee5\u5ffd\u7565NaN\u503c\u6765\u8ba1\u7b97\u5747\u503c\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528Pandas\u5e93\u65f6\uff0c<code>DataFrame.mean()<\/code>\u65b9\u6cd5\u9ed8\u8ba4\u4f1a\u5ffd\u7565NaN\u503c\u8fdb\u884c\u5747\u503c\u8ba1\u7b97\u3002\u5982\u679c\u5e0c\u671b\u5728NumPy\u4e2d\u5904\u7406NaN\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.nanmean()<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4f1a\u81ea\u52a8\u5ffd\u7565NaN\u503c\u5e76\u8ba1\u7b97\u5747\u503c\u3002<\/p>\n<p><strong>\u5982\u679c\u60f3\u8981\u5c06\u5747\u503c\u6dfb\u52a0\u5230\u539f\u59cb\u6570\u636e\u4e2d\uff0c\u5982\u4f55\u64cd\u4f5c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u4e2d\u7684<code>assign()<\/code>\u65b9\u6cd5\u6216\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217\u6765\u5b58\u50a8\u5747\u503c\u3002\u5728NumPy\u4e2d\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5e76\u5c06\u5747\u503c\u4f5c\u4e3a\u65b0\u7684\u4e00\u5217\u9644\u52a0\u5230\u539f\u59cb\u6570\u636e\u4e2d\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u6570\u636e\u5206\u6790\u66f4\u4e3a\u76f4\u89c2\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6c42\u6570\u636e\u6bcf\u4e00\u884c\u7684\u5747\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u548cPandas\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0 [&hellip;]","protected":false},"author":3,"featured_media":1126351,"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\/1126344"}],"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=1126344"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126344\/revisions"}],"predecessor-version":[{"id":1126354,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126344\/revisions\/1126354"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1126351"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1126344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1126344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1126344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}