{"id":1113908,"date":"2025-01-08T17:52:17","date_gmt":"2025-01-08T09:52:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113908.html"},"modified":"2025-01-08T17:52:20","modified_gmt":"2025-01-08T09:52:20","slug":"%e5%a6%82%e4%bd%95%e5%b0%86array%e6%89%a9%e5%b1%95%e4%b8%80%e5%88%97python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1113908.html","title":{"rendered":"\u5982\u4f55\u5c06array\u6269\u5c55\u4e00\u5217python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075329\/166c29cb-ea05-43bc-aa1d-281ccc0891b9.webp\" alt=\"\u5982\u4f55\u5c06array\u6269\u5c55\u4e00\u5217python\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u7ec4\u6269\u5c55\u4e00\u5217\uff0c\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u7b49\u3002\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u3001\u7406\u89e3\u6570\u636e\u7684\u7ed3\u6784\u4ee5\u53ca\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u662f\u5173\u952e\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e76\u5bf9\u5176\u5c55\u5f00\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u5e93\u662f\u6269\u5c55\u6570\u7ec4\u7684\u4e00\u79cd\u5e38\u89c1\u65b9\u6cd5\u3002NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u7684\u652f\u6301\u548c\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u5e93\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528NumPy\u6269\u5c55\u6570\u7ec4\u4e00\u5217\u7684\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528NumPy\u6269\u5c55\u6570\u7ec4<\/p>\n<\/p>\n<p><p>1\u3001\u52a0\u8f7dNumPy\u5e93<\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u52a0\u8f7dNumPy\u5e93\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528pip\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\u811a\u672c\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><p>2\u3001\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u5e0c\u671b\u5728\u5176\u672b\u5c3e\u6dfb\u52a0\u4e00\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u521b\u5efa\u4e00\u4e2a\u65b0\u5217<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u5e0c\u671b\u6dfb\u52a0\u4e00\u5217\u5168\u4e3a\u96f6\u7684\u65b0\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">new_column = np.array([[0], [0], [0]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>4\u3001\u4f7f\u7528NumPy\u7684hstack\u65b9\u6cd5<\/p>\n<p>NumPy\u63d0\u4f9b\u4e86hstack\u65b9\u6cd5\u6765\u6c34\u5e73\u5806\u53e0\u6570\u7ec4\uff0c\u4ece\u800c\u5b9e\u73b0\u6dfb\u52a0\u65b0\u5217\u7684\u76ee\u7684\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">expanded_array = np.hstack((array, new_column))<\/p>\n<p>print(expanded_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65f6\uff0cexpanded_array\u5c06\u5305\u542b\u539f\u59cb\u6570\u7ec4\u4ee5\u53ca\u65b0\u6dfb\u52a0\u7684\u5217\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Pandas\u5e93\u6269\u5c55\u6570\u7ec4<\/p>\n<\/p>\n<p><p>1\u3001\u52a0\u8f7dPandas\u5e93<\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u52a0\u8f7dPandas\u5e93\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528pip\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\u811a\u672c\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><p>2\u3001\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5e76\u5e0c\u671b\u5728\u5176\u672b\u5c3e\u6dfb\u52a0\u4e00\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.DataFrame({&#39;A&#39;: [1, 4, 7], &#39;B&#39;: [2, 5, 8], &#39;C&#39;: [3, 6, 9]})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u6dfb\u52a0\u65b0\u5217<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u5e0c\u671b\u6dfb\u52a0\u4e00\u5217\u5168\u4e3a\u96f6\u7684\u65b0\u5217\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df[&#39;D&#39;] = 0<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65f6\uff0cDataFrame df\u5c06\u5305\u542b\u539f\u59cb\u6570\u7ec4\u4ee5\u53ca\u65b0\u6dfb\u52a0\u7684\u5217\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5176\u4ed6\u65b9\u6cd5\u6269\u5c55\u6570\u7ec4<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528\u5217\u8868\u64cd\u4f5c<\/p>\n<p>\u5982\u679c\u4e0d\u5e0c\u671b\u4f9d\u8d56\u5916\u90e8\u5e93\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7eafPython\u7684\u5217\u8868\u64cd\u4f5c\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>new_column = [0, 0, 0]<\/p>\n<p>for i in range(len(array)):<\/p>\n<p>    array[i].append(new_column[i])<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc<\/p>\n<p>\u540c\u6837\u5730\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>new_column = [0, 0, 0]<\/p>\n<p>expanded_array = [row + [new_column[i]] for i, row in enumerate(array)]<\/p>\n<p>print(expanded_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6027\u80fd\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u8003\u8651\u6570\u636e\u89c4\u6a21\u548c\u6027\u80fd\u3002\u5bf9\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\uff0c\u53ef\u4ee5\u9009\u62e9\u4efb\u610f\u4e00\u79cd\u65b9\u6cd5\uff1b\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\uff0c\u4f7f\u7528NumPy\u6216Pandas\u53ef\u80fd\u66f4\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4eec\u5728\u5e95\u5c42\u8fdb\u884c\u4e86\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u3001\u7406\u89e3\u6570\u636e\u7684\u7ed3\u6784\u4ee5\u53ca\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u662f\u5173\u952e\u3002<\/strong>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u7075\u6d3b\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u6269\u5c55\u6570\u7ec4\u3002\u65e0\u8bba\u662f\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u8fd8\u662f\u5176\u4ed6\u5e94\u7528\u573a\u666f\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u80fd\u591f\u6ee1\u8db3\u9700\u6c42\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e3a\u6570\u7ec4\u6dfb\u52a0\u65b0\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u6269\u5c55\u6570\u7ec4\u5e76\u6dfb\u52a0\u65b0\u5217\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7<code>np.column_stack()<\/code>\u51fd\u6570\u5c06\u65b0\u5217\u4e0e\u73b0\u6709\u6570\u7ec4\u7ed3\u5408\u8d77\u6765\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>np.hstack()<\/code>\u3002\u786e\u4fdd\u65b0\u5217\u7684\u7ef4\u5ea6\u4e0e\u539f\u59cb\u6570\u7ec4\u5339\u914d\uff0c\u4ee5\u907f\u514d\u7ef4\u5ea6\u4e0d\u4e00\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728\u6269\u5c55\u6570\u7ec4\u5217\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u6269\u5c55\u6570\u7ec4\u65f6\uff0c\u786e\u4fdd\u65b0\u5217\u7684\u6570\u636e\u7c7b\u578b\u4e0e\u539f\u6570\u7ec4\u4e00\u81f4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u6b64\u5916\uff0c\u68c0\u67e5\u65b0\u5217\u7684\u957f\u5ea6\u662f\u5426\u4e0e\u539f\u6570\u7ec4\u7684\u884c\u6570\u76f8\u540c\uff0c\u4ee5\u786e\u4fdd\u5408\u5e76\u64cd\u4f5c\u987a\u5229\u8fdb\u884c\u3002\u5982\u679c\u539f\u6570\u7ec4\u662f\u4e8c\u7ef4\u7684\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u65b0\u5217\u7684\u7ef4\u5ea6\u6765\u8fbe\u5230\u517c\u5bb9\u6027\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5728Python\u4e2d\u6dfb\u52a0\u5217\u5230\u5217\u8868\u6216\u6570\u7ec4\u4e2d\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528NumPy\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u901a\u8fc7\u521b\u5efaDataFrame\u5e76\u4f7f\u7528<code>assign()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u6dfb\u52a0\u65b0\u5217\u3002\u6b64\u5916\uff0cPython\u7684\u539f\u751f\u5217\u8868\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u5faa\u73af\u6216\u5217\u8868\u63a8\u5bfc\u6765\u6784\u5efa\u65b0\u5217\uff0c\u5e76\u5c06\u5176\u9644\u52a0\u5230\u73b0\u6709\u7684\u6570\u636e\u7ed3\u6784\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u7ec4\u6269\u5c55\u4e00\u5217\uff0c\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u7b49\u3002\u4f7f\u7528NumPy\u5e93\u3001P [&hellip;]","protected":false},"author":3,"featured_media":1113918,"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\/1113908"}],"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=1113908"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113908\/revisions"}],"predecessor-version":[{"id":1113920,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113908\/revisions\/1113920"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1113918"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1113908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1113908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1113908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}