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é)果生成四個csv文件:

以table_繁體中文為例:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. IntelliJ IDEA恢復(fù)刪除文件的方法2. IntelliJ IDEA配置Tomcat服務(wù)器的方法3. docker鏡像完全卸載的操作步驟4. 使用Maven 搭建 Spring MVC 本地部署Tomcat的詳細(xì)教程5. idea刪除項(xiàng)目的操作方法6. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法7. IntelliJ IDEA導(dǎo)入jar包的方法8. idea導(dǎo)入maven項(xiàng)目的方法9. idea重置默認(rèn)配置的方法步驟10. Docker 部署 Prometheus的安裝詳細(xì)教程

網(wǎng)公網(wǎng)安備