{"id":1139428,"date":"2025-01-08T22:10:10","date_gmt":"2025-01-08T14:10:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1139428.html"},"modified":"2025-01-08T22:10:13","modified_gmt":"2025-01-08T14:10:13","slug":"python%e5%a6%82%e4%bd%95%e5%8f%96csv%e6%9f%90%e4%b8%80%e5%88%97%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1139428.html","title":{"rendered":"python\u5982\u4f55\u53d6csv\u67d0\u4e00\u5217\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102736\/be4374aa-3e8b-4168-8b67-8978eca267fb.webp\" alt=\"python\u5982\u4f55\u53d6csv\u67d0\u4e00\u5217\u6570\u636e\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8bfb\u53d6CSV\u6587\u4ef6\u4e2d\u7684\u67d0\u4e00\u5217\u6570\u636e\u5e76\u786e\u5b9a\u5176\u6570\u636e\u7c7b\u578b\u3002<\/strong> \u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u3001CSV\u6a21\u5757\u548cNumpy\u5e93\u3002<strong>\u5176\u4e2d\uff0cPandas\u5e93\u662f\u6700\u5e38\u7528\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u8fd8\u80fd\u81ea\u52a8\u63a8\u65ad\u6570\u636e\u7c7b\u578b\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>Pandas\u5e93\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u786e\u5b9a\u6570\u636e\u7c7b\u578b\uff1a<\/strong><\/p>\n<p>Pandas\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u67e5\u770b\u67d0\u4e00\u5217\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f60\u53ea\u9700\u8981\u4f7f\u7528<code>read_csv<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>dtypes<\/code>\u5c5e\u6027\u67e5\u770b\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165\u5b83\u4eec\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u67e5\u770b\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u6570\u636e\u5206\u6790\u4e2d\u6700\u5e38\u7528\u7684\u5de5\u5177\u4e4b\u4e00\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u8fd8\u80fd\u81ea\u52a8\u63a8\u65ad\u6570\u636e\u7c7b\u578b\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pandas\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u67e5\u770b\u67d0\u4e00\u5217\u6570\u636e\u7c7b\u578b\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528Pandas\u7684<code>read_csv<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_csv(&#39;your_file.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u67e5\u770b\u67d0\u4e00\u5217\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6\u6587\u4ef6\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>dtypes<\/code>\u5c5e\u6027\u67e5\u770b\u6240\u6709\u5217\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6216\u8005\u4f7f\u7528<code>astype<\/code>\u65b9\u6cd5\u8f6c\u6362\u67d0\u4e00\u5217\u7684\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">column_type = df[&#39;your_column&#39;].dtype<\/p>\n<p>print(f&quot;The data type of the column is: {column_type}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528CSV\u6a21\u5757\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u67e5\u770b\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u867d\u7136Pandas\u662f\u6700\u5e38\u7528\u7684\u5de5\u5177\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528Python\u81ea\u5e26\u7684CSV\u6a21\u5757\u6765\u8bfb\u53d6\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528CSV\u6a21\u5757\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u67e5\u770b\u67d0\u4e00\u5217\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5185\u7f6e\u7684CSV\u6a21\u5757\u8bfb\u53d6CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;your_file.csv&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    reader = csv.DictReader(csvfile)<\/p>\n<p>    data = [row[&#39;your_column&#39;] for row in reader]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u67e5\u770b\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>CSV\u6a21\u5757\u4e0d\u4f1a\u81ea\u52a8\u63a8\u65ad\u6570\u636e\u7c7b\u578b\uff0c\u56e0\u6b64\u4f60\u9700\u8981\u624b\u52a8\u68c0\u67e5\u6bcf\u4e2a\u5143\u7d20\u7684\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def infer_type(value):<\/p>\n<p>    try:<\/p>\n<p>        int(value)<\/p>\n<p>        return &#39;int&#39;<\/p>\n<p>    except ValueError:<\/p>\n<p>        try:<\/p>\n<p>            float(value)<\/p>\n<p>            return &#39;float&#39;<\/p>\n<p>        except ValueError:<\/p>\n<p>            return &#39;str&#39;<\/p>\n<p>column_types = set(infer_type(value) for value in data)<\/p>\n<p>print(f&quot;The data types in the column are: {column_types}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Numpy\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u67e5\u770b\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>Numpy\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u6570\u503c\u8ba1\u7b97\u3002\u867d\u7136Numpy\u4e0d\u5982Pandas\u5728\u8bfb\u53d6CSV\u6587\u4ef6\u65b9\u9762\u65b9\u4fbf\uff0c\u4f46\u5b83\u4ecd\u7136\u53ef\u4ee5\u5b8c\u6210\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Numpy\u7684<code>genfromtxt<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.genfromtxt(&#39;your_file.csv&#39;, delimiter=&#39;,&#39;, names=True, dtype=None, encoding=None)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u67e5\u770b\u67d0\u4e00\u5217\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>Numpy\u6570\u7ec4\u7684<code>dtype<\/code>\u5c5e\u6027\u53ef\u4ee5\u5e2e\u52a9\u4f60\u67e5\u770b\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">column_type = data[&#39;your_column&#39;].dtype<\/p>\n<p>print(f&quot;The data type of the column is: {column_type}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u67e5\u770b\u67d0\u4e00\u5217\u7684\u6570\u636e\u7c7b\u578b\u3002<strong>\u5176\u4e2d\uff0cPandas\u5e93\u662f\u6700\u63a8\u8350\u7684\u5de5\u5177<\/strong>\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u529f\u80fd\u5f3a\u5927\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a5\u53e3\u548c\u65b9\u6cd5\uff0c\u6781\u5927\u5730\u65b9\u4fbf\u4e86\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002<strong>CSV\u6a21\u5757\u548cNumpy\u5e93\u4e5f\u53ef\u4ee5\u5728\u7279\u5b9a\u60c5\u51b5\u4e0b\u4f7f\u7528<\/strong>\uff0c\u4f46\u5b83\u4eec\u53ef\u80fd\u9700\u8981\u66f4\u591a\u7684\u624b\u52a8\u64cd\u4f5c\u6765\u786e\u5b9a\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u60c5\u51b5\u3002\u65e0\u8bba\u4f60\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u4e86\u89e3\u6bcf\u79cd\u5de5\u5177\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u90fd\u80fd\u5e2e\u52a9\u4f60\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6CSV\u6587\u4ef6\u4e2d\u7279\u5b9a\u5217\u7684\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u8981\u8bfb\u53d6CSV\u6587\u4ef6\u4e2d\u7279\u5b9a\u5217\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u5229\u7528Pandas\u5e93\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>pd.read_csv()<\/code>\u51fd\u6570\u8bfb\u53d6\u6574\u4e2aCSV\u6587\u4ef6\u3002\u63a5\u7740\uff0c\u901a\u8fc7<code>df[&#39;\u5217\u540d&#39;].dtype<\/code>\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u67d0\u4e00\u5217\u7684\u6570\u636e\u7c7b\u578b\u3002\u8fd9\u79cd\u65b9\u6cd5\u9ad8\u6548\u4e14\u4fbf\u6377\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u68c0\u67e5CSV\u6587\u4ef6\u4e2d\u67d0\u4e00\u5217\u662f\u5426\u5305\u542b\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Pandas\u8bfb\u53d6CSV\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>df[&#39;\u5217\u540d&#39;].isnull().sum()<\/code>\u6765\u68c0\u67e5\u6307\u5b9a\u5217\u4e2d\u7684\u7f3a\u5931\u503c\u6570\u91cf\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u80fd\u5e2e\u52a9\u4f60\u4e86\u89e3\u6570\u636e\u7684\u5b8c\u6574\u6027\uff0c\u8fd8\u80fd\u4e3a\u540e\u7eed\u7684\u6570\u636e\u6e05\u7406\u548c\u5904\u7406\u63d0\u4f9b\u91cd\u8981\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u679cCSV\u67d0\u4e00\u5217\u7684\u6570\u636e\u7c7b\u578b\u4e0d\u7b26\u5408\u9884\u671f\uff0c\u6211\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u9047\u5230\u6570\u636e\u7c7b\u578b\u4e0d\u7b26\u5408\u9884\u671f\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>astype()<\/code>\u65b9\u6cd5\u5c06\u8be5\u5217\u8f6c\u6362\u4e3a\u6240\u9700\u7684\u6570\u636e\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>df[&#39;\u5217\u540d&#39;] = df[&#39;\u5217\u540d&#39;].astype(&#39;int&#39;)<\/code>\u5c06\u628a\u6307\u5b9a\u5217\u8f6c\u6362\u4e3a\u6574\u6570\u7c7b\u578b\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\uff0c\u4e3a\u540e\u7eed\u5206\u6790\u6253\u4e0b\u826f\u597d\u57fa\u7840\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8bfb\u53d6CSV\u6587\u4ef6\u4e2d\u7684\u67d0\u4e00\u5217\u6570\u636e\u5e76\u786e\u5b9a\u5176\u6570\u636e\u7c7b\u578b\u3002 \u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Pandas [&hellip;]","protected":false},"author":3,"featured_media":1139444,"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\/1139428"}],"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=1139428"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139428\/revisions"}],"predecessor-version":[{"id":1139448,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1139428\/revisions\/1139448"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1139444"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1139428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1139428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1139428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}