Python操作Excel把數(shù)據(jù)分給sheet
需求:根據(jù)country列的不同值,將內(nèi)容分到不同sheet
方法一:
讀取原Excel,根據(jù)country列將不同的內(nèi)容放到不同的sheet,并根據(jù)國家名稱命名,將結(jié)果放到新的輸出文件中。
#!/usr/bin/env python3#讀取Excel文件import pandas as pdinput_file = 'F://python入門//數(shù)據(jù)2//appname_test.xlsx'output_file = 'F://python入門//數(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ù)不同國家分到了不同sheet:
方法二:
讀取原Excel,根據(jù)country列將不同的內(nèi)容放到不同的CSV文件,并根據(jù)國家名稱命名。
#!/usr/bin/env python3#讀取Excel文件import pandas as pdinput_file = 'F://python入門//數(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入門/數(shù)據(jù)2/table_{}.csv'.format(country), encoding='gbk', index=False)
結(jié)果生成四個(gè)csv文件:
以table_繁體中文為例:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python 實(shí)現(xiàn)圖片修復(fù)(可用于去水印)2. Android自定義控件實(shí)現(xiàn)方向盤效果3. asp讀取xml文件和記數(shù)4. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案5. XHTML 1.0:標(biāo)記新的開端6. Java 生成帶Logo和文字的二維碼7. xml中的空格之完全解說8. python中的socket實(shí)現(xiàn)ftp客戶端和服務(wù)器收發(fā)文件及md5加密文件9. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?10. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)
