【问题标题】:How can I create an editing loop in python for a whole folder of excel sheets如何在python中为整个excel表文件夹创建一个编辑循环
【发布时间】:2021-10-17 15:49:24
【问题描述】:

我需要以同样的方式修改大约 200 个 excel 文件(删除前 2 行)。有没有办法创建一个 for 循环读取每个 excel 文件并对其进行编辑?

编辑很简单:

import os
import pandas as pd
file=pd.read_excel('file.xlsx')
file=file.iloc[2:]

for 循环是:

for filename in os.listdir('Folder'):
    if filename.endswith('.xlsx'):
        

如何将这段代码正确地集成到我的循环中,以便编辑文件夹中的每个文件?

【问题讨论】:

    标签: python pandas loops data-science


    【解决方案1】:
    in_path = 'Folder'
    out_path = 'output'
    if not os.path.exists(out_path):
        os.mkdir(out_path)
    
    for filename in os.listdir(in_path):
        if filename.endswith('.xlsx'):
            df = pd.read_excel(os.path.join(in_path, filename))
            df = df.iloc[2:]
            df.to_excel(os.path.join(out_path, filename), index=False)
    

    【讨论】:

    • 效果很好,非常感谢!
    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多