{"id":1143692,"date":"2025-01-08T22:52:39","date_gmt":"2025-01-08T14:52:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1143692.html"},"modified":"2025-01-08T22:52:41","modified_gmt":"2025-01-08T14:52:41","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86csv%e4%b8%80%e5%88%97%e6%95%b0%e6%8d%ae%e5%88%86%e6%88%90%e5%a4%9a%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1143692.html","title":{"rendered":"python\u5982\u4f55\u5c06csv\u4e00\u5217\u6570\u636e\u5206\u6210\u591a\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181158\/d995f3b0-285a-4e38-8134-be30f27d599b.webp\" alt=\"python\u5982\u4f55\u5c06csv\u4e00\u5217\u6570\u636e\u5206\u6210\u591a\u5217\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5c06CSV\u6587\u4ef6\u4e2d\u7684\u4e00\u5217\u6570\u636e\u5206\u6210\u591a\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Pandas\u5e93\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u8bfb\u53d6CSV\u6587\u4ef6\u3001\u9009\u53d6\u76ee\u6807\u5217\u3001\u5bf9\u76ee\u6807\u5217\u8fdb\u884c\u5206\u5272\u3001\u5c06\u5206\u5272\u540e\u7684\u6570\u636e\u6dfb\u52a0\u56de\u539fDataFrame\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528Pandas\u7684<code>str.split()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u975e\u5e38\u9ad8\u6548\u5730\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u7684\u5b9e\u73b0\u65b9\u6cd5\u548c\u76f8\u5173\u77e5\u8bc6\u70b9\u8bb2\u89e3\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u8bfb\u53d6CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406CSV\u6587\u4ef6\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u8bfb\u53d6\u8be5\u6587\u4ef6\u3002Pandas\u5e93\u63d0\u4f9b\u4e86<code>read_csv()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u5c06\u5176\u5b58\u50a8\u5728DataFrame\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;your_file.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bfb\u53d6CSV\u6587\u4ef6\u540e\uff0cDataFrame\u5bf9\u8c61<code>df<\/code>\u5c06\u5305\u542bCSV\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6570\u636e\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>df.head()<\/code>\u67e5\u770b\u6570\u636e\u7684\u524d\u51e0\u884c\uff0c\u4ee5\u786e\u8ba4\u6570\u636e\u662f\u5426\u6b63\u786e\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u9009\u53d6\u76ee\u6807\u5217<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u5217\u6570\u636e\u9700\u8981\u5206\u5272\uff0c\u6bd4\u5982\u5217\u540d\u4e3a<code>&#39;full_name&#39;<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u59d3\u540d\uff0c\u683c\u5f0f\u4e3a\u201c\u540d \u59d3\u201d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u6570\u636e<\/p>\n<p>print(df.head())<\/p>\n<h2><strong>\u9009\u53d6\u76ee\u6807\u5217<\/strong><\/h2>\n<p>target_column = df[&#39;full_name&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5bf9\u76ee\u6807\u5217\u8fdb\u884c\u5206\u5272<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u7684<code>str.split()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\uff08\u5982\u7a7a\u683c\u3001\u9017\u53f7\u7b49\uff09\u5c06\u4e00\u5217\u6570\u636e\u5206\u5272\u6210\u591a\u4e2a\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5206\u5272\u76ee\u6807\u5217<\/p>\n<p>new_columns = target_column.str.split(&#39; &#39;, expand=True)<\/p>\n<h2><strong>\u67e5\u770b\u5206\u5272\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>print(new_columns.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>expand=True<\/code>\u53c2\u6570\u8868\u793a\u5c06\u5206\u5272\u540e\u7684\u6570\u636e\u5c55\u5f00\u6210\u591a\u4e2a\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5c06\u5206\u5272\u540e\u7684\u6570\u636e\u6dfb\u52a0\u56de\u539fDataFrame<\/h3>\n<\/p>\n<p><p>\u5c06\u5206\u5272\u540e\u7684\u6570\u636e\u6dfb\u52a0\u56de\u539fDataFrame\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>assign()<\/code>\u65b9\u6cd5\uff0c\u6216\u8005\u76f4\u63a5\u5c06\u65b0\u7684\u5217\u8d4b\u503c\u7ed9DataFrame\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u65b0\u5217\u6dfb\u52a0\u56de\u539fDataFrame<\/p>\n<p>df[[&#39;first_name&#39;, &#39;last_name&#39;]] = new_columns<\/p>\n<h2><strong>\u67e5\u770b\u66f4\u65b0\u540e\u7684DataFrame<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4fdd\u5b58\u66f4\u65b0\u540e\u7684CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5b8c\u6210\u6570\u636e\u5904\u7406\u540e\uff0c\u53ef\u4ee5\u5c06\u66f4\u65b0\u540e\u7684DataFrame\u4fdd\u5b58\u56deCSV\u6587\u4ef6\uff0c\u4f7f\u7528Pandas\u7684<code>to_csv()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u66f4\u65b0\u540e\u7684CSV\u6587\u4ef6<\/p>\n<p>df.to_csv(&#39;updated_file.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5c06CSV\u6587\u4ef6\u4e2d\u7684\u4e00\u5217\u6570\u636e\u5206\u6210\u591a\u5217\u5e76\u4fdd\u5b58\u66f4\u65b0\u540e\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&#39;your_file.csv&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u6570\u636e<\/strong><\/h2>\n<p>print(&quot;Original DataFrame:&quot;)<\/p>\n<p>print(df.head())<\/p>\n<h2><strong>\u9009\u53d6\u76ee\u6807\u5217<\/strong><\/h2>\n<p>target_column = df[&#39;full_name&#39;]<\/p>\n<h2><strong>\u5206\u5272\u76ee\u6807\u5217<\/strong><\/h2>\n<p>new_columns = target_column.str.split(&#39; &#39;, expand=True)<\/p>\n<h2><strong>\u5c06\u65b0\u5217\u6dfb\u52a0\u56de\u539fDataFrame<\/strong><\/h2>\n<p>df[[&#39;first_name&#39;, &#39;last_name&#39;]] = new_columns<\/p>\n<h2><strong>\u67e5\u770b\u66f4\u65b0\u540e\u7684DataFrame<\/strong><\/h2>\n<p>print(&quot;Updated DataFrame:&quot;)<\/p>\n<p>print(df.head())<\/p>\n<h2><strong>\u4fdd\u5b58\u66f4\u65b0\u540e\u7684CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;updated_file.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u590d\u6742\u6570\u636e\u7684\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u53ef\u80fd\u5e76\u4e0d\u603b\u662f\u90a3\u4e48\u89c4\u6574\u3002\u4f8b\u5982\uff0c\u67d0\u4e9b\u884c\u53ef\u80fd\u5305\u542b\u591a\u4e8e\u4e24\u4e2a\u540d\u5b57\uff0c\u6216\u8005\u540d\u5b57\u4e4b\u95f4\u7684\u5206\u9694\u7b26\u53ef\u80fd\u4e0d\u4e00\u81f4\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u91c7\u7528\u66f4\u590d\u6742\u7684\u5206\u5272\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h4>\u5904\u7406\u591a\u4f59\u7684\u5206\u9694\u7b26<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u76ee\u6807\u5217\u4e2d\u7684\u6570\u636e\u5305\u542b\u591a\u4f59\u7684\u5206\u9694\u7b26\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u5176\u8fdb\u884c\u9884\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u591a\u4f59\u7684\u5206\u9694\u7b26<\/strong><\/h2>\n<p>df[&#39;full_name&#39;] = df[&#39;full_name&#39;].apply(lambda x: re.sub(&#39;\\s+&#39;, &#39; &#39;, x.strip()))<\/p>\n<h2><strong>\u5206\u5272\u76ee\u6807\u5217<\/strong><\/h2>\n<p>new_columns = df[&#39;full_name&#39;].str.split(&#39; &#39;, expand=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5904\u7406\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u67d0\u4e9b\u884c\u7684\u6570\u636e\u683c\u5f0f\u4e0d\u4e00\u81f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u5206\u5272\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u81ea\u5b9a\u4e49\u5206\u5272\u903b\u8f91<\/p>\n<p>def split_name(name):<\/p>\n<p>    parts = name.split(&#39; &#39;)<\/p>\n<p>    if len(parts) == 2:<\/p>\n<p>        return parts<\/p>\n<p>    elif len(parts) &gt; 2:<\/p>\n<p>        return [&#39; &#39;.join(parts[:-1]), parts[-1]]<\/p>\n<p>    else:<\/p>\n<p>        return [parts[0], &#39;&#39;]<\/p>\n<h2><strong>\u5e94\u7528\u81ea\u5b9a\u4e49\u5206\u5272\u903b\u8f91<\/strong><\/h2>\n<p>df[[&#39;first_name&#39;, &#39;last_name&#39;]] = df[&#39;full_name&#39;].apply(split_name).apply(pd.Series)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7Pandas\u5e93\u5904\u7406CSV\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u7684\u6570\u636e\u64cd\u4f5c\u3002<strong>\u5c06CSV\u6587\u4ef6\u4e2d\u7684\u4e00\u5217\u6570\u636e\u5206\u6210\u591a\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>str.split()<\/code>\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u81ea\u5b9a\u4e49\u903b\u8f91\u5904\u7406\u590d\u6742\u60c5\u51b5\u3002<\/strong>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u9ad8\u6548\uff0c\u800c\u4e14\u4ee3\u7801\u7b80\u6d01\u6613\u8bfb\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u5904\u7406\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06CSV\u6587\u4ef6\u4e2d\u7684\u4e00\u5217\u6570\u636e\u62c6\u5206\u4e3a\u591a\u5217\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u9996\u5148\uff0c\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u9009\u62e9\u9700\u8981\u62c6\u5206\u7684\u5217\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>str.split()<\/code>\u65b9\u6cd5\u5c06\u8be5\u5217\u6570\u636e\u6309\u6307\u5b9a\u5206\u9694\u7b26\u62c6\u5206\u4e3a\u591a\u4e2a\u90e8\u5206\u3002\u6700\u540e\uff0c\u5c06\u65b0\u751f\u6210\u7684\u5217\u5408\u5e76\u5230\u539f\u59cbDataFrame\u4e2d\uff0c\u5e76\u4fdd\u5b58\u4e3a\u65b0\u7684CSV\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728\u62c6\u5206\u6570\u636e\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7a7a\u503c\u6216\u7f3a\u5931\u6570\u636e\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>str.split()<\/code>\u65f6\uff0c\u7f3a\u5931\u6570\u636e\u4f1a\u88ab\u81ea\u52a8\u5904\u7406\uff0c\u7ed3\u679c\u4f1a\u8fd4\u56deNaN\u3002\u60a8\u53ef\u4ee5\u9009\u62e9\u5728\u62c6\u5206\u540e\u4f7f\u7528<code>fillna()<\/code>\u65b9\u6cd5\u586b\u5145\u8fd9\u4e9b\u7a7a\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7528\u7a7a\u5b57\u7b26\u4e32\u6216\u7279\u5b9a\u503c\u66ff\u4ee3NaN\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u4e0d\u540c\u7684\u5206\u9694\u7b26\u6765\u62c6\u5206\u5217\u6570\u636e\uff1f<\/strong><br \/>\u5728<code>str.split()<\/code>\u65b9\u6cd5\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>sep<\/code>\u53c2\u6570\u6307\u5b9a\u4e0d\u540c\u7684\u5206\u9694\u7b26\u3002\u6bd4\u5982\uff0c\u5982\u679c\u60a8\u7684\u6570\u636e\u662f\u4ee5\u9017\u53f7\u3001\u7a7a\u683c\u6216\u5176\u4ed6\u5b57\u7b26\u5206\u9694\u7684\uff0c\u60a8\u53ea\u9700\u5c06\u8fd9\u4e9b\u5b57\u7b26\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002\u8fd9\u6837\u7684\u7075\u6d3b\u6027\u8ba9\u60a8\u80fd\u591f\u6839\u636e\u5b9e\u9645\u6570\u636e\u60c5\u51b5\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06CSV\u6587\u4ef6\u4e2d\u7684\u4e00\u5217\u6570\u636e\u5206\u6210\u591a\u5217\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Pandas\u5e93\u6765\u5b9e\u73b0\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u8bfb\u53d6CSV\u6587 [&hellip;]","protected":false},"author":3,"featured_media":1143700,"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\/1143692"}],"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=1143692"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143692\/revisions"}],"predecessor-version":[{"id":1143703,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1143692\/revisions\/1143703"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1143700"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1143692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1143692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1143692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}