{"id":1113672,"date":"2025-01-08T17:50:11","date_gmt":"2025-01-08T09:50:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113672.html"},"modified":"2025-01-08T17:50:14","modified_gmt":"2025-01-08T09:50:14","slug":"python%e5%a6%82%e4%bd%95%e9%80%89%e5%8f%96%e6%95%b0%e6%8d%ae%e6%a1%86%e7%9a%84%e4%b8%80%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1113672.html","title":{"rendered":"python\u5982\u4f55\u9009\u53d6\u6570\u636e\u6846\u7684\u4e00\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075155\/63704a22-5c23-475d-abd9-26652bcf700f.webp\" alt=\"python\u5982\u4f55\u9009\u53d6\u6570\u636e\u6846\u7684\u4e00\u5217\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u9009\u53d6\u6570\u636e\u6846\u7684\u4e00\u5217\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u70b9\u7b26\u53f7\u8bbf\u95ee\u3001\u4f7f\u7528\u65b9\u62ec\u53f7\u3001\u4f7f\u7528iloc\u548cloc\u65b9\u6cd5\u7b49\u3002<\/strong> \u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u548c\u6570\u636e\u6846\u7684\u7ed3\u6784\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u70b9\u7b26\u53f7\u548c\u65b9\u62ec\u53f7\uff0c\u8fd9\u4e24\u79cd\u65b9\u6cd5\u7b80\u6d01\u4e14\u76f4\u89c2\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528\u70b9\u7b26\u53f7\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u70b9\u7b26\u53f7\u8bbf\u95ee\u6570\u636e\u6846\u7684\u4e00\u5217\u662f\u6700\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u5e76\u4e14\u4f60\u60f3\u8981\u8bbf\u95ee\u6570\u636e\u6846\u4e2d\u7684\u67d0\u4e00\u5217\uff0c\u4f8b\u5982\u540d\u4e3a<code>column_name<\/code>\u7684\u5217\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>df.column_name<\/code>\u6765\u5b9e\u73b0\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u4ee3\u7801\u7b80\u6d01\uff0c\u6613\u4e8e\u9605\u8bfb\u548c\u7406\u89e3\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4ec5\u9002\u7528\u4e8e\u5217\u540d\u7b26\u5408Python\u53d8\u91cf\u547d\u540d\u89c4\u5219\u7684\u60c5\u51b5\u3002\u5982\u679c\u5217\u540d\u5305\u542b\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\uff0c\u5219\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u9009\u53d6\u6570\u636e\u6846\u4e00\u5217\u7684\u591a\u79cd\u65b9\u6cd5\u53ca\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u70b9\u7b26\u53f7\u8bbf\u95ee<\/h3>\n<\/p>\n<p><p>\u70b9\u7b26\u53f7\u8bbf\u95ee\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u548c\u76f4\u89c2\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u5e76\u4e14\u60f3\u8bbf\u95ee\u540d\u4e3a<code>column_name<\/code>\u7684\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;column_name&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528\u70b9\u7b26\u53f7\u8bbf\u95ee\u5217<\/strong><\/h2>\n<p>selected_column = df.column_name<\/p>\n<p>print(selected_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u6d01\u3001\u6613\u8bfb<\/li>\n<li>\u9002\u7528\u4e8e\u5217\u540d\u7b26\u5408Python\u53d8\u91cf\u547d\u540d\u89c4\u5219\u7684\u60c5\u51b5<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u80fd\u5904\u7406\u5305\u542b\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\u7684\u5217\u540d<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u65b9\u62ec\u53f7\u8bbf\u95ee<\/h3>\n<\/p>\n<p><p>\u65b9\u62ec\u53f7\u8bbf\u95ee\u662f\u6700\u901a\u7528\u548c\u7075\u6d3b\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u5e76\u4e14\u60f3\u8bbf\u95ee\u540d\u4e3a<code>column_name<\/code>\u7684\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;column_name&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528\u65b9\u62ec\u53f7\u8bbf\u95ee\u5217<\/strong><\/h2>\n<p>selected_column = df[&#39;column_name&#39;]<\/p>\n<p>print(selected_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u901a\u7528\u3001\u7075\u6d3b<\/li>\n<li>\u9002\u7528\u4e8e\u4efb\u4f55\u5217\u540d\uff0c\u5305\u62ec\u5305\u542b\u7a7a\u683c\u6216\u7279\u6b8a\u5b57\u7b26\u7684\u5217\u540d<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u76f8\u5bf9\u4e8e\u70b9\u7b26\u53f7\u65b9\u6cd5\uff0c\u4ee3\u7801\u7565\u5fae\u5197\u957f<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001iloc\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>iloc\u65b9\u6cd5\u7528\u4e8e\u57fa\u4e8e\u6574\u6570\u4f4d\u7f6e\u9009\u62e9\u6570\u636e\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u5e76\u4e14\u60f3\u8bbf\u95ee\u7b2c\u4e00\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;column1&#39;: [1, 2, 3, 4, 5], &#39;column2&#39;: [6, 7, 8, 9, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528iloc\u65b9\u6cd5\u8bbf\u95ee\u7b2c\u4e00\u5217<\/strong><\/h2>\n<p>selected_column = df.iloc[:, 0]<\/p>\n<p>print(selected_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u57fa\u4e8e\u6574\u6570\u4f4d\u7f6e\uff0c\u9002\u7528\u4e8e\u5217\u540d\u672a\u77e5\u6216\u5217\u540d\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u60c5\u51b5<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9700\u8981\u77e5\u9053\u5217\u7684\u6574\u6570\u4f4d\u7f6e<\/li>\n<\/ul>\n<p><h3>\u56db\u3001loc\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>loc\u65b9\u6cd5\u7528\u4e8e\u57fa\u4e8e\u6807\u7b7e\u9009\u62e9\u6570\u636e\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u5e76\u4e14\u60f3\u8bbf\u95ee\u540d\u4e3a<code>column_name<\/code>\u7684\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;column_name&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528loc\u65b9\u6cd5\u8bbf\u95ee\u5217<\/strong><\/h2>\n<p>selected_column = df.loc[:, &#39;column_name&#39;]<\/p>\n<p>print(selected_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u57fa\u4e8e\u6807\u7b7e\u9009\u62e9\u6570\u636e\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9009\u62e9\u7279\u5b9a\u5217\u7684\u60c5\u51b5<\/li>\n<li>\u9002\u7528\u4e8e\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u5217\u540d<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u76f8\u5bf9\u4e8e\u70b9\u7b26\u53f7\u548c\u65b9\u62ec\u53f7\u65b9\u6cd5\uff0c\u4ee3\u7801\u7565\u5fae\u5197\u957f<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u4f7f\u7528at\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>at\u65b9\u6cd5\u7528\u4e8e\u57fa\u4e8e\u6807\u7b7e\u9009\u62e9\u5355\u4e2a\u6570\u636e\u70b9\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u5e76\u4e14\u60f3\u8bbf\u95ee\u7b2c\u4e00\u884c\u540d\u4e3a<code>column_name<\/code>\u7684\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;column_name&#39;: [1, 2, 3, 4, 5]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528at\u65b9\u6cd5\u8bbf\u95ee\u7b2c\u4e00\u884c\u7684\u7279\u5b9a\u5217<\/strong><\/h2>\n<p>selected_value = df.at[0, &#39;column_name&#39;]<\/p>\n<p>print(selected_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u9009\u62e9\u5355\u4e2a\u6570\u636e\u70b9<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u9002\u7528\u4e8e\u9009\u62e9\u6574\u5217\u6570\u636e<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u4f7f\u7528iat\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>iat\u65b9\u6cd5\u7528\u4e8e\u57fa\u4e8e\u6574\u6570\u4f4d\u7f6e\u9009\u62e9\u5355\u4e2a\u6570\u636e\u70b9\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u5e76\u4e14\u60f3\u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e00\u5217\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;column1&#39;: [1, 2, 3, 4, 5], &#39;column2&#39;: [6, 7, 8, 9, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528iat\u65b9\u6cd5\u8bbf\u95ee\u7b2c\u4e00\u884c\u7b2c\u4e00\u5217\u7684\u6570\u636e<\/strong><\/h2>\n<p>selected_value = df.iat[0, 0]<\/p>\n<p>print(selected_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u57fa\u4e8e\u6574\u6570\u4f4d\u7f6e\uff0c\u9002\u7528\u4e8e\u9009\u62e9\u5355\u4e2a\u6570\u636e\u70b9<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u9002\u7528\u4e8e\u9009\u62e9\u6574\u5217\u6570\u636e<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u4f7f\u7528filter\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>filter\u65b9\u6cd5\u7528\u4e8e\u57fa\u4e8e\u5217\u540d\u9009\u62e9\u6570\u636e\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u5e76\u4e14\u60f3\u8bbf\u95ee\u540d\u4e3a<code>column_name<\/code>\u7684\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;column_name&#39;: [1, 2, 3, 4, 5], &#39;other_column&#39;: [6, 7, 8, 9, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528filter\u65b9\u6cd5\u8bbf\u95ee\u5217<\/strong><\/h2>\n<p>selected_column = df.filter(items=[&#39;column_name&#39;])<\/p>\n<p>print(selected_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u540c\u65f6\u9009\u62e9\u591a\u4e2a\u5217<\/li>\n<li>\u9002\u7528\u4e8e\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u5217\u540d<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u76f8\u5bf9\u4e8e\u70b9\u7b26\u53f7\u548c\u65b9\u62ec\u53f7\u65b9\u6cd5\uff0c\u4ee3\u7801\u7565\u5fae\u5197\u957f<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u4f7f\u7528query\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>query\u65b9\u6cd5\u7528\u4e8e\u57fa\u4e8e\u8868\u8fbe\u5f0f\u9009\u62e9\u6570\u636e\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u5e76\u4e14\u60f3\u8bbf\u95ee\u540d\u4e3a<code>column_name<\/code>\u7684\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;column_name&#39;: [1, 2, 3, 4, 5], &#39;other_column&#39;: [6, 7, 8, 9, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528query\u65b9\u6cd5\u8bbf\u95ee\u5217<\/strong><\/h2>\n<p>selected_column = df.query(&#39;column_name &gt; 2&#39;)[&#39;column_name&#39;]<\/p>\n<p>print(selected_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u57fa\u4e8e\u8868\u8fbe\u5f0f\u9009\u62e9\u6570\u636e<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u9002\u7528\u4e8e\u9009\u62e9\u6574\u5217\u6570\u636e<\/li>\n<\/ul>\n<p><h3>\u4e5d\u3001\u4f7f\u7528get\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>get\u65b9\u6cd5\u7528\u4e8e\u57fa\u4e8e\u5217\u540d\u9009\u62e9\u6570\u636e\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u5e76\u4e14\u60f3\u8bbf\u95ee\u540d\u4e3a<code>column_name<\/code>\u7684\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;column_name&#39;: [1, 2, 3, 4, 5], &#39;other_column&#39;: [6, 7, 8, 9, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528get\u65b9\u6cd5\u8bbf\u95ee\u5217<\/strong><\/h2>\n<p>selected_column = df.get(&#39;column_name&#39;)<\/p>\n<p>print(selected_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u540c\u65f6\u9009\u62e9\u591a\u4e2a\u5217<\/li>\n<li>\u9002\u7528\u4e8e\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u5217\u540d<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u76f8\u5bf9\u4e8e\u70b9\u7b26\u53f7\u548c\u65b9\u62ec\u53f7\u65b9\u6cd5\uff0c\u4ee3\u7801\u7565\u5fae\u5197\u957f<\/li>\n<\/ul>\n<p><h3>\u5341\u3001\u4f7f\u7528assign\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>assign\u65b9\u6cd5\u7528\u4e8e\u57fa\u4e8e\u5217\u540d\u9009\u62e9\u6570\u636e\u5e76\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u6570\u636e\u6846<code>df<\/code>\uff0c\u5e76\u4e14\u60f3\u8bbf\u95ee\u540d\u4e3a<code>column_name<\/code>\u7684\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;column_name&#39;: [1, 2, 3, 4, 5], &#39;other_column&#39;: [6, 7, 8, 9, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528assign\u65b9\u6cd5\u8bbf\u95ee\u5217\u5e76\u8fdb\u884c\u6570\u636e\u5904\u7406<\/strong><\/h2>\n<p>df = df.assign(new_column=df[&#39;column_name&#39;] * 2)<\/p>\n<p>selected_column = df[&#39;new_column&#39;]<\/p>\n<p>print(selected_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u540c\u65f6\u9009\u62e9\u591a\u4e2a\u5217\u5e76\u8fdb\u884c\u6570\u636e\u5904\u7406<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u9002\u7528\u4e8e\u9009\u62e9\u6574\u5217\u6570\u636e<\/li>\n<\/ul>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u9009\u53d6\u6570\u636e\u6846\u7684\u4e00\u5217\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u70b9\u7b26\u53f7\u8bbf\u95ee\u3001\u65b9\u62ec\u53f7\u8bbf\u95ee\u3001iloc\u65b9\u6cd5\u3001loc\u65b9\u6cd5\u3001at\u65b9\u6cd5\u3001iat\u65b9\u6cd5\u3001filter\u65b9\u6cd5\u3001query\u65b9\u6cd5\u3001get\u65b9\u6cd5\u548cassign\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u548c\u6570\u636e\u6846\u7684\u7ed3\u6784\u3002\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u548c\u5904\u7406\u6570\u636e\u6846\u4e2d\u7684\u7279\u5b9a\u5217\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728Python\u4e2d\u9009\u53d6\u6570\u636e\u6846\u4e00\u5217\u7684\u65b9\u6cd5\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><p>\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5904\u7406\u6570\u636e\u65f6\u505a\u51fa\u66f4\u660e\u667a\u7684\u9009\u62e9\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002\u65e0\u8bba\u4f60\u662f\u65b0\u624b\u8fd8\u662f\u7ecf\u9a8c\u4e30\u5bcc\u7684\u6570\u636e\u79d1\u5b66\u5bb6\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u662f\u4f60\u6570\u636e\u5904\u7406\u5de5\u5177\u7bb1\u4e2d\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9009\u53d6Pandas\u6570\u636e\u6846\u7684\u7279\u5b9a\u5217\uff1f<\/strong><br \/>\u5728Pandas\u4e2d\uff0c\u9009\u53d6\u7279\u5b9a\u5217\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u6846\u7684\u5217\u540d\u6765\u76f4\u63a5\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6570\u636e\u6846\u540d\u4e3a<code>df<\/code>\uff0c\u60f3\u8981\u9009\u53d6\u540d\u4e3a<code>column_name<\/code>\u7684\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>df[&#39;column_name&#39;]<\/code>\u3002\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2aSeries\u5bf9\u8c61\uff0c\u5305\u542b\u8be5\u5217\u7684\u6240\u6709\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u9009\u53d6\u591a\u5217\u65f6\uff0c\u5982\u4f55\u4f7f\u7528Python\u4ee3\u7801\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u9009\u53d6\u591a\u4e2a\u5217\uff0c\u53ef\u4ee5\u5c06\u5217\u540d\u653e\u5165\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u4f8b\u5982\uff0c<code>df[[&#39;column1&#39;, &#39;column2&#39;]]<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846\uff0c\u53ea\u5305\u542b<code>column1<\/code>\u548c<code>column2<\/code>\u8fd9\u4e24\u5217\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5728\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u5b9e\u7528\uff0c\u80fd\u591f\u5e2e\u52a9\u60a8\u5feb\u901f\u805a\u7126\u4e8e\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u4e0d\u5b58\u5728\u7684\u5217\u540d\u9009\u62e9\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u9009\u53d6\u5217\u65f6\uff0c\u5982\u679c\u6307\u5b9a\u7684\u5217\u540d\u4e0d\u5b58\u5728\uff0cPandas\u5c06\u5f15\u53d1KeyError\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528<code>df.columns<\/code>\u67e5\u770b\u6570\u636e\u6846\u7684\u6240\u6709\u5217\u540d\uff0c\u786e\u4fdd\u6240\u9009\u5217\u540d\u7684\u6b63\u786e\u6027\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>df.get(&#39;column_name&#39;)<\/code>\u65b9\u6cd5\u6765\u5b89\u5168\u5730\u8bbf\u95ee\u5217\u540d\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de<code>None<\/code>\u800c\u4e0d\u662f\u62a5\u9519\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u52a8\u6001\u6570\u636e\u6846\u6216\u4e0d\u786e\u5b9a\u5217\u540d\u7684\u60c5\u51b5\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u9009\u53d6\u6570\u636e\u6846\u7684\u4e00\u5217\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u70b9\u7b26\u53f7\u8bbf\u95ee\u3001\u4f7f\u7528\u65b9\u62ec\u53f7\u3001\u4f7f\u7528iloc\u548cloc\u65b9\u6cd5\u7b49\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1113683,"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\/1113672"}],"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=1113672"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113672\/revisions"}],"predecessor-version":[{"id":1113687,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113672\/revisions\/1113687"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1113683"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1113672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1113672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1113672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}