{"id":1131243,"date":"2025-01-08T20:47:00","date_gmt":"2025-01-08T12:47:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1131243.html"},"modified":"2025-01-08T20:47:04","modified_gmt":"2025-01-08T12:47:04","slug":"python-%e5%a6%82%e4%bd%95%e8%8e%b7%e5%be%97%e5%88%97%e8%a1%a8%e7%9a%84%e6%9f%90%e4%b8%80%e5%88%97%e5%85%83%e7%b4%a0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1131243.html","title":{"rendered":"python \u5982\u4f55\u83b7\u5f97\u5217\u8868\u7684\u67d0\u4e00\u5217\u5143\u7d20"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101101\/7b2be5fe-87c3-49a9-80bc-4fe6cf6cc37a.webp\" alt=\"python \u5982\u4f55\u83b7\u5f97\u5217\u8868\u7684\u67d0\u4e00\u5217\u5143\u7d20\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u83b7\u53d6\u5217\u8868\u7684\u67d0\u4e00\u5217\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001zip\u51fd\u6570\u3001pandas\u5e93\u7b49\u65b9\u6cd5<\/strong>\u3002\u5728\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5b50\u5217\u8868\u5e76\u63d0\u53d6\u6240\u9700\u7684\u5217\uff1b\u4f7f\u7528zip\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8f6c\u7f6e\u4e8c\u7ef4\u5217\u8868\uff1b\u800c\u4f7f\u7528pandas\u5e93\u65f6\uff0c\u53ef\u4ee5\u5229\u7528\u5176\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u6765\u7b80\u5316\u64cd\u4f5c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u751f\u6210\u5217\u8868\u7684\u65b9\u6cd5\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868\u4e00\u884c\u6570\u636e\uff0c\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u67d0\u4e00\u5217\u7684\u6240\u6709\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868data\uff0c\u60f3\u8981\u63d0\u53d6\u7b2c2\u5217\u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>column = [row[1] for row in data]<\/p>\n<p>print(column)  # \u8f93\u51fa [2, 5, 8]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e00\u65b9\u6cd5\u4e2d\uff0c\u901a\u8fc7\u904d\u5386\u6bcf\u4e2a\u5b50\u5217\u8868\uff08\u5373\u6bcf\u4e00\u884c\uff09\uff0c\u5e76\u63d0\u53d6\u6307\u5b9a\u5217\uff08\u4f8b\u5982\u7b2c2\u5217\uff09\u7684\u503c\uff0c\u6700\u7ec8\u751f\u6210\u4e00\u4e2a\u65b0\u7684\u5217\u8868<code>column<\/code>\uff0c\u5305\u542b\u4e86\u6240\u6709\u5bf9\u5e94\u5217\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u5728Python\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u7528\u4e8e\u751f\u6210\u65b0\u7684\u5217\u8868\u3002\u5b83\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u6548\u7387\u9ad8\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u5217\u8868\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u66f4\u8be6\u7ec6\u7684\u793a\u4f8b\u548c\u89e3\u91ca\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u8003\u8651\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4e8c\u7ef4\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    [10, 20, 30],<\/p>\n<p>    [40, 50, 60],<\/p>\n<p>    [70, 80, 90],<\/p>\n<p>    [100, 110, 120]<\/p>\n<p>]<\/p>\n<p>column_1 = [row[0] for row in data]<\/p>\n<p>column_2 = [row[1] for row in data]<\/p>\n<p>column_3 = [row[2] for row in data]<\/p>\n<p>print(f&quot;Column 1: {column_1}&quot;)  # \u8f93\u51fa [10, 40, 70, 100]<\/p>\n<p>print(f&quot;Column 2: {column_2}&quot;)  # \u8f93\u51fa [20, 50, 80, 110]<\/p>\n<p>print(f&quot;Column 3: {column_3}&quot;)  # \u8f93\u51fa [30, 60, 90, 120]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u63d0\u53d6\u4e86\u7b2c1\u30012\u30013\u5217\u7684\u6240\u6709\u5143\u7d20\u3002\u901a\u8fc7\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u66f4\u5927\u6570\u636e\u96c6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u5927\u7684\u6570\u636e\u96c6\uff0c\u5217\u8868\u63a8\u5bfc\u5f0f\u4f9d\u7136\u9ad8\u6548\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b10000\u884c\u6570\u636e\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>data = [[random.randint(1, 100) for _ in range(5)] for _ in range(10000)]<\/p>\n<p>column_4 = [row[3] for row in data]<\/p>\n<p>print(f&quot;First 10 elements of Column 4: {column_4[:10]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5373\u4f7f\u6570\u636e\u91cf\u5f88\u5927\uff0c\u4e5f\u80fd\u5feb\u901f\u63d0\u53d6\u6307\u5b9a\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001zip\u51fd\u6570<\/h3>\n<\/p>\n<p><p>zip\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u8f6c\u7f6e\u4e8c\u7ef4\u5217\u8868\uff0c\u4ece\u800c\u8f7b\u677e\u63d0\u53d6\u4efb\u610f\u5217\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7zip\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u884c\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>transposed_data = list(zip(*data))<\/p>\n<p>column_1 = transposed_data[0]<\/p>\n<p>column_2 = transposed_data[1]<\/p>\n<p>column_3 = transposed_data[2]<\/p>\n<p>print(f&quot;Column 1: {column_1}&quot;)  # \u8f93\u51fa (1, 4, 7)<\/p>\n<p>print(f&quot;Column 2: {column_2}&quot;)  # \u8f93\u51fa (2, 5, 8)<\/p>\n<p>print(f&quot;Column 3: {column_3}&quot;)  # \u8f93\u51fa (3, 6, 9)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528zip\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u7b80\u6d01\u5730\u8f6c\u7f6e\u6570\u636e\uff0c\u7136\u540e\u8bbf\u95ee\u4efb\u610f\u5217\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u66f4\u5927\u6570\u636e\u96c6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u5927\u7684\u6570\u636e\u96c6\uff0czip\u51fd\u6570\u4f9d\u7136\u9ad8\u6548\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>data = [[random.randint(1, 100) for _ in range(5)] for _ in range(10000)]<\/p>\n<p>transposed_data = list(zip(*data))<\/p>\n<p>column_4 = transposed_data[3]<\/p>\n<p>print(f&quot;First 10 elements of Column 4: {column_4[:10]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u5927\u6570\u636e\u65f6\u4f9d\u7136\u8868\u73b0\u826f\u597d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u662f\u5904\u7406\u6570\u636e\u7684\u5229\u5668\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u7840\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>pandas\u5e93\u7684DataFrame\u5bf9\u8c61\u53ef\u4ee5\u65b9\u4fbf\u5730\u63d0\u53d6\u5217\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<p>df = pd.DataFrame(data, columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;])<\/p>\n<p>column_b = df[&#39;B&#39;]<\/p>\n<p>print(column_b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5217\u540d\u76f4\u63a5\u63d0\u53d6\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u66f4\u5927\u6570\u636e\u96c6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u5927\u7684\u6570\u636e\u96c6\uff0cpandas\u5e93\u4f9d\u7136\u8868\u73b0\u4f18\u5f02\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import random<\/p>\n<p>data = [[random.randint(1, 100) for _ in range(5)] for _ in range(10000)]<\/p>\n<p>df = pd.DataFrame(data, columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;])<\/p>\n<p>column_d = df[&#39;D&#39;]<\/p>\n<p>print(column_d.head(10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pandas\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5927\u6570\u636e\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u529f\u80fd\uff0c\u4f7f\u5176\u6210\u4e3a\u6570\u636e\u79d1\u5b66\u548c\u5206\u6790\u7684\u9996\u9009\u5de5\u5177\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ece\u4e0a\u9762\u7684\u5206\u6790\u53ef\u4ee5\u770b\u51fa\uff0c<strong>\u5217\u8868\u63a8\u5bfc\u5f0f\u3001zip\u51fd\u6570\u3001pandas\u5e93<\/strong>\u90fd\u662f\u5728Python\u4e2d\u63d0\u53d6\u5217\u8868\u67d0\u4e00\u5217\u5143\u7d20\u7684\u6709\u6548\u65b9\u6cd5\u3002\u5217\u8868\u63a8\u5bfc\u5f0f\u9002\u7528\u4e8e\u7b80\u5355\u4e14\u5feb\u901f\u7684\u64cd\u4f5c\uff0czip\u51fd\u6570\u5219\u5728\u9700\u8981\u8f6c\u7f6e\u6570\u636e\u65f6\u8868\u73b0\u4f18\u5f02\uff0c\u800cpandas\u5e93\u5219\u63d0\u4f9b\u4e86\u5168\u9762\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\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\u4ece\u5d4c\u5957\u5217\u8868\u4e2d\u63d0\u53d6\u7279\u5b9a\u5217\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8f7b\u677e\u63d0\u53d6\u5d4c\u5957\u5217\u8868\uff08\u4f8b\u5982\u4e8c\u7ef4\u5217\u8868\uff09\u4e2d\u7684\u7279\u5b9a\u5217\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u4ee3\u8868\u4e00\u884c\u6570\u636e\uff0c\u901a\u8fc7\u7d22\u5f15\u6307\u5b9a\u6240\u9700\u7684\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a<code>column_elements = [row[column_index] for row in nested_list]<\/code>\uff0c\u5176\u4e2d<code>column_index<\/code>\u662f\u4f60\u60f3\u63d0\u53d6\u7684\u5217\u7684\u7d22\u5f15\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u63d0\u53d6\u5217\u8868\u4e2d\u7684\u5217\u5143\u7d20\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01Pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\u3002\u5982\u679c\u4f60\u5c06\u5217\u8868\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61\uff0c\u53ef\u4ee5\u901a\u8fc7\u5217\u540d\u6216\u5217\u7d22\u5f15\u8f7b\u677e\u63d0\u53d6\u6240\u9700\u7684\u5217\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>df[&#39;column_name&#39;]<\/code>\u6216<code>df.iloc[:, column_index]<\/code>\u6765\u83b7\u53d6\u5bf9\u5e94\u7684\u5217\u6570\u636e\u3002\u8fd9\u6837\u505a\u8fd8\u53ef\u4ee5\u5229\u7528Pandas\u7684\u5176\u4ed6\u5f3a\u5927\u529f\u80fd\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5728\u63d0\u53d6\u5217\u5143\u7d20\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\u6216\u7a7a\u5143\u7d20\uff1f<\/strong><br \/>\u5904\u7406\u7f3a\u5931\u503c\u65f6\uff0c\u53ef\u4ee5\u5728\u63d0\u53d6\u5217\u65f6\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\u8fc7\u6ee4\u6389\u7a7a\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u6dfb\u52a0\u6761\u4ef6\uff1a<code>column_elements = [row[column_index] for row in nested_list if row[column_index] is not None]<\/code>\u3002\u6b64\u5916\uff0c\u5982\u679c\u4f7f\u7528Pandas\uff0c\u53ef\u4ee5\u4f7f\u7528<code>dropna()<\/code>\u65b9\u6cd5\u6765\u81ea\u52a8\u8fc7\u6ee4\u6389\u7f3a\u5931\u503c\uff0c\u786e\u4fdd\u63d0\u53d6\u7684\u6570\u636e\u5b8c\u6574\u6709\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u83b7\u53d6\u5217\u8868\u7684\u67d0\u4e00\u5217\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001zip\u51fd\u6570\u3001pandas\u5e93\u7b49\u65b9\u6cd5\u3002\u5728\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":1131257,"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\/1131243"}],"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=1131243"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131243\/revisions"}],"predecessor-version":[{"id":1131261,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1131243\/revisions\/1131261"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1131257"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1131243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1131243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1131243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}