循环超过2个列表,从多个Excel工作表中创建单独的DFS

发布于 2025-01-26 10:26:23 字数 375 浏览 2 评论 0原文

我在以下Excel工作簿中阅读了40张纸。以下在所有工作表中读取:

df = pd.read_excel(file_path, sheet_name = None)

所有工作表都具有相同的列,但是相关列在每个工作表中的不同行开始,因此我正在编写下面的每张表格以创建每个表:

df2 = df[Sheet_Name]
df2.columns = df2.iloc[20]

我可以使用该代码40次复制此代码,相关的行索引,但是必须有一个可以清洁代码的循环函数。

我正在考虑有2个列表,名称&行索引,该功能迭代为每个工作表创建单独的DF。

这可能吗?

感谢您的帮助

I've read in the below excel workbook which has 40 sheets. The below reads in all the worksheets:

df = pd.read_excel(file_path, sheet_name = None)

All the worksheets have identical columns, but the relevant columns start at different rows in each worksheet, so I'm writing the below to create a df from each sheet:

df2 = df[Sheet_Name]
df2.columns = df2.iloc[20]

I could replicate this code 40 times with the relevant row index, but there has to be a function with a loop that can clean the code.

I was thinking of having 2 lists, sheet name & row index, which the function iterates over to create a separate df for each worksheet.

Is this possible?

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简美 2025-02-02 10:26:24

我应该使用next()

new_dfs = list()
indexes = iter([20, 12, 20])
for sheet in df:
    new_df = df[sheet]
    new_df.colunms = new_df.iloc[next(indexes)]
    new_dfs.append(new_df)

我没有运行此代码,但是这个想法值得尝试

It should be possible using next()

new_dfs = list()
indexes = iter([20, 12, 20])
for sheet in df:
    new_df = df[sheet]
    new_df.colunms = new_df.iloc[next(indexes)]
    new_dfs.append(new_df)

I did not run this code, but the idea is worth trying

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文