gpt4 book ai didi

excel - For Loop - 将所有 excel 选项卡读入 Panda Df

转载 作者:行者123 更新时间:2023-12-04 20:51:47 24 4
gpt4 key购买 nike

我有一本 .xlsx 书,我想编写一个函数或循环,为 excel 中的每个选项卡创建 Panda(s) DF。例如,假设我有一本名为 book.xlsx 的 Excel 书和名为 sheet1 - sheet6 的标签。我想读取 excel 文件并从函数或循环创建 6 个 Panda DF(sheet1 - sheet6)?

最佳答案

要加载文件:

path = '../files_to_load/my_file.xlsx'
print(path)
excel_file = pd.ExcelFile(path)
print('File uploaded ✔')

要获取特定工作表:

# Get a specific sheet
raw_data = excel_file.parse('sheet1')

这是循环的示例:

您将所有工作表存储在一个列表中。所有工作表都将是数据框

In [1]:
import pandas as pd

path = 'my_path/my_file.xlsx'
excel_file = pd.ExcelFile(path)

sheets = []
for sheet in excel_file.sheet_names:
data = excel_file.parse(sheet)
sheets.append(data)


关于excel - For Loop - 将所有 excel 选项卡读入 Panda Df,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57665095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com