美文网首页Pythoner集中营
在一个文件夹中迭代合并多个文件

在一个文件夹中迭代合并多个文件

作者: 菜菜蜗牛 | 来源:发表于2017-09-14 18:15 被阅读0次

    有多个同样格式的excel表格,需要把这些表格合并起来,再处理。

    #先定义total,我是先读取其中一个文件,这样就可以确保concat的时候很方便
    import_data_path = '指定文件夹'
    os.chdir(import_data_path)
    for filename in os.listdir(import_data_path):
          xls_file = pd.ExcelFile(filename)
          df = xls_file.parse('Grid Results')
          total = pd.concat([total,df])
    total = total.reset_index(drop = True)
    

    最后一步也很重要,concat把小表的索引也直接连接起来,最后输出的总表需要重新定义索引,后面处理才比较方便。

    相关文章

      网友评论

        本文标题:在一个文件夹中迭代合并多个文件

        本文链接:https://www.haomeiwen.com/subject/xoodsxtx.html