我几乎将excel文件与python中的pandas合并在一起,但是当我给出路径时,它将无法正常工作。我收到错误“没有这样的文件或目录:'file1.xlsx'”。当我将路径保留为空时,它可以工作,但是我想确定应该从哪个文件夹中获取文件。并且我将文件保存在文件夹“ excel”
cwd = os.path.abspath('/Users/Viktor/downloads/excel') #If i leave it empty and have files in /Viktor it works but I have the desired excel files in /excel
print(cwd)
files = os.listdir(cwd)
df = pd.DataFrame()
for file in files:
if file.endswith('.xlsx'):
df = df.append(pd.read_excel(file), ignore_index=True)
df.head()
df.to_excel(r'/Users/Viktor/Downloads/excel/resultat/merged.xlsx')
答案 0 :(得分:1)
如何实际更改当前工作目录
os.chdir(cwd)
仅打印路径没有帮助。
答案 1 :(得分:1)
pd.read_excel(file)查找相对于脚本执行路径的文件。如果您在'/ Users / Viktor /'中执行,请尝试:
import os
import pandas as pd
cwd = os.path.abspath('/Users/Viktor/downloads/excel') #If i leave it empty and have files in /Viktor it works but I have the desired excel files in /excel
#print(cwd)
files = os.listdir(cwd)
df = pd.DataFrame()
for file in files:
if file.endswith('.xlsx'):
df = df.append(pd.read_excel('downloads/excel/' + file), ignore_index=True)
df.head()
df.to_excel(r'/Users/Viktor/downloads/excel/resultat/merged.xlsx')
答案 2 :(得分:1)
Path.glob()
查找所有文件Path.rglob()
pandas.concat
组合在pd.read_excel
中用list comprehension创建的数据框from pathlib import Path
import pandas as pd
# path to files
p = Path('/Users/Viktor/downloads/excel')
# find the xlsx files
files = p.glob('*.xlsx')
# create the dataframe
df = pd.concat([pd.read_excel(file, ignore_index=True) for file in files])
# save the file
df.to_excel(r'/Users/Viktor/Downloads/excel/resultat/merged.xlsx')