处理合并目录下的Excel文件数据并指定列去重

处理合并目录下的Excel文件数据并指定列去重

需求:读取指定目录下的Excel文件并给数据做合并与去重处理

Python代码实现

python 复制代码
import os
import pandas as pd
import warnings
import time
from tqdm import tqdm #进度条展示


def read_excel(path):
    dfs = []
    for file in tqdm(os.listdir(path), desc="Reading Excel Files"):
        if file.endswith(".xlsx"):
            dfs.append(pd.read_excel(os.path.join(path, file)))
    return pd.concat(dfs, ignore_index=True)


if __name__ == '__main__':
    start_time = time.time()
    print("程序开始时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_time)))
    warnings.filterwarnings('ignore')

    file_path = '策略列表'
    file_names = os.listdir(file_path)

    # 打印所有文件名
    for file_name in file_names:
        print(file_name)

    path = r"策略列表"
    print("正在合并表...")
    df = read_excel(path)
    # 根据账号去重数据
    df.drop_duplicates(subset='账号', inplace=True)
    df.to_excel("py合并后数据/合并策略数据-1207.xlsx", sheet_name='Sheet0', header=True, index=False)
    print("合并成功")

    end_time = time.time()
    print("程序结束时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_time)))
    run_time = end_time - start_time
    print("程序运行耗时:%0.2f" % run_time, "s")
相关推荐
金銀銅鐵10 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup1115 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0017 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵19 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf20 小时前
Agent 流程编排
后端·python·agent
copyer_xyf20 小时前
Agent RAG
后端·python·agent
copyer_xyf20 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf20 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python