{"id":194352,"date":"2020-06-29T08:24:20","date_gmt":"2020-06-29T00:24:20","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=194352"},"modified":"2020-06-22T09:25:43","modified_gmt":"2020-06-22T01:25:43","slug":"python-pandas-api","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-pandas-api.html","title":{"rendered":"Python\u8c03\u7528Pandas\u5e93\u7ed8\u5236\u6570\u636e"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nPandas \u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684 Python \u6570\u636e\u64cd\u4f5c\u5e93\uff0c\u672c\u6559\u7a0b\u5e26\u60a8\u5b66\u4e60\u600e\u6837\u4f7f\u7528\u5b83\u7684 API \u7ed8\u5236\u6570\u636e\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5728\u6709\u5173\u57fa\u4e8e Python \u7684\u7ed8\u56fe\u5e93\u7684\u7cfb\u5217\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5bf9\u4f7f\u7528 Pandas \u8fd9\u4e2a\u975e\u5e38\u6d41\u884c\u7684 Python \u6570\u636e\u64cd\u4f5c\u5e93\u8fdb\u884c\u7ed8\u56fe\u8fdb\u884c\u6982\u5ff5\u6027\u7684\u7814\u7a76\u3002Pandas \u662f Python \u4e2d\u7684\u6807\u51c6\u5de5\u5177\uff0c\u7528\u4e8e\u5bf9\u8fdb\u884c\u6570\u636e\u53ef\u6269\u5c55\u7684\u8f6c\u6362\uff0c\u5b83\u4e5f\u5df2\u6210\u4e3a\u4ece CSV \u548c Excel \u683c\u5f0f\u5bfc\u5165\u548c\u5bfc\u51fa\u6570\u636e\u7684\u6d41\u884c\u65b9\u6cd5\u3002<\/p>\n

\u9664\u6b64\u4e4b\u5916\uff0c\u5b83\u8fd8\u5305\u542b\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u7ed8\u56fe API\u3002\u8fd9\u975e\u5e38\u65b9\u4fbf\uff0c\u4f60\u5df2\u5c06\u6570\u636e\u5b58\u50a8\u5728 Pandas DataFrame \u4e2d\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u76f8\u540c\u7684\u5e93\u8fdb\u884c\u7ed8\u5236\u5462\uff1f<\/p>\n

\u5728\u672c\u7cfb\u5217\u4e2d\uff0c\u6211\u4eec\u5c06\u5728\u6bcf\u4e2a\u5e93\u4e2d\u5236\u4f5c\u76f8\u540c\u7684\u591a\u6761\u5f62\u67f1\u72b6\u56fe\uff0c\u4ee5\u4fbf\u6211\u4eec\u53ef\u4ee5\u6bd4\u8f83\u5b83\u4eec\u7684\u5de5\u4f5c\u65b9\u5f0f\u3002\u6211\u4eec\u4f7f\u7528\u7684\u6570\u636e\u662f 1966 \u5e74\u81f3 2020 \u5e74\u7684\u82f1\u56fd\u5927\u9009\u7ed3\u679c\uff1a<\/p>\n

\"\"<\/p>\n

\u5728\u7ee7\u7eed\u4e4b\u524d\uff0c\u8bf7\u6ce8\u610f\u4f60\u53ef\u80fd\u9700\u8981\u8c03\u6574 Python \u73af\u5883\u6765\u8fd0\u884c\u6b64\u4ee3\u7801\uff0c\u5305\u62ec\uff1a<\/p>\n

    \n
  1. \u8fd0\u884c\u6700\u65b0\u7248\u672c\u7684 Python\uff08\u7528\u4e8e Linux\u3001Mac \u548c Windows \u7684\u8bf4\u660e\uff09<\/li>\n
  2. \u786e\u8ba4\u4f60\u8fd0\u884c\u7684\u662f\u4e0e\u8fd9\u4e9b\u5e93\u517c\u5bb9\u7684 Python \u7248\u672c<\/li>\n<\/ol>\n

    \u6570\u636e\u53ef\u5728\u7ebf\u83b7\u5f97\uff0c\u5e76\u53ef\u4f7f\u7528 Pandas \u5bfc\u5165\uff1a <\/p>\n

    \r\nimport pandas as pd\r\ndf = pd.read_csv('https:\/\/anvil.works\/blog\/img\/plotting-in-python\/uk-election-results.csv')<\/pre>\n

    \u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\u3002\u5728\u672c\u7cfb\u5217\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u4e00\u4e9b\u4ee4\u4eba\u5370\u8c61\u6df1\u523b\u7684\u7b80\u5355 API\uff0c\u4f46\u662f Pandas \u4e00\u5b9a\u80fd\u593a\u51a0\u3002<\/p>\n

    \u8981\u5728 x \u8f74\u4e0a\u7ed8\u5236\u6309\u5e74\u4efd\u548c\u6bcf\u4e2a\u515a\u6d3e\u5206\u7ec4\u7684\u67f1\u72b6\u56fe\uff0c\u6211\u53ea\u9700\u8981\u8fd9\u6837\u505a\uff1a<\/p>\n

    \r\nimport matplotlib.pyplot as plt\r\nax = df.plot.bar(x='year')\r\nplt.show()<\/pre>\n

    \u53ea\u6709\u56db\u884c\uff0c\u8fd9\u7edd\u5bf9\u662f\u6211\u4eec\u5728\u672c\u7cfb\u5217\u4e2d\u521b\u5efa\u7684\u6700\u68d2\u7684\u591a\u6761\u5f62\u67f1\u72b6\u56fe\u3002<\/p>\n

    \u6211\u4ee5\u5bbd\u683c\u5f0f\u4f7f\u7528\u6570\u636e\uff0c\u8fd9\u610f\u5473\u7740\u6bcf\u4e2a\u515a\u6d3e\u90fd\u6709\u4e00\u5217\uff1a<\/p>\n

    \r\n        year  conservative  labour  liberal  others\r\n0       1966           253     364       12       1\r\n1       1970           330     287        6       7\r\n2   Feb 1974           297     301       14      18\r\n..       ...           ...     ...      ...     ...\r\n12      2015           330     232        8      80\r\n13      2017           317     262       12      59\r\n14      2019           365     202       11      72<\/pre>\n

    \u8fd9\u610f\u5473\u7740 Pandas \u4f1a\u81ea\u52a8\u77e5\u9053\u6211\u5e0c\u671b\u5982\u4f55\u5206\u7ec4\uff0c\u5982\u679c\u6211\u5e0c\u671b\u8fdb\u884c\u4e0d\u540c\u7684\u5206\u7ec4\uff0cPandas \u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u91cd\u7ec4 DataFrame\u3002<\/p>\n

    \u4e0e Seaborn \u4e00\u6837\uff0cPandas \u7684\u7ed8\u56fe\u529f\u80fd\u662f Matplotlib \u4e4b\u4e0a\u7684\u62bd\u8c61\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u8981\u8c03\u7528 Matplotlib \u7684 plt.show() \u51fd\u6570\u6765\u5b9e\u9645\u751f\u6210\u7ed8\u56fe\u7684\u539f\u56e0\u3002<\/p>\n

    \u770b\u8d77\u6765\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n

    \"\"<\/p>\n

    \u8c03\u6574\u6837\u5f0f<\/strong><\/span><\/div>\n

    \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5e95\u5c42\u7684 Matplotlib \u65b9\u6cd5\u8f7b\u677e\u5730\u8c03\u6574\u6837\u5f0f\u3002<\/p>\n

    \u9996\u5148\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06 Matplotlib \u989c\u8272\u8868\u4f20\u9012\u5230\u7ed8\u56fe\u51fd\u6570\u6765\u4e3a\u67f1\u72b6\u56fe\u7740\u8272\uff1a<\/p>\n

    \r\nfrom matplotlib.colors import ListedColormap\r\ncmap = ListedColormap(['#0343df', '#e50000', '#ffff14', '#929591'])\r\nax = df.plot.bar(x='year', colormap=cmap)<\/pre>\n

    \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7ed8\u56fe\u51fd\u6570\u7684\u8fd4\u56de\u503c\u8bbe\u7f6e\u5750\u6807\u8f74\u6807\u7b7e\u548c\u6807\u9898\uff0c\u5b83\u53ea\u662f\u4e00\u4e2a Matplotlib \u7684 Axis \u5bf9\u8c61\u3002 <\/p>\n

    \r\nax.set_xlabel(None)\r\nax.set_ylabel('Seats')\r\nax.set_title('UK election results')<\/pre>\n

    \u8fd9\u662f\u73b0\u5728\u7684\u6837\u5b50\uff1a<\/p>\n

    \"\"<\/p>\n

    \u8fd9\u4e0e\u4e0a\u9762\u7684 Matplotlib \u7248\u672c\u51e0\u4e4e\u76f8\u540c\uff0c\u4f46\u662f\u53ea\u7528\u4e86 8 \u884c\u4ee3\u7801\u800c\u4e0d\u662f 16 \u884c\uff01\u6211\u5185\u5fc3\u7684\u4ee3\u7801\u9ad8\u624b\u975e\u5e38\u9ad8\u5174\u3002 <\/p>\n

    \u62bd\u8c61\u5fc5\u987b\u662f\u53ef\u8f6c\u4e49\u7684<\/strong><\/span><\/div>\n

    \u4e0e Seaborn \u4e00\u6837\uff0c\u5411\u4e0b\u8bbf\u95ee Matplotlib API \u8fdb\u884c\u7ec6\u8282\u8c03\u6574\u7684\u80fd\u529b\u786e\u5b9e\u5f88\u6709\u5e2e\u52a9\u3002\u8fd9\u662f\u7ed9\u51fa\u62bd\u8c61\u7d27\u6025\u51fa\u53e3\u4f7f\u5176\u65e2\u5f3a\u5927\u53c8\u7b80\u5355\u7684\u4e00\u4e2a\u5f88\u597d\u7684\u4f8b\u5b50\u3002<\/p>\n

    via: https:\/\/opensource.com\/article\/20\/6\/pandas-python<\/p>\n

    \u4f5c\u8005\uff1aShaun Taylor-Morgan \u9009\u9898\uff1alujun9972 \u8bd1\u8005\uff1ageekpi \u6821\u5bf9\uff1awxy<\/p>\n

    \u672c\u6587\u7531 LCTT \u539f\u521b\u7f16\u8bd1\uff0cLinux\u4e2d\u56fd \u8363\u8a89\u63a8\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u5728\u6709\u5173\u57fa\u4e8e Python \u7684\u7ed8\u56fe\u5e93\u7684\u7cfb\u5217\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5bf9\u4f7f\u7528 Pandas \u8fd9\u4e2a\u975e\u5e38\u6d41\u884c\u7684 Python \u6570\u636e […]<\/p>\n","protected":false},"author":317,"featured_media":170268,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-194352","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/194352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/317"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=194352"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/194352\/revisions"}],"predecessor-version":[{"id":194356,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/194352\/revisions\/194356"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/170268"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=194352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=194352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=194352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}