{"id":1708,"date":"2016-05-20T18:10:00","date_gmt":"2016-05-20T10:10:00","guid":{"rendered":"http:\/\/aliengu.com\/?p=1708"},"modified":"2016-05-20T18:10:00","modified_gmt":"2016-05-20T10:10:00","slug":"%e7%94%a8-python-pandas-%e5%b8%ae%e6%9c%8b%e5%8f%8b%e5%a4%84%e7%90%86%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/aliengu.com\/archives\/1708","title":{"rendered":"\u7528 python + pandas \u5e2e\u670b\u53cb\u5904\u7406\u6570\u636e"},"content":{"rendered":"<p>\u90ed\u8001\u5e08\u53d1\u5c0f\u7684\u8001\u516c\uff0c\u5728\u4e00\u5bb6 Apple \u624b\u673a\u7535\u6c60\u4f9b\u5e94\u5546\u5de5\u4f5c\u3002\u8fd9\u5929\u5728\u670b\u53cb\u5708\u62b1\u6028\uff0c\u7528 Excel \u5904\u7406\u51e0\u5341\u4e07\u6761\u6570\u636e\uff0ci7 \u5904\u7406\u5668 5 \u5206\u949f\u8fdb\u5ea6 1% \u3002\u6211\u4e00\u4e0b\u53cd\u5e94\u8fc7\u6765\uff0c\u8fd9\u73a9\u610f\u7528 Python \u5904\u7406\u8d77\u6765\u5e94\u8be5\u5f88\u5feb\u554a\uff01<br \/>\n\u4e8e\u662f\u7559\u8a00\u7ed9\u4ed6\u8bf4\uff0c\u7528excel\u8dd1\u6700\u8d77\u7801\u51e0\u4e2a\u5c0f\u65f6\uff0c\u8ba9\u4ed6\u628a\u6587\u4ef6\u548c\u5904\u7406\u8981\u6c42\u53d1\u7ed9\u6211\u3002<br \/>\n\u8fc7\u4e86\u4e00\u4f1a\uff0c\u90ae\u7bb1\u6536\u5230\u3002\u5176\u5b9e\u6bd4\u8f83\u7b80\u5355\uff0c\u6587\u4ef6 A \u670940\u4e07\u6761\u7535\u6c60\u6570\u636e\uff0c\u6587\u4ef6 B \u670930\u4e07\u6761\u826f\u54c1\u7535\u6c60\u6570\u636e\uff0c\u8981\u7528 A \u51cf\u6389 B\uff0c\u5269\u4e0b\u6765\u7684\u5c31\u662f\u4e0d\u826f\u7535\u6c60\u7684\u6570\u636e\u3002\u6570\u636e\u5904\u7406\u7684\u76ee\u6807\u5373\u5f97\u5230\u6c47\u603b\u4e86\u4e0d\u826f\u7535\u6c60\u6570\u636e\u7684\u6587\u4ef6 C \u3002<br \/>\n\u8fd9\u91cc\u6709\u4e2a\u5c0f\u63d2\u66f2\uff1a\u6587\u4ef6 A \u670940\u591a\u4e07\u884c\u6570\u636e\uff0c\u6211\u7528 Mac \u7684 Number \u6253\u5f00\uff0c\u4ec5\u80fd\u663e\u793a 65535 \u6761\u3002\u770b\u6765 Numbers \u5904\u7406\u7a0d\u5927\u4e00\u70b9\u7684\u6570\u636e\u5c31\u5b8c\u5168\u4e0d\u884c\u4e86\u3002\u4e3a\u4e86\u80fd\u987a\u5229\u68c0\u89c6\u6570\u636e\u7ed3\u679c\uff0c\u53c8\u4e0b\u8f7d\u4e86 OpenOffice\u3002<br \/>\n\u867d\u7136\u4e4b\u524d\u6ca1\u63a5\u89e6\u8fc7 Python \u7684\u6570\u636e\u5904\u7406\uff0c\u4f46\u662f\u5b66\u4e60\u8fc7\u7528 Python \u7684 openpyxl \u5904\u7406 Excel \u8868\u683c\uff0c\u6240\u4ee5\u6211\u7684\u7b2c\u4e00\u53cd\u5e94\u5c31\u662f\u7528 xlrd \u8fd9\u79cd\u7b2c\u4e09\u65b9\u5e93\u6765\u5904\u7406 Excel \u65e0\u6cd5\u5feb\u901f\u5904\u7406\u7684\u6587\u4ef6\u3002\u4f46\u662f\u5728\u5b66\u4e60 xlrd \u7684\u8fc7\u7a0b\u4e2d\uff0c\u53d1\u73b0 xlrd \u53ef\u4ee5\u6bd4\u8f83\u597d\u7684\u8bfb\u53d6\u6587\u4ef6\uff0c\u5374\u4e0d\u80fd\u5f88\u597d\u7684\u5199\u5165\u6587\u4ef6\uff0c\u4e8e\u662f \u53c8\u4e0b\u8f7d\u4e86\u5bf9 Excel \u5199\u5165\u652f\u6301\u8f83\u597d\u7684 xlwt \u3002<br \/>\n\u6298\u817e\u4e86\u534a\u5929\uff0c\u867d\u7136\u5b9e\u73b0\u8bfb\u5199\uff0c\u4f46\u662f\u5bf9\u8868\u683c\u7684\u5904\u7406\u8fd8\u662f\u4e0d\u6ee1\u610f\u3002\u968f\u4fbf\u641c\u4e00\u641c\uff0c\u53d1\u73b0 pandas \u4e5f\u53ef\u4ee5\u505a\u8fd9\u4e2a\u5de5\u4f5c\u3002\u4e8e\u662f\u8f6c\u5411 pandas \u3002<br \/>\npandas \u652f\u6301\u8bfb\u53d6\/\u5199\u5165 XLSX \u548c CSV \u683c\u5f0f\uff0c\u7531\u4e8e\u6211\u7528\u7684\u662f Mac \uff0c\u56e0\u6b64\u5c06\u6587\u4ef6\u5148\u7edf\u4e00\u8f6c\u6362\u6210\u4e86 CSV \u3002<br \/>\n\u9996\u5148\u7528 pandas \u8bfb\u53d6 CSV \u6587\u4ef6\u5e76\u8f6c\u5316\u4e3a DataFrame\uff1a<\/p>\n<pre class=\"lang:default decode:true \">df = pd.read_csv(workbook, low_memory = False)<\/pre>\n<p>\u7136\u540e\u5c06\u6587\u4ef6 B \u590d\u5236\u5230\u6587\u4ef6 A \u9700\u8981\u53bb\u91cd\u7684\u5217\u4e0b\u65b9\uff08\u8fd9\u91cc\u53ef\u4ee5\u7528\u4ee3\u7801\u64cd\u4f5c\uff0c\u4f46\u662f\u6211\u6ca1\u6709\u67e5\u64cd\u4f5c\u51fd\u6570\uff0c\u56e0\u4e3a\u611f\u89c9\u624b\u52a8\u590d\u5236\u7c98\u8d34\u4e5f\u5f88\u65b9\u4fbf\uff09\u3002\u518d\u7528 drop_duplicates \u51fd\u6570\u53bb\u6389\u91cd\u590d\u7684\u9879\uff08\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\uff0cdrop_duplicates \u6709\u597d\u51e0\u4e2a\u53c2\u6570\uff0c\u53ef\u4ee5\u9009\u62e9\u7559\u4e0b\u91cd\u590d\u9879\u4e2d\u7684\u7b2c\u4e00\u9879\u3001\u6700\u540e\u4e00\u9879\u3001\u6216\u8005\u5168\u5220\u6389\uff0c\u89c6\u9700\u6c42\u800c\u5b9a\uff09\u3002<\/p>\n<pre class=\"lang:default decode:true \">new_df = df.drop_duplicates(subset = '\u8981\u53bb\u91cd\u7684\u5217\u540d\u79f0\uff0c\u5fc5\u987b\u82f1\u6587', keep = False)<\/pre>\n<p>\u6700\u540e\u7528 to_csv \u51fd\u6570\u4fdd\u5b58\u6210\u65b0\u7684 CSV \u6587\u4ef6\u5373\u53ef\u3002\u811a\u672c\u4e00\u5171 5 \u884c\u4ee3\u7801\uff0c\u8fd0\u7b97\u5904\u7406\u65f6\u95f4\u53ea\u6709\u51e0\u79d2\u949f\u3002<br \/>\n\u4e3a\u4e86\u51cf\u5c11\u670b\u53cb\u4ee5\u540e\u7684\u5de5\u4f5c\u91cf\uff0c\u6211\u628a\u811a\u672c\u5199\u597d\u53d1\u7ed9\u4ed6\uff0c\u5e76\u5c06\u300e\u6587\u4ef6\u540d\u300f\u7b49\u53ef\u81ea\u5b9a\u4e49\u7684\u90e8\u5206\u7559\u7a7a\u4e86\u3002\u6709\u5174\u8da3\u8bf7\u70b9\u00a0<a href=\"https:\/\/github.com\/zjuguxi\/Practice_Project\/blob\/master\/drop_duplicates\/miner_deng.py\" target=\"_blank\" rel=\"noopener noreferrer\">\u8fd9\u91cc<\/a>\u00a0\u770b\u6211\u5199\u7684\u811a\u672c\u3002<br \/>\n\u8fd9\u4e2a 5 \u884c\u4ee3\u7801\u811a\u672c\u6211\u5927\u7ea6\u5199\u4e86\u4e09\u4e2a\u5c0f\u65f6\uff0c\u4e3b\u8981\u65f6\u95f4\u90fd\u5728\u5b66\u4e60\u3002\u5728\u5e2e\u4e86\u670b\u53cb\u7684\u5fd9\u65f6\uff0c\u4e5f\u5b9e\u73b0\u4e86\u6211 Python \u5b66\u4e60\u53f2\u7684\u597d\u51e0\u4e2a\u7b2c\u4e00\u6b21\uff1a<br \/>\n\u7b2c\u4e00\u6b21\u73b0\u5b66\u73b0\u5356\uff0c\u5f53\u5929\u4e0b\u5348\u5c31\u7528\u964c\u751f\u7684\u77e5\u8bc6\u5b8c\u6210\u4efb\u52a1\u3002<br \/>\n\u7b2c\u4e00\u6b21\u5e2e\u670b\u53cb\u89e3\u51b3\u4e86\u5b9e\u9645\u95ee\u9898\u3002<br \/>\n\u7b2c\u4e00\u6b21\u5b8c\u6210\u4e86\u4e00\u4ef6\u6570\u636e\u5904\u7406\u5de5\u4f5c\u3002<br \/>\n\u975e\u5e38\u5f00\u5fc3\uff0c\u975e\u5e38\u503c\u5f97\u7eaa\u5ff5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u90ed\u8001\u5e08\u53d1\u5c0f\u7684\u8001\u516c\uff0c\u5728\u4e00\u5bb6 Apple \u624b\u673a\u7535\u6c60\u4f9b\u5e94\u5546\u5de5\u4f5c\u3002\u8fd9\u5929\u5728\u670b\u53cb\u5708\u62b1\u6028\uff0c\u7528 Excel \u5904\u7406\u51e0\u5341\u4e07\u6761\u6570\u636e\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1710,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[32,36,287],"class_list":["post-1708","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-4","tag-pandas","tag-python","tag-287"],"_links":{"self":[{"href":"https:\/\/aliengu.com\/wp-json\/wp\/v2\/posts\/1708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aliengu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aliengu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aliengu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aliengu.com\/wp-json\/wp\/v2\/comments?post=1708"}],"version-history":[{"count":0,"href":"https:\/\/aliengu.com\/wp-json\/wp\/v2\/posts\/1708\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aliengu.com\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/aliengu.com\/wp-json\/wp\/v2\/media?parent=1708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aliengu.com\/wp-json\/wp\/v2\/categories?post=1708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aliengu.com\/wp-json\/wp\/v2\/tags?post=1708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}