1-我在当前目录中有一个My_XL_list.txt
文件,其中包含指向不同文件夹中excel文件的路径。我想从该My_XL_list.txt文件中选择第一个路径并创建一个数据框,然后选择excel文件的第二个路径并创建另一个数据框,然后附加两个数据框,然后选择第三个.txt文件的路径,以此类推。最后,我想为所有这些数据框制作一个主Excel文件。
我正在尝试类似的操作,但并没有达到要求的结果。它返回了一个空的Excel文件。
import glob
import pandas as pd
all_data = pd.DataFrame()
path = "rC://Users//Desktop/Stockexchange Q/files/*.xlsx"
for f in glob.glob(path):
df = pd.read_excel(f, index=False, sheet_names='FRJ' )
all_data = all_data.append(df)
all_data.to_excel('All_Merged_Files.xlsx')
答案 0 :(得分:0)
pd.concat
组合它们\n
,用'GQH'
过滤文件,然后将每个路径转换为pathlib
对象。from pathlib import Path
import pandas as pd
# path to file
# p = Path('e:/PythonProjects/stack_overflow/My_XL_list.txt') # update the path to your path
p = Path.cwd() / 'My_XL_list.txt' # if the file is in the current working directory
# extract all the file paths from the file
with p.open('r', encoding='utf-8') as f:
files = [Path(file.strip()) for file in f.readlines() if 'GQH' in file]
# print(files) if you want
[WindowsPath('C:/Users/Desktop/Stockexchange Q/files/names/LT GQH lamas.xlsx'),
WindowsPath('C:/Users/Desktop/Stockexchange Q/files/names/LT1011 GQH abc.xlsx'),
WindowsPath('C:/Users/Desktop/Stockexchange Q/files/names/LT110011 GQH Bostonx.xlsx'),
WindowsPath('C:/Users/Desktop/Stockexchange Q/files/numbers/LT GQH AB.xlsx'),
WindowsPath('C:/Users/Desktop/Stockexchange Q/files/numbers/LT101011 GQH Abbots.xlsx'),
WindowsPath('C:/Users/Desktop/Stockexchange Q/files/numbers/LT1100011 GQH Boston-g.xlsx'),
WindowsPath('C:/Users/Desktop/Stockexchange Q/files/Tums/LT GQH AB.xlsx'),
WindowsPath('C:/Users/Desktop/Stockexchange Q/files/Tums/LT1000111 GQH Abbot-L.xlsx'),
WindowsPath('C:/Users/Desktop/Stockexchange Q/files/Tums/LT110011 GQH Bostonk.xlsx')]
# create a list of dataframes inside the list-comprehension and concat them together
df = pd.concat([pd.read_excel(f, index=False, sheet_names='FRJ') for f in files])
# save file
df.to_excel('GQH_merged.xlsx', index=False)