Python 使用pandas读取目录下的所有excel文件并拼接

Python 使用pandas读取目录下的所有excel文件并拼接

  • 代码如下

代码如下

import os
import pandas as pd

#读取所有execl文件并拼接成一个dataframe
def read_excel(path):
    df = pd.DataFrame()
    for file in os.listdir(path):
    	#若对文件名还有其他约束在此处添加,e.g. and file.startswith("升级")
        if file.endswith(".xlsx") :
        	#拼接所有符合要求的excel文件
            df = pd.concat([df, pd.read_excel(path + '\\' + file)],axis=0,ignore_index=True)
    return df


if __name__ == '__main__':
	#r为了不转义
    path = r"D:\Code\resources"
    df = read_excel(path)
    print(df)

你可能感兴趣的:(pandas,python,数据分析)