{"id":1140373,"date":"2025-01-08T22:20:07","date_gmt":"2025-01-08T14:20:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1140373.html"},"modified":"2025-01-08T22:20:09","modified_gmt":"2025-01-08T14:20:09","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba%e6%95%b0%e7%bb%84%e6%9c%80%e5%90%8e%e4%b8%80%e5%88%97%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1140373.html","title":{"rendered":"python\u5982\u4f55\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103342\/78f39c69-7392-42e2-bf7d-cb306e6b5a5a.webp\" alt=\"python\u5982\u4f55\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>Python \u5982\u4f55\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u6570\u7ec4\u4e2d\u53d6\u51fa\u6700\u540e\u4e00\u5217\u3002<strong>\u65b9\u6cd5\u662f\uff1a\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528pandas\u5e93<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u7684\u65b9\u6cd5\u6700\u4e3a\u9ad8\u6548\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528NumPy\u5e93\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528NumPy\u5e93\uff1a<\/strong><\/p>\n<p>NumPy\u5e93\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u7684\u5f3a\u5927\u5de5\u5177\u3002\u8981\u53d6\u51fa\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u8fdb\u884c\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>array[:, -1]<\/code> \u6765\u53d6\u51fa\u6700\u540e\u4e00\u5217\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([[1, 2, 3],<\/p>\n<p>                  [4, 5, 6],<\/p>\n<p>                  [7, 8, 9]])<\/p>\n<h2><strong>\u53d6\u51fa\u6700\u540e\u4e00\u5217<\/strong><\/h2>\n<p>last_column = array[:, -1]<\/p>\n<p>print(last_column)  # \u8f93\u51fa: [3 6 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u4e0a\u8ff0\u65b9\u6cd5\u4ee5\u53ca\u5176\u4ed6\u76f8\u5173\u7684\u5b9e\u73b0\u548c\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><h4>1\u3001NumPy\u5e93\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u652f\u6301\u5927\u591a\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u7684\u529f\u80fd\uff0c\u540c\u65f6\u4e5f\u5305\u542b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\u5e93\u3002NumPy\u7684\u6838\u5fc3\u662fndarray\u5bf9\u8c61\uff0c\u5b83\u662f\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4\uff0c\u7528\u4e8e\u5b58\u50a8\u540c\u7c7b\u578b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u521b\u5efaNumPy\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5NumPy\u5e93\uff0c\u5e76\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u5b89\u88c5\u548c\u521b\u5efa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5NumPy\u5e93<\/p>\n<p>!pip install numpy<\/p>\n<h2><strong>\u5bfc\u5165NumPy\u5e93<\/strong><\/h2>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([[1, 2, 3],<\/p>\n<p>                  [4, 5, 6],<\/p>\n<p>                  [7, 8, 9]])<\/p>\n<p>print(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u53d6\u51fa\u6700\u540e\u4e00\u5217<\/h4>\n<\/p>\n<p><p>\u8981\u53d6\u51faNumPy\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u7ec4\u5207\u7247\u64cd\u4f5c\u3002\u5207\u7247\u64cd\u4f5c\u5141\u8bb8\u6211\u4eec\u901a\u8fc7\u7d22\u5f15\u6765\u8bbf\u95ee\u6570\u7ec4\u7684\u5b50\u96c6\u3002\u5bf9\u4e8e\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5207\u7247\u64cd\u4f5c\u7684\u8bed\u6cd5\u4e3a <code>array[start_row:end_row, start_col:end_col]<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8d1f\u7d22\u5f15 <code>-1<\/code> \u6765\u8868\u793a\u6700\u540e\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53d6\u51fa\u6700\u540e\u4e00\u5217<\/p>\n<p>last_column = array[:, -1]<\/p>\n<p>print(last_column)  # \u8f93\u51fa: [3 6 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u53d6\u51faNumPy\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><h4>1\u3001pandas\u5e93\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u7684\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86DataFrame\u548cSeries\u4e24\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5206\u522b\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u548c\u4e00\u7ef4\u6570\u636e\u3002pandas\u5e93\u5728\u5904\u7406\u6570\u636e\u65f6\u975e\u5e38\u7075\u6d3b\u4e14\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u521b\u5efapandas DataFrame<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528pandas\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5pandas\u5e93\uff0c\u5e76\u521b\u5efa\u4e00\u4e2aDataFrame\u5bf9\u8c61\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u5b89\u88c5\u548c\u521b\u5efa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b89\u88c5pandas\u5e93<\/p>\n<p>!pip install pandas<\/p>\n<h2><strong>\u5bfc\u5165pandas\u5e93<\/strong><\/h2>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 4, 7],<\/p>\n<p>        &#39;B&#39;: [2, 5, 8],<\/p>\n<p>        &#39;C&#39;: [3, 6, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u53d6\u51fa\u6700\u540e\u4e00\u5217<\/h4>\n<\/p>\n<p><p>\u8981\u53d6\u51faDataFrame\u7684\u6700\u540e\u4e00\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528DataFrame\u7684 <code>iloc<\/code> \u5c5e\u6027\u3002 <code>iloc<\/code> \u5c5e\u6027\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u7d22\u5f15\u8fdb\u884c\u884c\u5217\u8bbf\u95ee\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8d1f\u7d22\u5f15 <code>-1<\/code> \u6765\u8868\u793a\u6700\u540e\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53d6\u51fa\u6700\u540e\u4e00\u5217<\/p>\n<p>last_column = df.iloc[:, -1]<\/p>\n<p>print(last_column)  # \u8f93\u51fa: 0    3<\/p>\n<p>                    #      1    6<\/p>\n<p>                    #      2    9<\/p>\n<p>                    # Name: C, dtype: int64<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u53d6\u51faDataFrame\u7684\u6700\u540e\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7eafPython\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4f7f\u7528NumPy\u548cpandas\u5e93\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u548c\u6570\u636e\u6846\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528\u7eafPython\u4ee3\u7801\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4e8c\u7ef4\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4e8c\u7ef4\u5217\u8868\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u4e8c\u7ef4\u6570\u636e\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u89e3\u6790\u6765\u53d6\u51fa\u6700\u540e\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u4e8c\u7ef4\u5217\u8868<\/p>\n<p>array = [[1, 2, 3],<\/p>\n<p>         [4, 5, 6],<\/p>\n<p>         [7, 8, 9]]<\/p>\n<h2><strong>\u53d6\u51fa\u6700\u540e\u4e00\u5217<\/strong><\/h2>\n<p>last_column = [row[-1] for row in array]<\/p>\n<p>print(last_column)  # \u8f93\u51fa: [3, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u904d\u5386\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5217\u8868\u6765\u53d6\u51fa\u6700\u540e\u4e00\u5217\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u4e8c\u7ef4\u5217\u8868<\/p>\n<p>array = [[1, 2, 3],<\/p>\n<p>         [4, 5, 6],<\/p>\n<p>         [7, 8, 9]]<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e00\u4e2a\u7a7a\u5217\u8868\u6765\u5b58\u50a8\u6700\u540e\u4e00\u5217<\/strong><\/h2>\n<p>last_column = []<\/p>\n<h2><strong>\u904d\u5386\u4e8c\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>for row in array:<\/p>\n<p>    last_column.append(row[-1])<\/p>\n<p>print(last_column)  # \u8f93\u51fa: [3, 6, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7eafPython\u5b9e\u73b0\u4ece\u4e8c\u7ef4\u5217\u8868\u4e2d\u53d6\u51fa\u6700\u540e\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u4ece\u6570\u636e\u96c6\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u5217\u8fdb\u884c\u5206\u6790\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u7279\u5f81\u7684\u9500\u552e\u6570\u636e\u96c6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u63d0\u53d6\u9500\u552e\u989d\u8fd9\u4e00\u5217\u6765\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e\u96c6<\/p>\n<p>data = {&#39;Product&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;],<\/p>\n<p>        &#39;Price&#39;: [100, 200, 300],<\/p>\n<p>        &#39;Sales&#39;: [150, 250, 350]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u63d0\u53d6\u9500\u552e\u989d\u8fd9\u4e00\u5217<\/strong><\/h2>\n<p>sales_column = df[&#39;Sales&#39;]<\/p>\n<p>print(sales_column)  # \u8f93\u51fa: 0    150<\/p>\n<p>                     #      1    250<\/p>\n<p>                     #      2    350<\/p>\n<p>                     # Name: Sales, dtype: int64<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/h4>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u7279\u5f81\u9009\u62e9\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u6570\u636e\u96c6\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u7279\u5f81\u5217\u6765\u8bad\u7ec3\u6a21\u578b\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u7279\u5f81\u7684\u6570\u636e\u96c6\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u63d0\u53d6\u6700\u540e\u4e00\u5217\u7279\u5f81\u4f5c\u4e3a\u6a21\u578b\u7684\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e\u96c6<\/p>\n<p>data = {&#39;Feature1&#39;: [1, 2, 3],<\/p>\n<p>        &#39;Feature2&#39;: [4, 5, 6],<\/p>\n<p>        &#39;Feature3&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u63d0\u53d6\u6700\u540e\u4e00\u5217\u7279\u5f81<\/strong><\/h2>\n<p>last_feature = df.iloc[:, -1]<\/p>\n<p>print(last_feature)  # \u8f93\u51fa: 0    7<\/p>\n<p>                     #      1    8<\/p>\n<p>                     #      2    9<\/p>\n<p>                     # Name: Feature3, dtype: int64<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6570\u636e\u5e93\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u67e5\u8be2\u7ed3\u679c\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u5217\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u4ece\u6570\u636e\u5e93\u4e2d\u67e5\u8be2\u51fa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5217\u7684\u6570\u636e\u96c6\uff0c\u6211\u4eec\u9700\u8981\u63d0\u53d6\u6700\u540e\u4e00\u5217\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;:memory:&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u8868<\/strong><\/h2>\n<p>cursor.execute(&#39;&#39;&#39;CREATE TABLE sales<\/p>\n<p>                  (product TEXT, price INTEGER, sales INTEGER)&#39;&#39;&#39;)<\/p>\n<h2><strong>\u63d2\u5165\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>cursor.executemany(&#39;INSERT INTO sales VALUES (?, ?, ?)&#39;,<\/p>\n<p>                   [(&#39;A&#39;, 100, 150),<\/p>\n<p>                    (&#39;B&#39;, 200, 250),<\/p>\n<p>                    (&#39;C&#39;, 300, 350)])<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM sales&#39;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u63d0\u53d6\u6700\u540e\u4e00\u5217<\/strong><\/h2>\n<p>last_column = [row[-1] for row in rows]<\/p>\n<p>print(last_column)  # \u8f93\u51fa: [150, 250, 350]<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\u4e2d\u63d0\u53d6\u6700\u540e\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u53d6\u51fa\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u5728\u6027\u80fd\u4e0a\u53ef\u80fd\u5b58\u5728\u5dee\u5f02\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u6bd4\u8f83\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001NumPy\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>NumPy\u65b9\u6cd5\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u65f6\u975e\u5e38\u9ad8\u6548\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u6765\u6d4b\u8bd5\u5176\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import timeit<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u89c4\u6a21\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.random.rand(1000000, 10)<\/p>\n<h2><strong>\u6d4b\u8bd5NumPy\u65b9\u6cd5\u7684\u6027\u80fd<\/strong><\/h2>\n<p>time_taken = timeit.timeit(&#39;array[:, -1]&#39;, globals=globals(), number=1000)<\/p>\n<p>print(f&#39;NumPy\u65b9\u6cd5\u8017\u65f6: {time_taken:.6f} \u79d2&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001pandas\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>pandas\u65b9\u6cd5\u5728\u5904\u7406\u5927\u89c4\u6a21DataFrame\u65f6\u4e5f\u975e\u5e38\u9ad8\u6548\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u6765\u6d4b\u8bd5\u5176\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import timeit<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u89c4\u6a21DataFrame<\/strong><\/h2>\n<p>data = {f&#39;Feature{i}&#39;: np.random.rand(1000000) for i in range(10)}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6d4b\u8bd5pandas\u65b9\u6cd5\u7684\u6027\u80fd<\/strong><\/h2>\n<p>time_taken = timeit.timeit(&#39;df.iloc[:, -1]&#39;, globals=globals(), number=1000)<\/p>\n<p>print(f&#39;pandas\u65b9\u6cd5\u8017\u65f6: {time_taken:.6f} \u79d2&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7eafPython\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u7eafPython\u65b9\u6cd5\u5728\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u65f6\u6027\u80fd\u5c1a\u53ef\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u53ef\u80fd\u5b58\u5728\u6027\u80fd\u74f6\u9888\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>timeit<\/code> \u6a21\u5757\u6765\u6d4b\u8bd5\u5176\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5927\u89c4\u6a21\u4e8c\u7ef4\u5217\u8868<\/strong><\/h2>\n<p>array = [list(np.random.rand(10)) for _ in range(1000000)]<\/p>\n<h2><strong>\u6d4b\u8bd5\u7eafPython\u65b9\u6cd5\u7684\u6027\u80fd<\/strong><\/h2>\n<p>time_taken = timeit.timeit(&#39;[row[-1] for row in array]&#39;, globals=globals(), number=1000)<\/p>\n<p>print(f&#39;\u7eafPython\u65b9\u6cd5\u8017\u65f6: {time_taken:.6f} \u79d2&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6027\u80fd\u6d4b\u8bd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u51fa\u4ee5\u4e0b\u7ed3\u8bba\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>NumPy\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\uff0c\u6027\u80fd\u4f18\u5f02\u3002<\/li>\n<li><strong>pandas\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21DataFrame\uff0c\u6027\u80fd\u4f18\u5f02\u3002<\/li>\n<li><strong>\u7eafPython\u65b9\u6cd5<\/strong>\uff1a\u9002\u7528\u4e8e\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u6027\u80fd\u8f83\u5dee\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d6\u51fa\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4f9b\u9009\u62e9\u3002<strong>\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528\u7eafPython\u5b9e\u73b0<\/strong>\u662f\u5e38\u89c1\u7684\u4e09\u79cd\u65b9\u6cd5\u3002NumPy\u548cpandas\u5e93\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u8868\u73b0\u4f18\u5f02\uff0c\u800c\u7eafPython\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528NumPy\u5e93\u3001pandas\u5e93\u548c\u7eafPython\u4ee3\u7801\u6765\u53d6\u51fa\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\uff0c\u5e76\u63a2\u8ba8\u4e86\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5e94\u7528\u573a\u666f\u548c\u6027\u80fd\u6bd4\u8f83\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\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\u63d0\u53d6\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5904\u7406\u6570\u7ec4\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u65b9\u5f0f\u8f7b\u677e\u63d0\u53d6\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4\narray = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n\n# \u63d0\u53d6\u6700\u540e\u4e00\u5217\nlast_column = array[:, -1]\nprint(last_column)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa\u6570\u7ec4\u7684\u6700\u540e\u4e00\u5217\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Pandas\u65f6\uff0c\u5982\u4f55\u83b7\u53d6DataFrame\u7684\u6700\u540e\u4e00\u5217\uff1f<\/strong><br \/>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u4f7f\u7528DataFrame\u65f6\u53ef\u4ee5\u901a\u8fc7<code>.iloc<\/code>\u5c5e\u6027\u8f7b\u677e\u83b7\u53d6\u6700\u540e\u4e00\u5217\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame\ndf = pd.DataFrame({&#39;A&#39;: [1, 4, 7], &#39;B&#39;: [2, 5, 8], &#39;C&#39;: [3, 6, 9]})\n\n# \u63d0\u53d6\u6700\u540e\u4e00\u5217\nlast_column = df.iloc[:, -1]\nprint(last_column)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u8fd4\u56deDataFrame\u7684\u6700\u540e\u4e00\u5217\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6570\u636e\u5e93\u4e2d\u67e5\u8be2\u7279\u5b9a\u5b57\u6bb5\u7684\u6700\u540e\u4e00\u5217\uff1f<\/strong><br \/>\u5728\u4f7f\u7528SQL\u67e5\u8be2\u6570\u636e\u5e93\u65f6\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u9009\u62e9\u7279\u5b9a\u7684\u5217\u6765\u83b7\u53d6\u6700\u540e\u4e00\u5217\u3002\u5728SQL\u4e2d\uff0c\u901a\u5e38\u6ca1\u6709\u76f4\u63a5\u7684\u201c\u6700\u540e\u4e00\u5217\u201d\u6982\u5ff5\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528<code>ORDER BY<\/code>\u548c<code>LIMIT<\/code>\u7b49\u8bed\u53e5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-sql\">SELECT column_name FROM table_name ORDER BY id DESC LIMIT 1;\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u67e5\u8be2\u4e2d\uff0c\u786e\u4fdd\u66ff\u6362<code>column_name<\/code>\u548c<code>table_name<\/code>\u4e3a\u5b9e\u9645\u7684\u5b57\u6bb5\u540d\u548c\u8868\u540d\u3002\u8fd9\u6837\u53ef\u4ee5\u83b7\u53d6\u8868\u4e2d\u6700\u540e\u4e00\u6761\u8bb0\u5f55\u7684\u7279\u5b9a\u5b57\u6bb5\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5982\u4f55\u53d6\u51fa\u6570\u7ec4\u6700\u540e\u4e00\u5217\u6570\u636e\u5e93 \u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u4ece\u6570\u7ec4\u4e2d\u53d6\u51fa\u6700\u540e\u4e00\u5217\u3002\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1140380,"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\/1140373"}],"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=1140373"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140373\/revisions"}],"predecessor-version":[{"id":1140381,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140373\/revisions\/1140381"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1140380"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1140373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1140373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1140373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}