简体   繁体   English

使用pandas在excel中创建多个工作表以循环工作表名称

[英]Create multiple sheets in excel using pandas to loop through sheet names

I am trying to create multiple sheets in excel, using pandas integration with xlsxwriter in Python, based on data represented in categories我正在尝试根据类别中表示的数据,在 excel 中创建多个工作表,使用 Pandas 与 Python 中的 xlsxwriter 集成

Categories ="Autoimmune Diseases","Blood","Cancer: Head & Neck","Cancer: Hematological","Cancer: Other","Cardiovascular","Dermatological Treatment","Eyes","Immune Deficiencies","Infectious Diseases","Liver Disease","Neurological","Neurotology","Pain Management","Rare Diseases","Respiratory Diseases","Women's Health"

Right now my code looks like:现在我的代码看起来像:

writer = pd.ExcelWriter(Individualreport, engine='xlsxwriter')
Categories ="Autoimmune Diseases","Blood","Cancer: Head & Neck","Cancer: Hematological","Cancer: Other","Cardiovascular","Dermatological Treatment","Eyes","Immune Deficiencies","Infectious Diseases","Liver Disease","Neurological","Neurotology","Pain Management","Rare Diseases","Respiratory Diseases","Women's Health"

Overview = pd.read_excel(READ,sheet_name='Overview', header=None)
OverviewCols = pd.read_excel(READ, sheet_name='Overview', header=None,nrows=1).values[0]
Overview.columns = OverviewCols

auto = Overview.loc[(Overview['Category']=="Autoimmune Diseases")]
auto.to_excel(writer, sheet_name='Auto', startrow= over_row, startcol=over_col, header=False, index=False)

and I would have to repeat for all 17 categories to make each sheet like:我必须对所有 17 个类别重复,以使每张纸都像:

blood = Overview.loc[(Overview['Category']=="Blood")]
blood.to_excel(writer, sheet_name='Blood', startrow= over_row, startcol=over_col, header=False, index=False)

Is there a way to loop through the list to simplify the code?有没有办法遍历列表来简化代码? I know this isn't right, but something like:我知道这是不对的,但类似于:

for i in Categories:
    i = Overview.loc[(Overview['Category']==i)]
    i.to_excel(writer, sheet_name=i, startrow= over_row, startcol=over_col, header=False, index=False)

referring to your code:参考你的代码:

for i in Categories:
    i = Overview.loc[(Overview['Category']==i)]
    i.to_excel(writer, sheet_name=i, startrow= over_row, startcol=over_col, header=False, index=False)

The variable "Categories" is a tuple of strings - you are trying to use a string "i" as a variable eg:变量 "Categories" 是一个字符串元组 - 您正在尝试使用字符串 "i" 作为变量,例如:

"Blood" = Overview.loc[(Overview['Category']=="Blood")

which won t work.这行不通。

Have your tried to use a free variable name?您是否尝试过使用自由变量名?

for cat in Categories:
    test = Overview.loc[(Overview['Category']==cat)]
    test.to_excel(writer, sheet_name=cat, startrow=over_row, startcol=over_col, header=False, index=False)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 dfs 导出到带有多个工作表和不同工作表名称的 excel 熊猫 - How to export dfs to excel with multiple sheets and different sheet names pandas 使用循环创建带有 Dataframe Pandas 的 Excel 表格 - Using Loop to Create Excel Sheets with Dataframe Pandas Python / Pandas:使用“for-loop”将多个Dataframes写入Excel工作表 - Python/Pandas: writing multiple Dataframes to Excel sheets using a “for-loop” Pandas循环通过Excel张和append到df - Pandas loop through Excel sheets and append to df 我尝试使用 python openpyxl 在 excel 中创建多个工作表以在循环中存储不同的值,但它只创建了一张工作表 - i tried to create multiple sheets in excel using python openpyxl to store different values in loop, but it creates only one sheet 通过python pandas将3个SQL查询结果导出到excel表中的3个表中 - Export 3 SQL Query results to 3 sheets in an excel sheet through python pandas 如何遍历 excel 工作表并使用 python 在每张工作表上执行相同的任务 - How to loop through excel sheets and perform same task on each sheet using python 使用熊猫从Excel转换为CSV,我有多个可能的Excel工作表名称 - Using Pandas to Convert from Excel to CSV and I Have Multiple Possible Excel Sheet Names 循环通过 excel 工作表并根据条件将每个工作表保存到 csv - Loop through excel sheets and save each sheet into a csv based on a condition 使用 Python/Pandas 创建具有多张工作表的 Excel 文件 - Create an Excel file with multiple sheets with Python/Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM