已有相同表结构不同日期的excel数据
import pandas as pd
import os
import warnings
warnings.filterwarnings('ignore')
def traverse_directory(path):
combined_data = pd.DataFrame()
# 目录下文件遍历
for root, dirs, files in os.walk(path):
for file in files:
path_dev =os.path.join(root, file)
print(path_dev)
# 合并数据帧
df = pd.read_excel(path_dev, engine='openpyxl')
combined_data = pd.concat([combined_data, df], ignore_index=True)
return combined_data
path =r'C:\Users\pengli.she\Desktop\账户表分表'
df_merged = traverse_directory(path)
# 将合并后的数据框保存到新的Excel文件
df_merged.to_excel('001.xlsx', index=False)
df_merged.info()
import pandas as pd
import os
import warnings
warnings.filterwarnings('ignore')
def traverse_directory(path):
combined_data = pd.DataFrame()
# 目录下文件遍历
for root, dirs, files in os.walk(path):
for file in files:
path_dev =os.path.join(root, file)
print(path_dev)
# 合并数据帧
df = pd.read_excel(path_dev, engine='openpyxl')
combined_data = pd.concat([combined_data, df], ignore_index=True)
return combined_data
path =r'C:\Users\pengli.she\Desktop\账户表分表'
df_merged = traverse_directory(path)
# 将合并后的数据框保存到新的Excel文件
df_merged.to_excel('001.xlsx', index=False)
df_merged.info()
合并后的总记录数2338,与实际一致