猿问

根据 Python 中的特定标头定位数据帧并进行连接

如果我有很多excel文件如下(这里只是两个例子):

数据1.xlsx

数据2.xlsx

http://img4.mukewang.com/616e7c680001841f02850226.jpg

是否有可能我只使用列的部分id, a, b, c并忽略其余部分并将所有这些文件连接到 Python 中的一个新的 excel 文件中。谢谢。


http://img4.mukewang.com/616e7c7400014d6b02870172.jpg

这是我尝试过的:


import os


for root, dirs, files in os.walk(src, topdown=False):

    for file in files:

        if file.endswith('.xlsx') or file.endswith('.xls'):

            #print(os.path.join(root, file))

            try:

                df0 = pd.read_excel(os.path.join(root, file))

                #print(df0)

            except:

                continue

            df1 = pd.DataFrame(columns = [columns_selected])

            df1 = df1.append(df0, ignore_index = True)

            print(df1)

            df1.to_excel('test.xlsx', index = False)


繁花如伊
浏览 219回答 2
2回答

哈士奇WWW

根据您对多个 excel 文件的要求扩展 @Charles R 的答案。# get all the filesos.chdir('C:\ExcelWorkbooksFolder')FileList = glob.glob('*.xlsx')print(FileList)进而:for File in FileList:    for x in File:        # the rest of the code for reading
随时随地看视频慕课网APP

相关分类

Python
我要回答