将每个Excel文件的数据量统一减少至120000行

为了将每个Excel文件的数据量统一减少至120000行,可以使用Python的pandas库来加载、修改和保存每个文件。以下是一个简单的Python脚本:

首先,确保已经安装了pandas库,如果没有安装,可以通过以下命令安装:

py 复制代码
pip install pandas

接下来是处理数据的代码:

python 复制代码
import os
import pandas as pd

def trim_excel_files(root_dir, max_rows=120000-1):
    # 遍历根目录下的所有子目录
    for condition_dir in os.listdir(root_dir):
        condition_path = os.path.join(root_dir, condition_dir)
        if os.path.isdir(condition_path):
            # 遍历每个工况目录下的所有Excel文件
            for file in os.listdir(condition_path):
                if file.endswith('.xlsx'):
                    file_path = os.path.join(condition_path, file)
                    # 读取Excel文件
                    df = pd.read_excel(file_path)
                    # 如果行数超过120000,进行缩减
                    if len(df) > max_rows:
                        df = df.iloc[:max_rows]
                    # 保存修改后的数据到原文件
                    df.to_excel(file_path, index=False)
                    print(f"Processed {file_path}")

# 指定数据所在的根目录
root_dir = './程序代码1/data'
trim_excel_files(root_dir)

这个脚本首先定义了一个trim_excel_files函数,它接收一个包含数据文件的根目录作为参数。该函数将遍历根目录下的所有子目录,读取每个.xlsx文件,检查其中的行数,如果超过120000行,则将其缩减至120000行,并将修改后的数据保存回原文件。这个过程会替换掉原有的文件,请确保有备份或是可以接受这种替换。

相关推荐
不太会写16 分钟前
基于Python+django+mysql旅游数据爬虫采集可视化分析推荐系统
python·推荐算法
呱牛do it29 分钟前
Python Matplotlib图形美化指南
开发语言·python·matplotlib
pianmian132 分钟前
python制图之小提琴图
开发语言·python·信息可视化
橙子小哥的代码世界35 分钟前
【机器学习】【KMeans聚类分析实战】用户分群聚类详解——SSE、CH 指数、SC全解析,实战电信客户分群案例
人工智能·python·机器学习·kmeans·数据科学·聚类算法·肘部法
计算机徐师兄35 分钟前
Python基于Flask的豆瓣Top250电影数据可视化分析与评分预测系统(附源码,技术说明)
python·flask·豆瓣top250电影数据可视化·豆瓣top250电影评分预测·豆瓣电影数据可视化分析系统·豆瓣电影评分预测系统·豆瓣电影数据
k layc39 分钟前
【论文解读】《Training Large Language Models to Reason in a Continuous Latent Space》
人工智能·python·机器学习·语言模型·自然语言处理·大模型推理
代码猪猪傻瓜coding1 小时前
【模块】 ASFF 模块
人工智能·深度学习
阿正的梦工坊1 小时前
Sliding Window Attention(滑动窗口注意力)解析: Pytorch实现并结合全局注意力(Global Attention )
人工智能·pytorch·python
GW_Cheng1 小时前
easyexcel和poi同时存在版本问题,使用easyexcel导出excel设置日期格式
excel
喜-喜1 小时前
Python pip 缓存清理:全面方法与操作指南
python·缓存·pip