{"id":12,"date":"2026-11-08T13:15:33","date_gmt":"2026-11-08T13:15:33","guid":{"rendered":"http:\/\/blog.antpython.net\/?p=12"},"modified":"2026-11-08T13:15:33","modified_gmt":"2026-11-08T13:15:33","slug":"pandas-%e8%af%bb%e5%86%99-csv-excel-%e4%b8%8e%e7%bc%ba%e5%a4%b1%e5%80%bc%e5%a4%84%e7%90%86%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"http:\/\/blog.antpython.net\/?p=12","title":{"rendered":"Pandas \u8bfb\u5199 CSV\/Excel \u4e0e\u7f3a\u5931\u503c\u5904\u7406\u5b9e\u6218"},"content":{"rendered":"<p>\u6570\u636e\u5206\u6790\u7b2c\u4e00\u5173\u4e0d\u662f\u5efa\u6a21\uff0c\u800c\u662f<strong>\u628a\u8868\u8bfb\u8fdb\u6765\u5e76\u6d17\u5e72\u51c0<\/strong>\u3002CSV\/Excel \u8bfb\u5199\u548c\u7f3a\u5931\u503c\u5904\u7406\uff0c\u662f Pandas \u6bcf\u5929\u90fd\u5728\u7528\u7684\u57fa\u672c\u529f\u3002<\/p>\n<h2>\u8bfb\u5165\u6570\u636e<\/h2>\n<pre><code>import pandas as pd\n\ndf = pd.read_csv(\"orders.csv\", encoding=\"utf-8-sig\")\n# \u5927\u6587\u4ef6\u53ef\u5148\u770b\u524d\u51e0\u884c\u3001\u6307\u5b9a\u5217\ndf_preview = pd.read_csv(\"orders.csv\", nrows=5, usecols=[\"order_id\", \"amount\", \"status\"])\n\n# Excel\nxlsx = pd.read_excel(\"sales.xlsx\", sheet_name=\"2026Q1\")\n<\/code><\/pre>\n<h2>\u5199\u51fa\u7ed3\u679c<\/h2>\n<pre><code>df.to_csv(\"clean_orders.csv\", index=False, encoding=\"utf-8-sig\")\ndf.to_excel(\"clean_orders.xlsx\", index=False)\n<\/code><\/pre>\n<h2>\u7f3a\u5931\u503c\u957f\u4ec0\u4e48\u6837<\/h2>\n<pre><code>print(df.isna().sum())          # \u6bcf\u5217\u7f3a\u5931\u6570\u91cf\nprint(df.isna().mean().sort_values(ascending=False))  # \u7f3a\u5931\u6bd4\u4f8b\n<\/code><\/pre>\n<h2>\u5e38\u89c1\u5904\u7406\u7b56\u7565<\/h2>\n<pre><code># 1) \u5220\u9664\uff1a\u7f3a\u5931\u5f88\u5c11\u4e14\u968f\u673a\ndf2 = df.dropna(subset=[\"order_id\", \"user_id\"])\n\n# 2) \u586b\u5145\uff1a\u6570\u503c\u7528\u4e2d\u4f4d\u6570\uff0c\u7c7b\u522b\u7528\u4f17\u6570\/\u672a\u77e5\ndf[\"amount\"] = df[\"amount\"].fillna(df[\"amount\"].median())\ndf[\"city\"] = df[\"city\"].fillna(\"\u672a\u77e5\")\n\n# 3) \u6807\u8bb0\uff1a\u4fdd\u7559\u4fe1\u606f\uff0c\u53e6\u52a0\u4e00\u5217\ndf[\"amount_missing\"] = df[\"amount\"].isna().astype(int)\n<\/code><\/pre>\n<h2>\u7c7b\u578b\u4e5f\u8981\u4e00\u8d77\u4fee<\/h2>\n<pre><code>df[\"order_date\"] = pd.to_datetime(df[\"order_date\"], errors=\"coerce\")\ndf[\"amount\"] = pd.to_numeric(df[\"amount\"], errors=\"coerce\")\ndf[\"status\"] = df[\"status\"].astype(\"category\")\n<\/code><\/pre>\n<h2>\u4e00\u6761\u5b9e\u7528\u6e05\u6d17\u6d41\u6c34\u7ebf<\/h2>\n<pre><code>def clean_orders(path: str) -&gt; pd.DataFrame:\n    df = pd.read_csv(path)\n    df.columns = [c.strip().lower() for c in df.columns]\n    df = df.drop_duplicates(subset=[\"order_id\"])\n    df[\"amount\"] = pd.to_numeric(df[\"amount\"], errors=\"coerce\")\n    df = df[df[\"amount\"].notna() &amp; (df[\"amount\"] &gt;= 0)]\n    df[\"order_date\"] = pd.to_datetime(df[\"order_date\"], errors=\"coerce\")\n    return df.reset_index(drop=True)\n<\/code><\/pre>\n<p>\u5148\u6807\u51c6\u5316\u5217\u540d \u2192 \u53bb\u91cd \u2192 \u8f6c\u7c7b\u578b \u2192 \u518d\u5904\u7406\u7f3a\u5931\uff0c\u987a\u5e8f\u5f88\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u5206\u6790\u7b2c\u4e00\u5173\u4e0d\u662f\u5efa\u6a21\uff0c\u800c\u662f\u628a\u8868\u8bfb\u8fdb\u6765\u5e76\u6d17\u5e72\u51c0\u3002CSV\/Excel \u8bfb\u5199\u548c\u7f3a\u5931\u503c\u5904\u7406\uff0c\u662f Pandas \u6bcf\u5929\u90fd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-pandas-analysis"],"_links":{"self":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12"}],"version-history":[{"count":0,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.antpython.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}