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. ajax請(qǐng)求添加自定義header參數(shù)代碼2. ASP基礎(chǔ)知識(shí)VBScript基本元素講解3. Python requests庫(kù)參數(shù)提交的注意事項(xiàng)總結(jié)4. Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動(dòng)庫(kù)5. Gitlab CI-CD自動(dòng)化部署SpringBoot項(xiàng)目的方法步驟6. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法7. 利用CSS3新特性創(chuàng)建透明邊框三角8. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……9. asp知識(shí)整理筆記4(問(wèn)答模式)10. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式
