{"id":1145000,"date":"2025-01-08T23:04:36","date_gmt":"2025-01-08T15:04:36","guid":{"rendered":""},"modified":"2025-01-08T23:04:38","modified_gmt":"2025-01-08T15:04:38","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e7%bb%84%e7%9a%84%e4%b8%80%e5%88%97%e6%8f%90%e5%8f%96%e5%87%ba%e6%9d%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1145000.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6570\u7ec4\u7684\u4e00\u5217\u63d0\u53d6\u51fa\u6765"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181815\/c9b9962e-7f37-4081-8d50-ffabf86e39b9.webp\" alt=\"python\u5982\u4f55\u5c06\u6570\u7ec4\u7684\u4e00\u5217\u63d0\u53d6\u51fa\u6765\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u63d0\u53d6\u6570\u7ec4\u7684\u4e00\u5217\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u4ee5\u53ca\u5217\u8868\u89e3\u6790\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u548c\u5e94\u7528\u573a\u666f\u3002<strong>NumPy\u5e93\u6027\u80fd\u9ad8\u3001Pandas\u5e93\u529f\u80fd\u5f3a\u5927\u3001\u5217\u8868\u89e3\u6790\u7075\u6d3b<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bbaNumPy\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528NumPy\u5e93<\/h2>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u975e\u5e38\u6d41\u884c\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u65b9\u6cd5\u3002\u901a\u8fc7NumPy\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u63d0\u53d6\u6570\u7ec4\u4e2d\u7684\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><h3>1.1\u3001\u5b89\u88c5\u4e0e\u5f15\u5165NumPy\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728Python\u4ee3\u7801\u4e2d\u5f15\u5165NumPy\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>1.2\u3001\u521b\u5efaNumPy\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u6f14\u793a\u5982\u4f55\u63d0\u53d6\u6570\u7ec4\u7684\u4e00\u5217\uff0c\u6211\u4eec\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\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><h3>1.3\u3001\u63d0\u53d6\u6570\u7ec4\u7684\u4e00\u5217<\/h3>\n<\/p>\n<p><p>\u5728NumPy\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u63d0\u53d6\u6570\u7ec4\u7684\u4e00\u5217\u3002\u4f8b\u5982\uff0c\u8981\u63d0\u53d6\u7b2c\u4e8c\u5217\uff08\u7d22\u5f15\u4e3a1\uff09\u7684\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">column = array[:, 1]<\/p>\n<p>print(column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>[2 5 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>array[:, 1]<\/code>\u8868\u793a\u63d0\u53d6\u6240\u6709\u884c\u7684\u7b2c\u4e8c\u5217\u6570\u636e\u3002<strong>\u8fd9\u79cd\u5207\u7247\u64cd\u4f5c\u975e\u5e38\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><h3>1.4\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u4f8b\u5982\u6570\u7ec4\u4e2d\u5305\u542b\u7f3a\u5931\u503c\u6216\u9700\u8981\u5bf9\u63d0\u53d6\u7684\u5217\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u63d0\u4f9b\u7684\u5176\u4ed6\u51fd\u6570\u6765\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5904\u7406\u7f3a\u5931\u503c<\/strong>\uff1a\u4f7f\u7528<code>np.isnan()<\/code>\u51fd\u6570\u68c0\u67e5\u7f3a\u5931\u503c\uff0c\u5e76\u7528<code>np.nan_to_num()<\/code>\u51fd\u6570\u66ff\u6362\u7f3a\u5931\u503c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">array = np.array([[1, 2, 3], [4, np.nan, 6], [7, 8, 9]])<\/p>\n<p>column = array[:, 1]<\/p>\n<p>column = np.nan_to_num(column, nan=-1)  # \u5c06\u7f3a\u5931\u503c\u66ff\u6362\u4e3a-1<\/p>\n<p>print(column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5bf9\u63d0\u53d6\u7684\u5217\u8fdb\u884c\u8ba1\u7b97<\/strong>\uff1a\u4f7f\u7528NumPy\u63d0\u4f9b\u7684\u5404\u79cd\u6570\u5b66\u51fd\u6570\u5bf9\u63d0\u53d6\u7684\u5217\u8fdb\u884c\u8ba1\u7b97\uff0c\u4f8b\u5982\u6c42\u548c\u3001\u5e73\u5747\u503c\u7b49\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">column_sum = np.sum(column)<\/p>\n<p>column_mean = np.mean(column)<\/p>\n<p>print(f&quot;Sum: {column_sum}, Mean: {column_mean}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528Pandas\u5e93<\/h2>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86DataFrame\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5904\u7406\u4e8c\u7ef4\u6570\u636e\uff0c\u5e76\u652f\u6301\u591a\u79cd\u6570\u636e\u64cd\u4f5c\u65b9\u6cd5\u3002\u4f7f\u7528Pandas\u63d0\u53d6\u6570\u7ec4\u7684\u4e00\u5217\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h3>2.1\u3001\u5b89\u88c5\u4e0e\u5f15\u5165Pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Pandas\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728Python\u4ee3\u7801\u4e2d\u5f15\u5165Pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.2\u3001\u521b\u5efaPandas DataFrame<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u6f14\u793a\u5982\u4f55\u63d0\u53d6\u6570\u7ec4\u7684\u4e00\u5217\uff0c\u6211\u4eec\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2aPandas DataFrame\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {&#39;A&#39;: [1, 4, 7], &#39;B&#39;: [2, 5, 8], &#39;C&#39;: [3, 6, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2.3\u3001\u63d0\u53d6DataFrame\u7684\u4e00\u5217<\/h3>\n<\/p>\n<p><p>\u5728Pandas\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5217\u540d\u6765\u63d0\u53d6DataFrame\u4e2d\u7684\u4e00\u5217\u3002\u4f8b\u5982\uff0c\u8981\u63d0\u53d6\u5217<code>B<\/code>\u7684\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">column = df[&#39;B&#39;]<\/p>\n<p>print(column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>0    2<\/p>\n<p>1    5<\/p>\n<p>2    8<\/p>\n<p>Name: B, dtype: int64<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528Pandas\u63d0\u53d6\u5217\u7684\u6570\u636e\u7ed3\u6784\u662f<code>Series<\/code>\uff0c\u5b83\u662fPandas\u4e2d\u4e00\u7ef4\u6570\u636e\u7684\u57fa\u672c\u5355\u4f4d\u3002<\/strong><\/p>\n<\/p>\n<p><h3>2.4\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u4e0eNumPy\u7c7b\u4f3c\uff0cPandas\u4e5f\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u6765\u5904\u7406\u5404\u79cd\u7279\u6b8a\u60c5\u51b5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5904\u7406\u7f3a\u5931\u503c<\/strong>\uff1a\u4f7f\u7528<code>fillna()<\/code>\u51fd\u6570\u66ff\u6362\u7f3a\u5931\u503c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">df = pd.DataFrame({&#39;A&#39;: [1, 4, 7], &#39;B&#39;: [2, None, 8], &#39;C&#39;: [3, 6, 9]})<\/p>\n<p>column = df[&#39;B&#39;].fillna(-1)  # \u5c06\u7f3a\u5931\u503c\u66ff\u6362\u4e3a-1<\/p>\n<p>print(column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5bf9\u63d0\u53d6\u7684\u5217\u8fdb\u884c\u8ba1\u7b97<\/strong>\uff1a\u4f7f\u7528Pandas\u63d0\u4f9b\u7684\u5404\u79cd\u51fd\u6570\u5bf9\u63d0\u53d6\u7684\u5217\u8fdb\u884c\u8ba1\u7b97\uff0c\u4f8b\u5982\u6c42\u548c\u3001\u5e73\u5747\u503c\u7b49\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">column_sum = column.sum()<\/p>\n<p>column_mean = column.mean()<\/p>\n<p>print(f&quot;Sum: {column_sum}, Mean: {column_mean}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h2>\n<\/p>\n<p><p>\u5217\u8868\u89e3\u6790\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u7684\u7279\u6027\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\u3002\u5728\u5904\u7406\u4e8c\u7ef4\u5217\u8868\u65f6\uff0c\u5217\u8868\u89e3\u6790\u4e5f\u53ef\u4ee5\u7528\u6765\u63d0\u53d6\u6570\u7ec4\u4e2d\u7684\u4e00\u5217\u3002<\/p>\n<\/p>\n<p><h3>3.1\u3001\u521b\u5efa\u4e8c\u7ef4\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u6f14\u793a\u5982\u4f55\u63d0\u53d6\u6570\u7ec4\u7684\u4e00\u5217\uff0c\u6211\u4eec\u9700\u8981\u5148\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2\u3001\u63d0\u53d6\u5217\u8868\u7684\u4e00\u5217<\/h3>\n<\/p>\n<p><p>\u5728\u5217\u8868\u89e3\u6790\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u63d0\u53d6\u4e8c\u7ef4\u5217\u8868\u4e2d\u7684\u4e00\u5217\u3002\u4f8b\u5982\uff0c\u8981\u63d0\u53d6\u7b2c\u4e8c\u5217\uff08\u7d22\u5f15\u4e3a1\uff09\u7684\u6570\u636e\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">column = [row[1] for row in array]<\/p>\n<p>print(column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code>[2, 5, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5217\u8868\u89e3\u6790\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u7684\u7b80\u6d01\u6027\u548c\u7075\u6d3b\u6027\uff0c\u9002\u5408\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u548c\u7b80\u5355\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><h3>3.3\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5904\u7406\u7f3a\u5931\u503c<\/strong>\uff1a\u53ef\u4ee5\u5728\u5217\u8868\u89e3\u6790\u4e2d\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\u6765\u5904\u7406\u7f3a\u5931\u503c\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">array = [[1, 2, 3], [4, None, 6], [7, 8, 9]]<\/p>\n<p>column = [row[1] if row[1] is not None else -1 for row in array]  # \u5c06\u7f3a\u5931\u503c\u66ff\u6362\u4e3a-1<\/p>\n<p>print(column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5bf9\u63d0\u53d6\u7684\u5217\u8fdb\u884c\u8ba1\u7b97<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684\u51fd\u6570\u5bf9\u63d0\u53d6\u7684\u5217\u8fdb\u884c\u8ba1\u7b97\uff0c\u4f8b\u5982\u6c42\u548c\u3001\u5e73\u5747\u503c\u7b49\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">column_sum = sum(column)<\/p>\n<p>column_mean = sum(column) \/ len(column)<\/p>\n<p>print(f&quot;Sum: {column_sum}, Mean: {column_mean}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u5e94\u7528\u573a\u666f\u4e0e\u6027\u80fd\u6bd4\u8f83<\/h2>\n<\/p>\n<p><h3>4.1\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<ol>\n<li><strong>NumPy\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u503c\u8ba1\u7b97\u548c\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u79d1\u5b66\u8ba1\u7b97\u3001\u56fe\u50cf\u5904\u7406\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u9886\u57df\u3002<\/li>\n<li><strong>Pandas\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u573a\u666f\uff0c\u7279\u522b\u662f\u9700\u8981\u5904\u7406\u5e26\u6709\u6807\u7b7e\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u91d1\u878d\u6570\u636e\u5206\u6790\u3001\u6570\u636e\u9884\u5904\u7406\u3001\u6570\u636e\u53ef\u89c6\u5316\u7b49\u9886\u57df\u3002<\/li>\n<li><strong>\u5217\u8868\u89e3\u6790<\/strong>\uff1a\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u548c\u7b80\u5355\u64cd\u4f5c\u7684\u573a\u666f\u3002\u4f8b\u5982\uff0c\u5feb\u901f\u63d0\u53d6\u6570\u636e\u3001\u6570\u636e\u8f6c\u6362\u7b49\u3002<\/li>\n<\/ol>\n<p><h3>4.2\u3001\u6027\u80fd\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0cNumPy\u5e93\u548cPandas\u5e93\u7684\u6027\u80fd\u901a\u5e38\u4f18\u4e8e\u5217\u8868\u89e3\u6790\u3002\u8fd9\u662f\u56e0\u4e3aNumPy\u548cPandas\u90fd\u8fdb\u884c\u4e86\u5e95\u5c42\u4f18\u5316\uff0c\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u548cDataFrame\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6027\u80fd\u6bd4\u8f83\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u521b\u5efa\u5927\u89c4\u6a21\u6570\u636e<\/strong><\/h2>\n<p>array = np.random.rand(1000000, 3).tolist()<\/p>\n<h2><strong>\u5217\u8868\u89e3\u6790<\/strong><\/h2>\n<p>start_time = time.time()<\/p>\n<p>column = [row[1] for row in array]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;\u5217\u8868\u89e3\u6790\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<h2><strong>NumPy<\/strong><\/h2>\n<p>array_np = np.array(array)<\/p>\n<p>start_time = time.time()<\/p>\n<p>column_np = array_np[:, 1]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;NumPy\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<h2><strong>Pandas<\/strong><\/h2>\n<p>df = pd.DataFrame(array, columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;])<\/p>\n<p>start_time = time.time()<\/p>\n<p>column_pd = df[&#39;B&#39;]<\/p>\n<p>end_time = time.time()<\/p>\n<p>print(f&quot;Pandas\u8017\u65f6: {end_time - start_time} \u79d2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u901a\u5e38\u60c5\u51b5\u4e0b\uff0cNumPy\u548cPandas\u7684\u6027\u80fd\u4f1a\u660e\u663e\u4f18\u4e8e\u5217\u8868\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u63d0\u53d6\u6570\u7ec4\u7684\u4e00\u5217\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u4ee5\u53ca\u5217\u8868\u89e3\u6790\u3002<strong>NumPy\u5e93\u6027\u80fd\u9ad8\u3001Pandas\u5e93\u529f\u80fd\u5f3a\u5927\u3001\u5217\u8868\u89e3\u6790\u7075\u6d3b<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6570\u636e\u89c4\u6a21\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>NumPy\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u9700\u8981\u8fdb\u884c\u5927\u91cf\u6570\u503c\u8ba1\u7b97\u548c\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u573a\u666f\u3002<\/li>\n<li><strong>Pandas\u5e93<\/strong>\uff1a\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u573a\u666f\uff0c\u7279\u522b\u662f\u9700\u8981\u5904\u7406\u5e26\u6709\u6807\u7b7e\u7684\u6570\u636e\u3002<\/li>\n<li><strong>\u5217\u8868\u89e3\u6790<\/strong>\uff1a\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u6570\u636e\u548c\u7b80\u5355\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><p>\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\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\u7279\u5b9a\u5217\uff1f<\/strong><br \/>\u8981\u4ece\u6570\u7ec4\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u6765\u5904\u7406\u6570\u7ec4\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5NumPy\u5e93\u3002\u7136\u540e\uff0c\u901a\u8fc7\u6307\u5b9a\u5217\u7684\u7d22\u5f15\u5373\u53ef\u8f7b\u677e\u63d0\u53d6\u6240\u9700\u7684\u5217\u3002\u4f8b\u5982\uff0c<code>array[:, column_index]<\/code>\u53ef\u4ee5\u63d0\u53d6\u6307\u5b9a\u5217\uff0c<code>column_index<\/code>\u4e3a\u5217\u7684\u7d22\u5f15\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u64cd\u4f5c\u6570\u7ec4\u5e76\u63d0\u53d6\u5217\uff1f<\/strong><br \/>\u9664\u4e86NumPy\uff0cPandas\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u5e93\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u5728Pandas\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528DataFrame\u6765\u8868\u793a\u6570\u636e\u8868\uff0c\u5e76\u901a\u8fc7\u5217\u540d\u63d0\u53d6\u7279\u5b9a\u5217\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>dataframe[&#39;column_name&#39;]<\/code>\u53ef\u4ee5\u63d0\u53d6\u5bf9\u5e94\u5217\u7684\u6570\u636e\uff0c\u7075\u6d3b\u6027\u66f4\u9ad8\uff0c\u9002\u5408\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>\u63d0\u53d6\u5217\u65f6\u5982\u4f55\u5904\u7406\u591a\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\u5bf9\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u5982\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u548c\u5207\u7247\u6765\u63d0\u53d6\u7279\u5b9a\u7684\u5217\u3002\u4ee5NumPy\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>array[:, column_index]<\/code>\u6765\u63d0\u53d6\u6574\u4e2a\u5217\u3002\u5982\u679c\u8981\u63d0\u53d6\u591a\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>array[:, [col_index1, col_index2]]<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u540c\u65f6\u63d0\u53d6\u591a\u4e2a\u6307\u5b9a\u7684\u5217\uff0c\u65b9\u4fbf\u6570\u636e\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u63d0\u53d6\u6570\u7ec4\u7684\u4e00\u5217\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u4ee5\u53ca\u5217\u8868\u89e3\u6790\u3002\u672c\u6587\u5c06\u8be6 [&hellip;]","protected":false},"author":3,"featured_media":1145011,"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\/1145000"}],"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=1145000"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145000\/revisions"}],"predecessor-version":[{"id":1145014,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1145000\/revisions\/1145014"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1145011"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1145000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1145000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1145000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}