Python操作Excel把數(shù)據(jù)分給sheet
需求:根據(jù)country列的不同值,將內(nèi)容分到不同sheet
方法一:
讀取原Excel,根據(jù)country列將不同的內(nèi)容放到不同的sheet,并根據(jù)國(guó)家名稱(chēng)命名,將結(jié)果放到新的輸出文件中。
#!/usr/bin/env python3#讀取Excel文件import pandas as pdinput_file = 'F://python入門(mén)//數(shù)據(jù)2//appname_test.xlsx'output_file = 'F://python入門(mén)//數(shù)據(jù)2//output.xlsx'data_frame = pd.read_excel(input_file,sheet_name=’sum1’,index_col = None)data_frame_country = data_frame[’country’]category_countory = set(data_frame_country)writer = pd.ExcelWriter(output_file)for country in list(category_countory): df = data_frame[data_frame[’country’] == country] df.to_excel(writer, sheet_name= country ,index=False)writer.save()
結(jié)果,生成了output.xlsx,將appname_test.xlsx中的匯總數(shù)據(jù)根據(jù)不同國(guó)家分到了不同sheet:
方法二:
讀取原Excel,根據(jù)country列將不同的內(nèi)容放到不同的CSV文件,并根據(jù)國(guó)家名稱(chēng)命名。
#!/usr/bin/env python3#讀取Excel文件import pandas as pdinput_file = 'F://python入門(mén)//數(shù)據(jù)2//appname_test.xlsx'data_frame = pd.read_excel(input_file,sheet_name=’sum1’,index_col = None)data_frame_country = data_frame[’country’]category_countory = set(data_frame_country)for country in list(category_countory): df = data_frame[data_frame[’country’] == country] df.to_csv('F:/python入門(mén)/數(shù)據(jù)2/table_{}.csv'.format(country), encoding='gbk', index=False)
結(jié)果生成四個(gè)csv文件:
以table_繁體中文為例:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車(chē)輛管理系統(tǒng)3. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算4. ASP.Net MVC利用NPOI導(dǎo)入導(dǎo)出Excel的示例代碼5. jstl 字符串處理函數(shù)6. JSP動(dòng)態(tài)網(wǎng)頁(yè)開(kāi)發(fā)原理詳解7. python實(shí)現(xiàn)布爾型盲注的示例代碼8. 帶你了解CSS基礎(chǔ)知識(shí),樣式9. asp與php中定時(shí)生成頁(yè)面的思路與代碼10. React實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)hook組件實(shí)戰(zhàn)示例
