{"id":1113575,"date":"2025-01-08T17:49:13","date_gmt":"2025-01-08T09:49:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113575.html"},"modified":"2025-01-08T17:49:16","modified_gmt":"2025-01-08T09:49:16","slug":"python%e6%95%b0%e6%8d%ae%e6%a1%86%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e5%b1%9e%e6%80%a7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1113575.html","title":{"rendered":"python\u6570\u636e\u6846\u5982\u4f55\u6dfb\u52a0\u4e00\u4e2a\u5c5e\u6027"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075133\/6f36c05d-d44a-44e8-a7c8-46d2d7faec50.webp\" alt=\"python\u6570\u636e\u6846\u5982\u4f55\u6dfb\u52a0\u4e00\u4e2a\u5c5e\u6027\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4e3a\u6570\u636e\u6846\u6dfb\u52a0\u5c5e\u6027\u7684\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6570\u636e\u6846\uff08DataFrame\uff09\u662f\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\u3002\u8981\u4e3a\u6570\u636e\u6846\u6dfb\u52a0\u4e00\u4e2a\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e2d\u7684<code>DataFrame<\/code>\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u5c5e\u6027\u7684\u6dfb\u52a0\u548c\u64cd\u4f5c\u3002<strong>\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>assign<\/code>\u65b9\u6cd5\u3001\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528<code>insert<\/code>\u65b9\u6cd5<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\u7684\u4e00\u79cd\u2014\u2014\u76f4\u63a5\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u76f4\u63a5\u5728\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u65b0\u5217\uff0c\u5e76\u4e3a\u5176\u6307\u5b9a\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165pandas\u5e93<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5bfc\u5165pandas\u5e93\uff0c\u8fd9\u662f\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u57fa\u7840\u3002<\/li>\n<li><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u6570\u636e\u6846\uff0c\u53ef\u4ee5\u4ece\u5b57\u5178\u3001\u5217\u8868\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\u5bfc\u5165\u6570\u636e\u3002<\/li>\n<li><strong>\u6dfb\u52a0\u65b0\u5c5e\u6027<\/strong>\uff1a\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u65b9\u5f0f\u5c06\u65b0\u5c5e\u6027\uff08\u5217\uff09\u6dfb\u52a0\u5230\u6570\u636e\u6846\u4e2d\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u5c5e\u6027\uff08\u5217\uff09<\/strong><\/h2>\n<p>df[&#39;City&#39;] = [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u540d\u5b57\u548c\u5e74\u9f84\u7684\u57fa\u672c\u6570\u636e\u6846\u3002\u7136\u540e\uff0c\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u6cd5\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u5c5e\u6027\uff08\u5217\uff09\u201cCity\u201d\uff0c\u5e76\u4e3a\u5176\u6307\u5b9a\u4e86\u76f8\u5e94\u7684\u57ce\u5e02\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528assign\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>assign<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u6dfb\u52a0\u65b0\u5217\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846\u3002\u5b83\u5141\u8bb8\u94fe\u5f0f\u64cd\u4f5c\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u6613\u8bfb\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528assign\u65b9\u6cd5\u6dfb\u52a0\u65b0\u5c5e\u6027<\/strong>\uff1a\u901a\u8fc7\u8c03\u7528<code>assign<\/code>\u65b9\u6cd5\uff0c\u5e76\u4f20\u9012\u4e00\u4e2a\u5173\u952e\u5b57\u53c2\u6570\u6765\u6307\u5b9a\u65b0\u5c5e\u6027\u7684\u540d\u79f0\u548c\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528assign\u65b9\u6cd5\u6dfb\u52a0\u65b0\u5c5e\u6027<\/strong><\/h2>\n<p>df = df.assign(City=[&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528<code>assign<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u4e3a\u6570\u636e\u6846\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u5c5e\u6027\uff08\u5217\uff09\u201cCity\u201d\uff0c\u5e76\u6307\u5b9a\u4e86\u76f8\u5e94\u7684\u57ce\u5e02\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528insert\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>insert<\/code>\u65b9\u6cd5\u5141\u8bb8\u5728\u6307\u5b9a\u7684\u4f4d\u7f6e\u63d2\u5165\u65b0\u5217\uff0c\u8fd9\u5bf9\u4e8e\u9700\u8981\u63a7\u5236\u5217\u7684\u987a\u5e8f\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528insert\u65b9\u6cd5\u6dfb\u52a0\u65b0\u5c5e\u6027<\/strong>\uff1a\u901a\u8fc7\u8c03\u7528<code>insert<\/code>\u65b9\u6cd5\uff0c\u6307\u5b9a\u63d2\u5165\u4f4d\u7f6e\u3001\u5217\u540d\u548c\u6570\u636e\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528insert\u65b9\u6cd5\u5728\u7b2c\u4e8c\u5217\u4f4d\u7f6e\u63d2\u5165\u65b0\u5c5e\u6027<\/strong><\/h2>\n<p>df.insert(1, &#39;City&#39;, [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528<code>insert<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u5728\u6570\u636e\u6846\u7684\u7b2c\u4e8c\u5217\u4f4d\u7f6e\u63d2\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u5c5e\u6027\uff08\u5217\uff09\u201cCity\u201d\uff0c\u5e76\u6307\u5b9a\u4e86\u76f8\u5e94\u7684\u57ce\u5e02\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5728\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u5c5e\u6027\uff0c\u4f8b\u5982\u4f7f\u7528<code>concat<\/code>\u65b9\u6cd5\u5c06\u591a\u4e2a\u6570\u636e\u6846\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u6216\u4f7f\u7528<code>merge<\/code>\u65b9\u6cd5\u5408\u5e76\u4e24\u4e2a\u6570\u636e\u6846\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>concat<\/code>\u65b9\u6cd5\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u6570\u636e\u6846<\/strong><\/h2>\n<p>df1 = pd.DataFrame({<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35]<\/p>\n<p>})<\/p>\n<p>df2 = pd.DataFrame({<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>})<\/p>\n<h2><strong>\u4f7f\u7528concat\u65b9\u6cd5\u8fde\u63a5\u4e24\u4e2a\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.concat([df1, df2], axis=1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u91ca<\/strong>\uff1a\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u8c03\u7528<code>concat<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u5c06\u4e24\u4e2a\u6570\u636e\u6846\u6cbf\u5217\u65b9\u5411\u8fde\u63a5\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u7684\u5c5e\u6027\uff08\u5217\uff09\u201cCity\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4e3a\u6570\u636e\u6846\u6dfb\u52a0\u5c5e\u6027\u662f\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4e2d\u7684\u5e38\u89c1\u64cd\u4f5c\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528<code>assign<\/code>\u65b9\u6cd5\u3001\u4f7f\u7528<code>insert<\/code>\u65b9\u6cd5\u4ee5\u53ca\u5176\u4ed6\u65b9\u6cd5<\/strong>\u6765\u5b9e\u73b0\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u9ad8\u6548\uff0c\u5e76\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u7075\u6d3b\u6027\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u6709\u6548\u5730\u7ba1\u7406\u548c\u5206\u6790\u6570\u636e\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002\u65e0\u8bba\u662f\u6dfb\u52a0\u65b0\u7684\u5217\u3001\u66f4\u65b0\u5df2\u6709\u7684\u5217\uff0c\u8fd8\u662f\u8fdb\u884c\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u90fd\u80fd\u6781\u5927\u5730\u589e\u5f3a\u6570\u636e\u5904\u7406\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u6570\u636e\u6846\u4e2d\u6dfb\u52a0\u65b0\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u5904\u7406\u6570\u636e\u6846\uff08DataFrame\uff09\u3002\u8981\u6dfb\u52a0\u65b0\u5217\uff0c\u53ea\u9700\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u60f3\u6dfb\u52a0\u4e00\u5217\u540d\u4e3a\u201c\u65b0\u5c5e\u6027\u201d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<code>dataframe[&#39;\u65b0\u5c5e\u6027&#39;] = \u503c<\/code>\uff0c\u5176\u4e2d\u201c\u503c\u201d\u53ef\u4ee5\u662f\u4e00\u4e2a\u5217\u8868\u3001\u7cfb\u5217\u6216\u5e38\u6570\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u6570\u636e\u6846\u4e2d\u6839\u636e\u6761\u4ef6\u6dfb\u52a0\u5c5e\u6027\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u73b0\u6709\u5217\u7684\u6761\u4ef6\u6dfb\u52a0\u65b0\u5c5e\u6027\u3002\u53ef\u4ee5\u4f7f\u7528<code>np.where()<\/code>\u51fd\u6570\u6216<code>apply()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u8981\u6839\u636e\u53e6\u4e00\u5217\u7684\u503c\u6765\u8bbe\u7f6e\u65b0\u5217\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u4e8e<code>dataframe[&#39;\u65b0\u5c5e\u6027&#39;] = np.where(dataframe[&#39;\u6761\u4ef6\u5217&#39;] &gt; \u67d0\u503c, &#39;\u6ee1\u8db3\u6761\u4ef6&#39;, &#39;\u4e0d\u6ee1\u8db3\u6761\u4ef6&#39;)<\/code>\u7684\u8bed\u53e5\u3002<\/p>\n<p><strong>\u5982\u4f55\u6dfb\u52a0\u591a\u4e2a\u5c5e\u6027\u5230\u6570\u636e\u6846\u4e2d\uff1f<\/strong><br \/>\u8981\u4e00\u6b21\u6027\u6dfb\u52a0\u591a\u4e2a\u5c5e\u6027\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>assign()<\/code>\u65b9\u6cd5\u3002\u8be5\u65b9\u6cd5\u5141\u8bb8\u60a8\u5728\u4e00\u4e2a\u6b65\u9aa4\u4e2d\u6dfb\u52a0\u591a\u4e2a\u5217\u3002\u4f8b\u5982\uff1a<code>dataframe = dataframe.assign(\u65b0\u5c5e\u60271=\u503c1, \u65b0\u5c5e\u60272=\u503c2)<\/code>\uff0c\u8fd9\u5c06\u5728\u6570\u636e\u6846\u4e2d\u540c\u65f6\u6dfb\u52a0\u201c\u65b0\u5c5e\u60271\u201d\u548c\u201c\u65b0\u5c5e\u60272\u201d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4e3a\u6570\u636e\u6846\u6dfb\u52a0\u5c5e\u6027\u7684\u65b9\u6cd5 \u5728Python\u4e2d\uff0c\u6570\u636e\u6846\uff08DataFrame\uff09\u662f\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u7684\u5f3a [&hellip;]","protected":false},"author":3,"featured_media":1113583,"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\/1113575"}],"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=1113575"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113575\/revisions"}],"predecessor-version":[{"id":1113586,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113575\/revisions\/1113586"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1113583"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1113575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1113575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1113575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}