{"id":96972,"date":"2024-10-20T13:54:19","date_gmt":"2024-10-20T05:54:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96972\/"},"modified":"2024-10-20T13:54:19","modified_gmt":"2024-10-20T05:54:19","slug":"python-pandas-%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e5%88%a9%e7%94%a8pandas%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%88%86%e6%9e%90%e8%88%87%e7%ae%a1%e7%90%86-python-pandas-%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96972\/","title":{"rendered":"Python Pandas \u6578\u64da\u5eab\uff1a\u5229\u7528Pandas\u9032\u884c\u6578\u64da\u5206\u6790\u8207\u7ba1\u7406 (python pandas \u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"python-pandas-%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e5%88%a9%e7%94%a8pandas%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%88%86%e6%9e%90%e8%88%87%e7%ae%a1%e7%90%86-EsSbzMVRkh\">Python Pandas \u6578\u64da\u5eab\uff1a\u5229\u7528Pandas\u9032\u884c\u6578\u64da\u5206\u6790\u8207\u7ba1\u7406<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5206\u6790\u5df2\u6210\u70ba\u5404\u884c\u5404\u696d\u7684\u91cd\u8981\u7d44\u6210\u90e8\u5206\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u64c1\u6709\u5f37\u5927\u7684\u6578\u64da\u5206\u6790\u5eab\uff0c\u5176\u4e2dPandas\u662f\u6700\u53d7\u6b61\u8fce\u7684\u9078\u64c7\u4e4b\u4e00\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0ePandas\u7684\u529f\u80fd\u53ca\u5176\u5728\u6578\u64da\u5206\u6790\u8207\u7ba1\u7406\u4e2d\u7684\u61c9\u7528\u3002<\/p>\n<h2 id=\"pandas%e7%b0%a1%e4%bb%8b-EsSbzMVRkh\">Pandas\u7c21\u4ecb<\/h2>\n<p>Pandas\u662f\u4e00\u500b\u958b\u6e90\u7684Python\u5eab\uff0c\u5c08\u9580\u7528\u65bc\u6578\u64da\u64cd\u4f5c\u548c\u5206\u6790\u3002\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6578\u64da\u7d50\u69cb\uff0c\u5982Series\u548cDataFrame\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u7684\u8655\u7406\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u548c\u76f4\u89c0\u3002Pandas\u7684\u8a2d\u8a08\u7406\u5ff5\u662f\u4f7f\u6578\u64da\u5206\u6790\u7684\u904e\u7a0b\u66f4\u52a0\u9748\u6d3b\u548c\u9ad8\u6548\uff0c\u7121\u8ad6\u662f\u5c0d\u65bc\u5c0f\u578b\u6578\u64da\u96c6\u9084\u662f\u5927\u578b\u6578\u64da\u96c6\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9dpandas-EsSbzMVRkh\">\u5b89\u88ddPandas<\/h2>\n<p>\u8981\u4f7f\u7528Pandas\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd\u5b83\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u5728Python\u74b0\u5883\u4e2d\u5b89\u88ddPandas\uff1a<\/p>\n<pre><code>pip install pandas<\/code><\/pre>\n<h2 id=\"%e5%9f%ba%e6%9c%ac%e6%95%b8%e6%93%9a%e7%b5%90%e6%a7%8b-EsSbzMVRkh\">\u57fa\u672c\u6578\u64da\u7d50\u69cb<\/h2>\n<h3 id=\"series-EsSbzMVRkh\">Series<\/h3>\n<p>Series\u662f\u4e00\u7a2e\u4e00\u7dad\u7684\u6578\u64da\u7d50\u69cb\uff0c\u53ef\u4ee5\u5b58\u5132\u4efb\u4f55\u6578\u64da\u985e\u578b\u3002\u5b83\u985e\u4f3c\u65bcPython\u7684\u5217\u8868\uff0c\u4f46\u5177\u6709\u6a19\u7c64\u7d22\u5f15\u3002\u4ee5\u4e0b\u662f\u5275\u5efaSeries\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import pandas as pd\n\ndata = [1, 2, 3, 4, 5]\ns = pd.Series(data)\nprint(s)<\/code><\/pre>\n<h3 id=\"dataframe-EsSbzMVRkh\">DataFrame<\/h3>\n<p>DataFrame\u662f\u4e00\u7a2e\u4e8c\u7dad\u7684\u6578\u64da\u7d50\u69cb\uff0c\u985e\u4f3c\u65bc\u96fb\u5b50\u8868\u683c\u6216SQL\u8868\u3002\u5b83\u7531\u591a\u500bSeries\u7d44\u6210\uff0c\u4e26\u4e14\u6bcf\u500bSeries\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u6578\u64da\u985e\u578b\u3002\u4ee5\u4e0b\u662f\u5275\u5efaDataFrame\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>data = {\n    '\u59d3\u540d': ['\u5c0f\u660e', '\u5c0f\u7d05', '\u5c0f\u85cd'],\n    '\u5e74\u9f61': [25, 30, 22],\n    '\u57ce\u5e02': ['\u9999\u6e2f', '\u5317\u4eac', '\u4e0a\u6d77']\n}\ndf = pd.DataFrame(data)\nprint(df)<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%88%86%e6%9e%90%e8%88%87%e7%ae%a1%e7%90%86-EsSbzMVRkh\">\u6578\u64da\u5206\u6790\u8207\u7ba1\u7406<\/h2>\n<h3 id=\"%e6%95%b8%e6%93%9a%e6%b8%85%e7%90%86-EsSbzMVRkh\">\u6578\u64da\u6e05\u7406<\/h3>\n<p>\u5728\u9032\u884c\u6578\u64da\u5206\u6790\u4e4b\u524d\uff0c\u6578\u64da\u6e05\u7406\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u6b65\u9a5f\u3002Pandas\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u8655\u7406\u7f3a\u5931\u503c\u548c\u91cd\u8907\u6578\u64da\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u522a\u9664\u7f3a\u5931\u503c\uff1a<\/p>\n<pre><code>df.dropna(inplace=True)<\/code><\/pre>\n<h3 id=\"%e6%95%b8%e6%93%9a%e7%af%a9%e9%81%b8-EsSbzMVRkh\">\u6578\u64da\u7be9\u9078<\/h3>\n<p>Pandas\u5141\u8a31\u7528\u6236\u6839\u64da\u689d\u4ef6\u7be9\u9078\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u6839\u64da\u5e74\u9f61\u7be9\u9078\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>filtered_df = df[df['\u5e74\u9f61'] &gt; 25]\nprint(filtered_df)<\/code><\/pre>\n<h3 id=\"%e6%95%b8%e6%93%9a%e8%81%9a%e5%90%88-EsSbzMVRkh\">\u6578\u64da\u805a\u5408<\/h3>\n<p>Pandas\u9084\u652f\u6301\u6578\u64da\u7684\u805a\u5408\u64cd\u4f5c\uff0c\u4f8b\u5982\u8a08\u7b97\u5e73\u5747\u503c\u3001\u7e3d\u548c\u7b49\u3002\u4ee5\u4e0b\u662f\u8a08\u7b97\u5e74\u9f61\u5e73\u5747\u503c\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>average_age = df['\u5e74\u9f61'].mean()\nprint(average_age)<\/code><\/pre>\n<h2 id=\"%e5%8f%af%e8%a6%96%e5%8c%96%e6%95%b8%e6%93%9a-EsSbzMVRkh\">\u53ef\u8996\u5316\u6578\u64da<\/h2>\n<p>Pandas\u8207Matplotlib\u7b49\u53ef\u8996\u5316\u5eab\u517c\u5bb9\uff0c\u80fd\u5920\u8f15\u9b06\u5730\u5c07\u6578\u64da\u53ef\u8996\u5316\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u7e6a\u88fd\u7c21\u55ae\u5716\u8868\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import matplotlib.pyplot as plt\n\ndf['\u5e74\u9f61'].plot(kind='bar')\nplt.title('\u5e74\u9f61\u5206\u4f48')\nplt.show()<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-EsSbzMVRkh\">\u7e3d\u7d50<\/h2>\n<p>Pandas\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u7528\u6236\u9ad8\u6548\u5730\u9032\u884c\u6578\u64da\u5206\u6790\u8207\u7ba1\u7406\u3002\u7121\u8ad6\u662f\u6578\u64da\u6e05\u7406\u3001\u7be9\u9078\u9084\u662f\u805a\u5408\uff0cPandas\u90fd\u63d0\u4f9b\u4e86\u7c21\u55ae\u6613\u7528\u7684\u63a5\u53e3\uff0c\u8b93\u6578\u64da\u5206\u6790\u8b8a\u5f97\u66f4\u52a0\u76f4\u89c0\u3002\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u6578\u64da\u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u4f3a\u670d\u5668\u74b0\u5883\u81f3\u95dc\u91cd\u8981\u3002\u4e86\u89e3\u66f4\u591a\u95dc\u65bc\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8b93\u60a8\u7684\u6578\u64da\u5206\u6790\u5de5\u4f5c\u66f4\u52a0\u9806\u5229\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Python Pandas\u6578\u64da\u5eab\uff0c\u5b78\u7fd2\u5982\u4f55\u5229\u7528Pandas\u9032\u884c\u9ad8\u6548\u6578\u64da\u5206\u6790\u8207\u7ba1\u7406\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u80fd\u529b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-96972","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96972","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=96972"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96972\/revisions"}],"predecessor-version":[{"id":96973,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96972\/revisions\/96973"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96972"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96972"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96972"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}