{"id":1093533,"date":"2025-01-08T14:32:00","date_gmt":"2025-01-08T06:32:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1093533.html"},"modified":"2025-01-08T14:32:02","modified_gmt":"2025-01-08T06:32:02","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%a2%9e%e5%8a%a0%e4%b8%80%e5%88%97%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1093533.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u589e\u52a0\u4e00\u5217\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210055\/b8d93054-1078-458c-a78f-65edbf1257fb.webp\" alt=\"python\u4e2d\u5982\u4f55\u589e\u52a0\u4e00\u5217\u6570\u636e\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u589e\u52a0\u4e00\u5217\u6570\u636e\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Pandas\u5e93\u3002<strong>Pandas\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u64cd\u4f5c\u6570\u636e\u6846\u3001\u4f7f\u7528assign\u65b9\u6cd5\u3001\u4f7f\u7528\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f<\/strong>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u6709\u6548\u5730\u589e\u52a0\u4e00\u5217\u6570\u636e\u5230\u73b0\u6709\u7684\u6570\u636e\u6846\u4e2d\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u8ba8\u8bba\u5b83\u4eec\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Pandas\u5e93\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u5728\u6570\u636e\u79d1\u5b66\u548c\u6570\u636e\u5206\u6790\u9886\u57df\uff0cPandas\u662f\u4e00\u4e2a\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u8981\u4f7f\u7528Pandas\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\u5e76\u5bfc\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u662f\u589e\u52a0\u4e00\u5217\u6570\u636e\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u6570\u636e\u6846\u7684\u5217\u540d\uff0c\u5e76\u5c06\u65b0\u5217\u7684\u6570\u636e\u8d4b\u503c\u7ed9\u5b83\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u5e76\u4e14\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>        &#39;Age&#39;: [25, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u589e\u52a0\u4e00\u5217\u6570\u636e<\/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>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u201cName\u201d\u548c\u201cAge\u201d\u4e24\u5217\u7684\u6570\u636e\u6846\uff0c\u5e76\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\u589e\u52a0\u4e86\u4e00\u5217\u540d\u4e3a\u201cCity\u201d\u7684\u6570\u636e\u3002\u7ed3\u679c\u7684\u6570\u636e\u6846\u5305\u542b\u4e09\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u7b80\u5355\u76f4\u89c2\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5f53\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<li>\u5982\u679c\u65b0\u5217\u7684\u6570\u636e\u4e0d\u5339\u914d\u73b0\u6709\u6570\u636e\u6846\u7684\u957f\u5ea6\uff0c\u4f1a\u5f15\u53d1\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528assign\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Pandas\u7684assign\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u7075\u6d3b\u4e14\u94fe\u5f0f\u64cd\u4f5c\u7684\u65b9\u5f0f\u6765\u589e\u52a0\u4e00\u5217\u6570\u636e\u3002assign\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846\uff0c\u800c\u4e0d\u662f\u5728\u539f\u6570\u636e\u6846\u4e0a\u8fdb\u884c\u4fee\u6539\u3002\u8fd9\u79cd\u65b9\u5f0f\u7279\u522b\u9002\u5408\u9700\u8981\u8fdb\u884c\u591a\u4e2a\u94fe\u5f0f\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>        &#39;Age&#39;: [25, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528assign\u65b9\u6cd5\u589e\u52a0\u4e00\u5217\u6570\u636e<\/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>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528assign\u65b9\u6cd5\u589e\u52a0\u4e86\u4e00\u5217\u540d\u4e3a\u201cCity\u201d\u7684\u6570\u636e\u3002assign\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5c06\u5176\u91cd\u65b0\u8d4b\u503c\u7ed9df\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u652f\u6301\u94fe\u5f0f\u64cd\u4f5c\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u4f18\u96c5\u3002<\/li>\n<li>\u4e0d\u4f1a\u4fee\u6539\u539f\u6570\u636e\u6846\uff0c\u5177\u6709\u66f4\u9ad8\u7684\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u53ef\u80fd\u4e0d\u5982\u76f4\u63a5\u8d4b\u503c\u65b9\u5f0f\u76f4\u89c2\u3002<\/li>\n<li>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u6846\uff0c\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528insert\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>insert\u65b9\u6cd5\u5141\u8bb8\u6211\u4eec\u5728\u6570\u636e\u6846\u7684\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u4e00\u5217\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u63a7\u5236\u65b0\u5217\u63d2\u5165\u4f4d\u7f6e\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>        &#39;Age&#39;: [25, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528insert\u65b9\u6cd5\u5728\u6307\u5b9a\u4f4d\u7f6e\u63d2\u5165\u4e00\u5217\u6570\u636e<\/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>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528insert\u65b9\u6cd5\u5728\u6570\u636e\u6846\u7684\u7b2c\u4e8c\u5217\u4f4d\u7f6e\u63d2\u5165\u4e86\u4e00\u5217\u540d\u4e3a\u201cCity\u201d\u7684\u6570\u636e\u3002insert\u65b9\u6cd5\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff1a\u63d2\u5165\u4f4d\u7f6e\u3001\u5217\u540d\u548c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u53ef\u4ee5\u63a7\u5236\u65b0\u5217\u7684\u63d2\u5165\u4f4d\u7f6e\uff0c\u7075\u6d3b\u6027\u66f4\u9ad8\u3002<\/li>\n<li>\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u6301\u5217\u987a\u5e8f\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u76f8\u5bf9\u590d\u6742\uff0c\u4e0d\u5982\u76f4\u63a5\u8d4b\u503c\u548cassign\u65b9\u6cd5\u7b80\u6d01\u3002<\/li>\n<li>\u53ef\u80fd\u4f1a\u5f71\u54cd\u6570\u636e\u6846\u7684\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5728\u9891\u7e41\u63d2\u5165\u64cd\u4f5c\u65f6\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u4f7f\u7528apply\u65b9\u6cd5\u8ba1\u7b97\u65b0\u5217\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u65b0\u5217\u7684\u6570\u636e\u9700\u8981\u901a\u8fc7\u8ba1\u7b97\u73b0\u6709\u5217\u7684\u6570\u636e\u6765\u751f\u6210\u3002Pandas\u7684apply\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>        &#39;Age&#39;: [25, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528apply\u65b9\u6cd5\u8ba1\u7b97\u65b0\u5217\u6570\u636e<\/strong><\/h2>\n<p>df[&#39;AgeGroup&#39;] = df[&#39;Age&#39;].apply(lambda x: &#39;Youth&#39; if x &lt; 30 else &#39;Adult&#39;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528apply\u65b9\u6cd5\u8ba1\u7b97\u201cAgeGroup\u201d\u5217\u7684\u6570\u636e\u3002\u6839\u636e\u201cAge\u201d\u5217\u7684\u503c\uff0c\u6211\u4eec\u5c06\u5e74\u9f84\u5c0f\u4e8e30\u7684\u5206\u4e3a\u201cYouth\u201d\uff0c\u5176\u4ed6\u7684\u5206\u4e3a\u201cAdult\u201d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u9700\u8981\u57fa\u4e8e\u73b0\u6709\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\u7684\u65b0\u5217\u3002<\/li>\n<li>\u7075\u6d3b\u6027\u9ad8\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u590d\u6742\u7684\u8ba1\u7b97\u903b\u8f91\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u4e0d\u9002\u5408\u7b80\u5355\u7684\u5217\u589e\u52a0\u64cd\u4f5c\u3002<\/li>\n<li>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u6846\uff0capply\u65b9\u6cd5\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><h3>\u516d\u3001\u7ed3\u5408\u591a\u4e2a\u65b9\u6cd5\u5b9e\u73b0\u590d\u6742\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7ed3\u5408\u76f4\u63a5\u8d4b\u503c\u548capply\u65b9\u6cd5\u7684\u793a\u4f8b\uff0c\u7528\u4e8e\u751f\u6210\u66f4\u590d\u6742\u7684\u65b0\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>        &#39;Age&#39;: [25, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u589e\u52a0\u4e00\u5217\u6027\u522b\u6570\u636e<\/strong><\/h2>\n<p>df[&#39;Gender&#39;] = [&#39;Female&#39;, &#39;Male&#39;, &#39;Male&#39;]<\/p>\n<h2><strong>\u4f7f\u7528apply\u65b9\u6cd5\u8ba1\u7b97\u65b0\u5217\u6570\u636e<\/strong><\/h2>\n<p>df[&#39;Description&#39;] = df.apply(lambda row: f&quot;{row[&#39;Name&#39;]} is a {row[&#39;Age&#39;]} years old {row[&#39;Gender&#39;]}.&quot;, axis=1)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u589e\u52a0\u4e86\u4e00\u5217\u201cGender\u201d\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528apply\u65b9\u6cd5\u751f\u6210\u201cDescription\u201d\u5217\u7684\u6570\u636e\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u548c\u8ba1\u7b97\u3002<\/li>\n<li>\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u9700\u8981\u66f4\u9ad8\u7684\u7f16\u7a0b\u6280\u5de7\u3002<\/li>\n<li>\u53ef\u80fd\u5bf9\u6027\u80fd\u4ea7\u751f\u5f71\u54cd\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u589e\u52a0\u4e00\u5217\u6570\u636e\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ec\u76f4\u63a5\u8d4b\u503c\u3001assign\u65b9\u6cd5\u3001insert\u65b9\u6cd5\u548capply\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u76f4\u63a5\u8d4b\u503c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u5217\u589e\u52a0\u64cd\u4f5c\uff0cassign\u65b9\u6cd5\u9002\u7528\u4e8e\u94fe\u5f0f\u64cd\u4f5c\uff0cinsert\u65b9\u6cd5\u9002\u7528\u4e8e\u63a7\u5236\u63d2\u5165\u4f4d\u7f6e\uff0capply\u65b9\u6cd5\u9002\u7528\u4e8e\u57fa\u4e8e\u73b0\u6709\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\u7684\u65b0\u5217\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u6216\u8005\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u3002\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u6846\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1\u5728Python\u4e2d\u589e\u52a0\u4e00\u5217\u6570\u636e\u7684\u65b9\u6cd5\u548c\u6280\u5de7\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u7591\u95ee\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u5728\u8bc4\u8bba\u533a\u7559\u8a00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Pandas\u5e93\u589e\u52a0\u4e00\u5217\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u3002\u8981\u589e\u52a0\u4e00\u5217\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528DataFrame\u7684\u8d4b\u503c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a<code>df<\/code>\u7684DataFrame\uff0c\u53ef\u4ee5\u901a\u8fc7<code>df[&#39;\u65b0\u5217\u540d&#39;] = \u6570\u636e<\/code>\u7684\u65b9\u5f0f\u6dfb\u52a0\u65b0\u5217\u3002\u6570\u636e\u53ef\u4ee5\u662f\u4e00\u4e2a\u5217\u8868\u3001\u6570\u7ec4\u6216\u5176\u4ed6Series\u5bf9\u8c61\uff0c\u957f\u5ea6\u9700\u8981\u4e0eDataFrame\u7684\u884c\u6570\u4e00\u81f4\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6dfb\u52a0\u4e00\u5217\u65f6\uff0c\u5982\u4f55\u6839\u636e\u5df2\u6709\u5217\u7684\u503c\u8fdb\u884c\u8ba1\u7b97\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u5e0c\u671b\u65b0\u5217\u7684\u503c\u57fa\u4e8e\u5176\u4ed6\u5217\u7684\u8ba1\u7b97\u7ed3\u679c\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u8fd0\u7b97\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709<code>df[&#39;A&#39;]<\/code>\u548c<code>df[&#39;B&#39;]<\/code>\u4e24\u5217\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>df[&#39;C&#39;] = df[&#39;A&#39;] + df[&#39;B&#39;]<\/code>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5217<code>C<\/code>\uff0c\u5176\u503c\u4e3a<code>A<\/code>\u548c<code>B<\/code>\u76f8\u52a0\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u7ed9\u65b0\u589e\u7684\u5217\u8d4b\u4e88\u9ed8\u8ba4\u503c\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u65b0\u5217\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u8d4b\u4e88\u5176\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>df[&#39;\u65b0\u5217\u540d&#39;] = \u9ed8\u8ba4\u503c<\/code>\u7684\u65b9\u5f0f\u3002\u8fd9\u6837\uff0c\u6240\u6709\u884c\u7684\u8be5\u5217\u90fd\u4f1a\u88ab\u8d4b\u4e88\u76f8\u540c\u7684\u9ed8\u8ba4\u503c\uff0c\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u5c06\u65b0\u5217\u7684\u9ed8\u8ba4\u503c\u8bbe\u4e3a0\uff0c\u53ef\u4ee5\u4f7f\u7528<code>df[&#39;\u65b0\u5217\u540d&#39;] = 0<\/code>\u3002\u8fd9\u6837\u6240\u6709\u884c\u7684\u65b0\u5217\u6570\u636e\u90fd\u4f1a\u662f0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u589e\u52a0\u4e00\u5217\u6570\u636e\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Pandas\u5e93\u3002Pandas\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u64cd\u4f5c\u6570\u636e\u6846\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1093538,"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\/1093533"}],"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=1093533"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093533\/revisions"}],"predecessor-version":[{"id":1093540,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093533\/revisions\/1093540"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1093538"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1093533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1093533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1093533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}