{"id":1034207,"date":"2024-12-31T11:48:19","date_gmt":"2024-12-31T03:48:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1034207.html"},"modified":"2024-12-31T11:48:21","modified_gmt":"2024-12-31T03:48:21","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1034207.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4f7f\u7528\u4e8c\u7ef4\u6570\u7ec4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/6b5479be-3384-4a28-b7d4-a015e2ecc235.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u4f7f\u7528\u4e8c\u7ef4\u6570\u7ec4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f7f\u7528\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u5217\u8868\u3001NumPy\u5e93\u6216Pandas\u5e93\u3002\u4f7f\u7528\u5217\u8868\u3001\u4f7f\u7528NumPy\u6570\u7ec4\u3001\u4f7f\u7528Pandas DataFrame<\/strong>\u3002\u63a5\u4e0b\u6765\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u5f0f\uff0c\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u548c\u64cd\u4f5c\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e8c\u7ef4\u6570\u7ec4\u53ef\u4ee5\u7528\u5d4c\u5957\u5217\u8868\u6765\u8868\u793a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u5217\u8868\u6765\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bbf\u95ee\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(array[0][1])  # \u8f93\u51fa2<\/p>\n<p>print(array[2][2])  # \u8f93\u51fa9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u4fee\u6539\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u4fee\u6539\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array[0][1] = 20<\/p>\n<p>print(array[0][1])  # \u8f93\u51fa20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u904d\u5386\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684for\u5faa\u73af\u6765\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for row in array:<\/p>\n<p>    for element in row:<\/p>\n<p>        print(element, end=&quot; &quot;)<\/p>\n<p>    print()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002\u5b83\u6bd4\u7eaf\u5217\u8868\u64cd\u4f5c\u66f4\u9ad8\u6548\uff0c\u529f\u80fd\u66f4\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5NumPy<\/h4>\n<\/p>\n<p><p>\u4f60\u9700\u8981\u5148\u5b89\u88c5NumPy\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\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><h4>2. \u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>array<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8bbf\u95ee\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95eeNumPy\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(array[0, 1])  # \u8f93\u51fa2<\/p>\n<p>print(array[2, 2])  # \u8f93\u51fa9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u4fee\u6539\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u4fee\u6539NumPy\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array[0, 1] = 20<\/p>\n<p>print(array[0, 1])  # \u8f93\u51fa20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u5207\u7247\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u652f\u6301\u5f3a\u5927\u7684\u5207\u7247\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sub_array = array[0:2, 1:3]<\/p>\n<p>print(sub_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6. \u5e38\u7528\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u64cd\u4f5c\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6c42\u548c<\/p>\n<p>print(np.sum(array))<\/p>\n<h2><strong>\u6c42\u5747\u503c<\/strong><\/h2>\n<p>print(np.mean(array))<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>array2 = np.array([<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4],<\/p>\n<p>    [5, 6]<\/p>\n<p>])<\/p>\n<p>result = np.dot(array, array2)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas DataFrame<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3aDataFrame\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Pandas<\/h4>\n<\/p>\n<p><p>\u4f60\u9700\u8981\u5148\u5b89\u88c5Pandas\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\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><h4>2. \u521b\u5efaDataFrame<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>DataFrame<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2aDataFrame\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame([<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>], columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8bbf\u95ee\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u548c\u5217\u540d\u6765\u8bbf\u95eeDataFrame\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(df.at[0, &#39;B&#39;])  # \u8f93\u51fa2<\/p>\n<p>print(df.at[2, &#39;C&#39;])  # \u8f93\u51fa9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u4fee\u6539\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u548c\u5217\u540d\u6765\u4fee\u6539DataFrame\u4e2d\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.at[0, &#39;B&#39;] = 20<\/p>\n<p>print(df.at[0, &#39;B&#39;])  # \u8f93\u51fa20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u5207\u7247\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>DataFrame\u652f\u6301\u5f3a\u5927\u7684\u5207\u7247\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sub_df = df.loc[0:1, [&#39;B&#39;, &#39;C&#39;]]<\/p>\n<p>print(sub_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6. \u5e38\u7528\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u64cd\u4f5c\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6c42\u548c<\/p>\n<p>print(df.sum())<\/p>\n<h2><strong>\u6c42\u5747\u503c<\/strong><\/h2>\n<p>print(df.mean())<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>df2 = pd.DataFrame([<\/p>\n<p>    [1, 2],<\/p>\n<p>    [3, 4],<\/p>\n<p>    [5, 6]<\/p>\n<p>], columns=[&#39;X&#39;, &#39;Y&#39;])<\/p>\n<p>result = df.dot(df2)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4f7f\u7528\u4e8c\u7ef4\u6570\u7ec4\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u5305\u62ec\u4f7f\u7528\u5217\u8868\u3001NumPy\u6570\u7ec4\u548cPandas DataFrame\u3002\u6bcf\u79cd\u65b9\u5f0f\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u4f7f\u7528\u5217\u8868\u9002\u5408\u5c0f\u89c4\u6a21\u6570\u636e\u548c\u7b80\u5355\u64cd\u4f5c\uff0c\u4f7f\u7528NumPy\u6570\u7ec4\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u548c\u590d\u6742\u6570\u5b66\u8fd0\u7b97\uff0c\u4f7f\u7528Pandas DataFrame\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728Python\u4e2d\u4f7f\u7528\u4e8c\u7ef4\u6570\u7ec4\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a2&#215;3\u7684\u4e8c\u7ef4\u6570\u7ec4\uff1a  <\/p>\n<pre><code class=\"language-python\">array_2d = [[1, 2, 3], [4, 5, 6]]\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c<code>array_2d[0]<\/code>\u5c06\u8fd4\u56de\u7b2c\u4e00\u884c<code>[1, 2, 3]<\/code>\uff0c\u800c<code>array_2d[1]<\/code>\u5c06\u8fd4\u56de\u7b2c\u4e8c\u884c<code>[4, 5, 6]<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u548c\u4fee\u6539Python\u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u5143\u7d20\uff1f<\/strong><br \/>\u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u884c\u548c\u5217\u7684\u7d22\u5f15\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u8981\u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e8c\u5217\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528<code>array_2d[0][1]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>2<\/code>\u3002\u5982\u679c\u60f3\u8981\u4fee\u6539\u8be5\u5143\u7d20\uff0c\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\uff1a  <\/p>\n<pre><code class=\"language-python\">array_2d[0][1] = 10  # \u5c06\u7b2c\u4e8c\u5217\u7684\u503c\u6539\u4e3a10\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u904d\u5386\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u6253\u5370\u51fa\u6bcf\u4e2a\u5143\u7d20\uff1a  <\/p>\n<pre><code class=\"language-python\">for row in array_2d:\n    for element in row:\n        print(element)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u6bcf\u4e00\u884c\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u88ab\u8bbf\u95ee\u5230\uff0c\u53ef\u4ee5\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u6c42\u548c\u3001\u7b5b\u9009\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5982\u4f7f\u7528\u5217\u8868\u3001NumPy\u5e93\u6216Pandas\u5e93\u3002\u4f7f\u7528\u5217\u8868\u3001\u4f7f\u7528N [&hellip;]","protected":false},"author":3,"featured_media":1034212,"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\/1034207"}],"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=1034207"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1034207\/revisions"}],"predecessor-version":[{"id":1034216,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1034207\/revisions\/1034216"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1034212"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1034207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1034207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1034207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}