{"id":237052,"date":"2022-03-16T08:33:50","date_gmt":"2022-03-16T00:33:50","guid":{"rendered":"https:\/\/gulass.cn\/?p=237052"},"modified":"2022-03-03T10:34:28","modified_gmt":"2022-03-03T02:34:28","slug":"python-pandas-excel","status":"publish","type":"post","link":"https:\/\/gulass.cn\/python-pandas-excel.html","title":{"rendered":"python pandas\u5e93\u8bfb\u53d6excel\/csv\u4e2d\u6307\u5b9a\u884c\u6216\u5217\u6570\u636e\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u901a\u8fc7\u9605\u8bfb\u8868\u683c,\u53ef\u4ee5\u53d1\u73b0Pandas\u4e2d\u63d0\u4f9b\u4e86\u975e\u5e38\u4e30\u5bcc\u7684\u6570\u636e\u8bfb\u5199\u65b9\u6cd5,\u4e0b\u9762\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8epython\u5229\u7528pandas\u5e93\u8bfb\u53d6excel\/csv\u4e2d\u6307\u5b9a\u884c\u6216\u5217\u6570\u636e\u7684\u76f8\u5173\u8d44\u6599,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u5f15\u8a00<\/strong><\/div>\n

\u5173\u952e\uff01\uff01\uff01\uff01\u4f7f\u7528loc\u51fd\u6570\u6765\u67e5\u627e\u3002<\/p>\n

\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u6f14\u793a\uff1a<\/p>\n

\u6709\u4ee5\u4e0b\u540d\u4e3atry.xlsx\u8868\uff1a<\/p>\n

\"\"<\/p>\n

1.\u6839\u636eindex\u67e5\u8be2<\/strong><\/div>\n

\u6761\u4ef6\uff1a\u9996\u5148\u5bfc\u5165\u7684\u6570\u636e\u5fc5\u987b\u7684\u6709index<\/p>\n

\u6216\u8005\u81ea\u5df1\u6dfb\u52a0\u5427\uff0c\u65b9\u6cd5\u7b80\u5355\uff0c\u8bfb\u53d6excel\u6587\u4ef6\u65f6\u76f4\u63a5\u52a0index_col<\/p>\n

\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n

import pandas as pd                      #\u5bfc\u5165pandas\u5e93\r\n \r\nexcel_file = '.\/try.xlsx'               #\u5bfc\u5165excel\u6570\u636e\r\ndata = pd.read_excel(excel_file, index_col='\u59d3\u540d')        \r\n#\u8fd9\u4e2a\u7684index_col\u5c31\u662findex\uff0c\u53ef\u4ee5\u9009\u62e9\u4efb\u610f\u5b57\u6bb5\u4f5c\u4e3a\u7d22\u5f15index\uff0c\u8bfb\u5165\u6570\u636e\r\nprint(data.loc['\u674e\u56db'])\r\n\u6253\u5370\u7ed3\u679c\u5c31\u662f<\/pre>\n

\u90e8\u95e8 B<\/p>\n

\u5de5\u8d44 6600<\/p>\n

Name: \u674e\u56db, dtype: object\uff08\u6ce8\u610f\u70b9:\u7d22\u5f15\uff09<\/p>\n

2.\u5df2\u77e5\u6570\u636e\u5728\u7b2c\u51e0\u884c\u627e\u5230\u60f3\u8981\u7684\u6570\u636e<\/strong><\/div>\n

\u5047\u5982\u6211\u4eec\u7684\u8868\u4e2d\uff0c\u6709\u67d0\u4e2a\u5458\u5de5\u7684\u5de5\u8d44\u6570\u636e\u4e3a\u7a7a\u4e86\uff0c\u90a3\u6211\u4eec\u600e\u4e48\u627e\u5230\u81ea\u5df1\u60f3\u8981\u7684\u6570\u636e\u5462\u3002<\/p>\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

for i in data.columns:\r\n    for j in range(len(data)):\r\n        if (data[i].isnull())[j]:\r\n            bumen = data.iloc[j, [0]]                             #\u627e\u51fa\u7f3a\u5931\u503c\u6240\u5728\u7684\u90e8\u95e8\r\n            data[i][j] = charuzhi(bumen)<\/pre>\n

\u539f\u7406\u5f88\u7b80\u5355\uff0c\u9996\u5148\u68c0\u7d22\u5168\u90e8\u7684\u6570\u636e\uff0c\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u7528pandas\u4e2d\u7684iloc\u51fd\u6570\u3002\u4e0a\u9762\u7684iloc[j, [2]]\u4e2dj\u662f\u5177\u4f53\u7684\u4f4d\u7f6e\uff0c\u30100\u3011\u662f\u4f60\u8981\u5f97\u5230\u7684\u6570\u636e\u6240\u5728\u7684column<\/p>\n

3.\u6839\u636e\u6761\u4ef6\u67e5\u8be2\u627e\u5230\u6307\u5b9a\u884c\u6570\u636e<\/strong><\/div>\n

\u4f8b\u5982\u67e5\u627eA\u90e8\u95e8\u6240\u6709\u6210\u5458\u7684\u7684\u59d3\u540d\u548c\u5de5\u8d44\u6216\u8005\u5de5\u8d44\u4f4e\u4e8e3000\u7684\u4eba\uff1a<\/p>\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\"\"\"\u6839\u636e\u6761\u4ef6\u67e5\u8be2\u67d0\u884c\u6570\u636e\"\"\"\r\nimport pandas as pd            #\u5bfc\u5165pandas\u5e93\r\n \r\nexcel_file = '.\/try.xlsx'        #\u5bfc\u5165\u6587\u4ef6\r\ndata = pd.read_excel(excel_file)      #\u8bfb\u5165\u6570\u636e\r\n \r\nprint(data.loc[data['\u90e8\u95e8'] == 'A', ['\u59d3\u540d', '\u5de5\u8d44']])     #\u90e8\u95e8\u4e3aA\uff0c\u6253\u5370\u59d3\u540d\u548c\u5de5\u8d44\r\nprint(data.loc[data['\u5de5\u8d44'] < 3000, ['\u59d3\u540d','\u5de5\u8d44']])    #\u67e5\u627e\u5de5\u8d44\u5c0f\u4e8e3000\u7684\u4eba<\/pre>\n

\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

\"\"<\/p>\n

\u82e5\u8981\u628a\u8fd9\u4e9b\u6570\u636e\u72ec\u7acb\u751f\u6210excel\u6587\u4ef6\u6216\u8005csv\u6587\u4ef6\uff1a<\/p>\n

\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801<\/p>\n

\"\"\"\u5bfc\u51fa\u4e3aexcel\u6216csv\u6587\u4ef6\"\"\"\r\n#\u5355\u6761\u4ef6\r\ndataframe_1 = data.loc[data['\u90e8\u95e8'] == 'A', ['\u59d3\u540d', '\u5de5\u8d44']]\r\n#\u5355\u6761\u4ef6\r\ndataframe_2 = data.loc[data['\u5de5\u8d44'] < 3000, ['\u59d3\u540d', '\u5de5\u8d44']]\r\n#\u591a\u6761\u4ef6\r\ndataframe_3 = data.loc[(data['\u90e8\u95e8'] == 'A')&(data['\u5de5\u8d44'] < 3000), ['\u59d3\u540d', '\u5de5\u8d44']]\r\n#\u5bfc\u51fa\u4e3aexcel\r\ndataframe_1.to_excel('dataframe_1.xlsx')\r\ndataframe_2.to_excel('dataframe_2.xlsx')<\/pre>\n
4.\u627e\u51fa\u6307\u5b9a\u5217<\/strong><\/div>\n
data['columns']  #columns\u5373\u4f60\u9700\u8981\u7684\u5b57\u6bb5\u540d\u79f0\u5373\u53ef\r\n#\u6ce8\u610f\u8fd9\u5217\u7684columns\u4e0d\u80fd\u662findex\u7684\u540d\u79f0\r\n#\u5982\u679c\u8981\u6253\u5370index\u7684\u8bdd\u5c31data.index\r\ndata.columns  #\u4e0e\u4e0a\u9762\u7684\u4e00\u6837<\/pre>\n

\u4ee5\u4e0a\u5168\u8fc7\u7a0b\u7528\u5230\u7684\u5e93\uff1a<\/p>\n

pandas\uff0cxlrd , openpyxl<\/p>\n

5.\u627e\u51fa\u6307\u5b9a\u7684\u884c\u548c\u6307\u5b9a\u7684\u5217<\/strong><\/div>\n

\u4e3b\u8981\u4f7f\u7528\u7684\u5c31\u662f\u51fd\u6570iloc<\/p>\n

data.iloc[:,:2]  #\u5373\u5168\u90e8\u884c\uff0c\u524d\u4e24\u5217\u7684\u6570\u636e<\/pre>\n

\u9017\u53f7\u524d\u662f\u884c\uff0c\u9017\u53f7\u540e\u662f\u5217\u7684\u8303\u56f4\uff0c\u5f88\u5bb9\u6613\u7406\u89e3<\/p>\n

6.\u5728\u89c4\u5b9a\u8303\u56f4\u5185\u627e\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e<\/strong><\/div>\n
data.iloc[:10,:][data.\u5de5\u8d44>6000]<\/pre>\n

\u8fd9\u6837\u5373\u53ef\u627e\u51fa\u524d11\u884c\u91cc\u5de5\u8d44\u5927\u4e8e6000\u7684\u6240\u6709\u4eba\u7684\u4fe1\u606f\u4e86<\/p>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8epython pandas\u5e93\u8bfb\u53d6excel\/csv\u4e2d\u6307\u5b9a\u884c\u6216\u5217\u6570\u636e\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5173\u952e\uff01\uff01\uff01\uff01\u4f7f\u7528loc\u51fd\u6570\u6765\u67e5\u627e\u3002 \u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u6f14\u793a\uff1a \u6709\u4ee5\u4e0b\u540d\u4e3atry.xlsx\u8868\uff1a \u6761\u4ef6\uff1a\u9996\u5148\u5bfc\u5165\u7684\u6570\u636e […]<\/p>\n","protected":false},"author":1479,"featured_media":231815,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-237052","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/237052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1479"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=237052"}],"version-history":[{"count":5,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/237052\/revisions"}],"predecessor-version":[{"id":237059,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/237052\/revisions\/237059"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/231815"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=237052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=237052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=237052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}