{"id":704,"date":"2025-02-04T20:28:05","date_gmt":"2025-02-04T12:28:05","guid":{"rendered":"http:\/\/www.cnitw.com\/?p=704"},"modified":"2025-02-04T20:28:05","modified_gmt":"2025-02-04T12:28:05","slug":"python-pandas%e5%ba%93%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e5%a4%84%e7%90%86%e7%9a%84%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"http:\/\/www.cnitw.com\/?p=704","title":{"rendered":"Python\u2013pandas\u5e93\u8fdb\u884c\u6570\u636e\u5904\u7406\u7684\u5b9e\u4f8b"},"content":{"rendered":"\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 Python `pandas` \u5e93\u8fdb\u884c\u6570\u636e\u5904\u7406\u7684\u5b9e\u4f8b\uff0c\u6db5\u76d6\u5e38\u89c1\u64cd\u4f5c\u5982\u6570\u636e\u8bfb\u53d6\u3001\u6e05\u6d17\u3001\u7b5b\u9009\u3001\u805a\u5408\u7b49\u3002<br><br>&#8212;<br><br>### \u793a\u4f8b\u573a\u666f<br>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u9500\u552e\u6570\u636e\u96c6 `sales_data.csv`\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<br>&#8211; `OrderID` (\u8ba2\u5355ID)<br>&#8211; `Product` (\u4ea7\u54c1\u540d\u79f0)<br>&#8211; `Quantity` (\u8d2d\u4e70\u6570\u91cf)<br>&#8211; `Price` (\u5355\u4ef7)<br>&#8211; `OrderDate` (\u8ba2\u5355\u65e5\u671f)<br>&#8211; `CustomerID` (\u5ba2\u6237ID)<br><br>\u76ee\u6807\uff1a\u5206\u6790\u9500\u552e\u6570\u636e\uff0c\u627e\u51fa\u6700\u7545\u9500\u7684\u4ea7\u54c1\u548c\u5ba2\u6237\u6d88\u8d39\u884c\u4e3a\u3002<br><br>&#8212;<br><br>### 1. \u5bfc\u5165\u5e93\u5e76\u8bfb\u53d6\u6570\u636e<br>&#8220;`python<br>import pandas as pd<br><br># \u8bfb\u53d6 CSV \u6587\u4ef6<br>df = pd.read_csv(&#8220;sales_data.csv&#8221;)<br><br># \u67e5\u770b\u524d 5 \u884c\u6570\u636e<br>print(df.head())<br>&#8220;`<br><br>&#8212;<br><br>### 2. \u6570\u636e\u6e05\u6d17<br>#### \u5904\u7406\u7f3a\u5931\u503c<br>&#8220;`python<br># \u68c0\u67e5\u7f3a\u5931\u503c<br>print(df.isnull().sum())<br><br># \u586b\u5145\u7f3a\u5931\u503c\uff08\u4f8b\u5982\u7528\u5e73\u5747\u503c\u586b\u5145 &#8220;Price&#8221; \u5217\u7684\u7f3a\u5931\u503c\uff09<br>df[&#8220;Price&#8221;].fillna(df[&#8220;Price&#8221;].mean(), inplace=True)<br><br># \u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\uff08\u53ef\u9009\uff09<br>df.dropna(inplace=True)<br>&#8220;`<br><br>#### \u5904\u7406\u91cd\u590d\u503c<br>&#8220;`python<br># \u5220\u9664\u91cd\u590d\u884c<br>df.drop_duplicates(inplace=True)<br>&#8220;`<br><br>&#8212;<br><br>### 3. \u6570\u636e\u7b5b\u9009\u4e0e\u6392\u5e8f<br>#### \u7b5b\u9009\u7279\u5b9a\u6761\u4ef6\u7684\u6570\u636e<br>&#8220;`python<br># \u7b5b\u9009\u5355\u4ef7\u5927\u4e8e 100 \u7684\u8ba2\u5355<br>high_price_orders = df[df[&#8220;Price&#8221;] > 100]<br><br># \u7b5b\u9009 2023 \u5e74\u7684\u8ba2\u5355\uff08\u5047\u8bbe OrderDate \u683c\u5f0f\u4e3a &#8220;YYYY-MM-DD&#8221;\uff09<br>df[&#8220;OrderDate&#8221;] = pd.to_datetime(df[&#8220;OrderDate&#8221;])<br>orders_2023 = df[df[&#8220;OrderDate&#8221;].dt.year == 2023]<br>&#8220;`<br><br>#### \u6309\u5217\u6392\u5e8f<br>&#8220;`python<br># \u6309\u5355\u4ef7\u964d\u5e8f\u6392\u5217<br>df_sorted = df.sort_values(&#8220;Price&#8221;, ascending=False)<br>&#8220;`<br><br>&#8212;<br><br>### 4. \u6570\u636e\u805a\u5408\u4e0e\u5206\u7ec4\u7edf\u8ba1<br>#### \u8ba1\u7b97\u603b\u9500\u552e\u989d\uff08\u65b0\u589e\u5217\uff09<br>&#8220;`python<br>df[&#8220;TotalSales&#8221;] = df[&#8220;Quantity&#8221;] * df[&#8220;Price&#8221;]<br>&#8220;`<br><br>#### \u6309\u4ea7\u54c1\u7edf\u8ba1\u603b\u9500\u91cf\u548c\u603b\u9500\u552e\u989d<br>&#8220;`python<br>product_stats = df.groupby(&#8220;Product&#8221;).agg({<br>    &#8220;Quantity&#8221;: &#8220;sum&#8221;,<br>    &#8220;TotalSales&#8221;: &#8220;sum&#8221;<br>}).reset_index()<br><br>print(product_stats)<br>&#8220;`<br><br>#### \u6309\u5ba2\u6237\u7edf\u8ba1\u6d88\u8d39\u6b21\u6570\u548c\u603b\u6d88\u8d39\u91d1\u989d<br>&#8220;`python<br>customer_stats = df.groupby(&#8220;CustomerID&#8221;).agg({<br>    &#8220;OrderID&#8221;: &#8220;count&#8221;,<br>    &#8220;TotalSales&#8221;: &#8220;sum&#8221;<br>}).rename(columns={&#8220;OrderID&#8221;: &#8220;PurchaseCount&#8221;})<br><br>print(customer_stats)<br>&#8220;`<br><br>&#8212;<br><br>### 5. \u6570\u636e\u5408\u5e76<br>\u5047\u8bbe\u6709\u53e6\u4e00\u4e2a\u5ba2\u6237\u4fe1\u606f\u8868 `customer_info.csv`\uff0c\u5305\u542b `CustomerID` \u548c `CustomerName`\uff1a<br>&#8220;`python<br># \u8bfb\u53d6\u5ba2\u6237\u4fe1\u606f\u8868<br>df_customers = pd.read_csv(&#8220;customer_info.csv&#8221;)<br><br># \u5408\u5e76\u9500\u552e\u6570\u636e\u4e0e\u5ba2\u6237\u4fe1\u606f\uff08\u7c7b\u4f3c SQL \u7684 JOIN\uff09<br>merged_df = pd.merge(df, df_customers, on=&#8221;CustomerID&#8221;, how=&#8221;left&#8221;)<br>&#8220;`<br><br>&#8212;<br><br>### 6. \u6570\u636e\u4fdd\u5b58<br>\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u4fdd\u5b58\u4e3a\u65b0\u6587\u4ef6\uff1a<br>&#8220;`python<br># \u4fdd\u5b58\u4e3a CSV<br>merged_df.to_csv(&#8220;processed_sales_data.csv&#8221;, index=False)<br><br># \u4fdd\u5b58\u4e3a Excel<br>merged_df.to_excel(&#8220;processed_sales_data.xlsx&#8221;, index=False)<br>&#8220;`<br><br>&#8212;<br><br>### 7. \u9ad8\u7ea7\u5206\u6790\u793a\u4f8b<br>#### \u65f6\u95f4\u5e8f\u5217\u5206\u6790\uff08\u6309\u6708\u7edf\u8ba1\u9500\u552e\u989d\uff09<br>&#8220;`python<br>monthly_sales = merged_df.resample(&#8220;M&#8221;, on=&#8221;OrderDate&#8221;)[&#8220;TotalSales&#8221;].sum()<br>print(monthly_sales)<br>&#8220;`<br><br>#### \u6700\u7545\u9500\u4ea7\u54c1 Top 5<br>&#8220;`python<br>top_products = product_stats.sort_values(&#8220;TotalSales&#8221;, ascending=False).head(5)<br>print(top_products)<br>&#8220;`<br><br>&#8212;<br><br>### 8. \u6570\u636e\u53ef\u89c6\u5316\uff08\u7ed3\u5408 `matplotlib`\uff09<br>&#8220;`python<br>import matplotlib.pyplot as plt<br><br># \u7ed8\u5236\u6708\u5ea6\u9500\u552e\u989d\u8d8b\u52bf\u56fe<br>monthly_sales.plot(kind=&#8221;line&#8221;, title=&#8221;Monthly Sales Trend&#8221;)<br>plt.xlabel(&#8220;Month&#8221;)<br>plt.ylabel(&#8220;Total Sales&#8221;)<br>plt.show()<br><br># \u7ed8\u5236\u4ea7\u54c1\u9500\u91cf\u67f1\u72b6\u56fe<br>top_products.plot(kind=&#8221;bar&#8221;, x=&#8221;Product&#8221;, y=&#8221;TotalSales&#8221;, title=&#8221;Top 5 Products by Sales&#8221;)<br>plt.ylabel(&#8220;Total Sales&#8221;)<br>plt.show()<br>&#8220;`<br><br>&#8212;<br><br>### \u603b\u7ed3<br>\u901a\u8fc7\u4e0a\u8ff0\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5b9e\u73b0\uff1a<br>1. **\u6570\u636e\u6e05\u6d17**\uff1a\u5904\u7406\u7f3a\u5931\u503c\u3001\u91cd\u590d\u503c\u3002<br>2. **\u6570\u636e\u7b5b\u9009**\uff1a\u6309\u6761\u4ef6\u8fc7\u6ee4\u548c\u6392\u5e8f\u3002<br>3. **\u6570\u636e\u805a\u5408**\uff1a\u5206\u7ec4\u7edf\u8ba1\u5173\u952e\u6307\u6807\u3002<br>4. **\u6570\u636e\u5408\u5e76**\uff1a\u5173\u8054\u591a\u5f20\u8868\u3002<br>5. **\u53ef\u89c6\u5316**\uff1a\u76f4\u89c2\u5c55\u793a\u5206\u6790\u7ed3\u679c\u3002<br><br>\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u6269\u5c55\u66f4\u590d\u6742\u7684\u903b\u8f91\uff08\u5982\u7279\u5f81\u5de5\u7a0b\u3001\u673a\u5668\u5b66\u4e60\u96c6\u6210\u7b49\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 Python `pandas` \u5e93\u8fdb\u884c\u6570\u636e\u5904\u7406\u7684\u5b9e\u4f8b\uff0c\u6db5\u76d6\u5e38\u89c1\u64cd\u4f5c\u5982\u6570\u636e\u8bfb\u53d6\u3001\u6e05\u6d17\u3001\u7b5b\u9009\u3001\u805a\u5408\u7b49\u3002 &#8212; ### \u793a\u4f8b\u573a\u666f\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u9500\u552e\u6570\u636e\u96c6 `sales_data.csv`\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a&#8211; `OrderID` (\u8ba2\u5355ID)&#8211; `Product` (\u4ea7\u54c1\u540d\u79f0)&#8211; `Quantity` (\u8d2d\u4e70\u6570\u91cf)&#8211; `Price` (\u5355\u4ef7)&#8211; `OrderDate` (\u8ba2\u5355\u65e5\u671f)&#8211; `CustomerID` (\u5ba2\u6237ID) \u76ee\u6807\uff1a\u5206\u6790\u9500\u552e\u6570\u636e\uff0c\u627e\u51fa\u6700\u7545\u9500\u7684\u4ea7\u54c1\u548c\u5ba2\u6237\u6d88\u8d39\u884c\u4e3a\u3002 &#8212; ### 1. \u5bfc\u5165\u5e93\u5e76\u8bfb\u53d6\u6570\u636e&#8220;`pythonimport pandas as pd # \u8bfb\u53d6&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-704","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=704"}],"version-history":[{"count":1,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/704\/revisions"}],"predecessor-version":[{"id":705,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=\/wp\/v2\/posts\/704\/revisions\/705"}],"wp:attachment":[{"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=704"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cnitw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}