简体   繁体   English

将每个 excel 工作表读取为 Python 中的不同 dataframe

[英]Read each excel sheet as a different dataframe in Python

I have an excel file with 40 sheet_names.我有一个包含 40 个 sheet_names 的 excel 文件。 I want to read each sheet to a different dataframe, so I can export an xlsx file for each sheet.我想将每张纸读入不同的 dataframe,因此我可以为每张纸导出一个 xlsx 文件。 Instead of writing all the sheet names one by one, I want to create a loop that will get all sheet names and add them as a variable in the "sheet_name" option of "pandas_read_excel"我不想一个一个地写下所有工作表名称,而是创建一个循环来获取所有工作表名称并将它们作为变量添加到“pandas_read_excel”的“sheet_name”选项中

I am trying to avoid this:我试图避免这种情况:

 df1 = pd.read_excel(r'C:\Users\filename.xlsx', sheet_name= 'Sheet1'); df2 = pd.read_excel(r'C:\Users\filename.xlsx', sheet_name= 'Sheet2');.... df40 = pd.read_excel(r'C:\Users\filename.xlsx', sheet_name= 'Sheet40');

thank you all guys谢谢大家

Specifying sheet_name as None with read_excel reads all worksheets and returns a dict of DataFrames .使用read_excelsheet_name指定为None会读取所有工作表并返回DataFramesdict

 import pandas as pd file = 'C:\Users\filename.xlsx' xl = pd.read_excel(file, sheet_name=None) sheets = xl.keys() for sheet in sheets: xl[sheet].to_excel(f"{sheet}.xlsx")

I think this is what you are looking for.我想这就是你要找的。

 import pandas as pd xlsx = pd.read_excel('file.xlsx', sheet_name=None, header=None) for sheet in xlsx.keys(): xlsx[sheet].to_excel(sheet+'.xlsx', header=False, index=False)

暂无
暂无

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

相关问题 数据帧中每行/索引的 Excel Writer Python 单独工作表 - Excel Writer Python Separate Sheet For Each Row/Index In DataFrame python pandas从同一张Excel表格中读取各种数据框 - python pandas read various dataframe from same excel sheet 如何从 Python 中的 excel 工作表的每个选项卡中读取多个表格? - How to read multiple tables from each tab of an excel sheet in Python? Python Pandas 使用表头读取不同范围的 excel 表 - Python Pandas read excel sheet different range with table headers 将 dataframe 的每个切片保存到特定的 excel 工作表中 - Save each slice of a dataframe into a specific excel sheet 有没有办法从单个 xlsx 读取多个 excel 选项卡/工作表到多个数据帧,每个 dataframe 以工作表名称命名? - is there a way to read multiple excel tab/sheets from single xlsx to multiple dataframes with each dataframe named with sheet name? 如何将 for 循环生成的每个数据帧保存到同一个 Excel 工作表,但不同的工作表? - How to save each dataframe produced by a for loop to the same excel sheet, but different sheets? 如何分组并获取 Dataframe 中每个人的唯一值计数,然后将其写入 Python 中的 Excel 工作表? - How can I groupby and get unique value counts for each person in Dataframe and then write that to an Excel sheet in Python? python数据框自动生成excel表 - python dataframe auto-generate excel sheet Python Excel 在现有工作表中写入 DataFrame - Python Excel write a DataFrame in an existing sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM