Python data processing | Batch extract csv files under a folder, each csv file extracts specific columns according to the column index, and saves the extracted data to a new folder


1. The source of the problem

worth 30 30A question and answer of 30 yuan, I just saw it before I was going to eat that day, it was done in a few minutes, and I added a chicken leg for lunch~~
insert image description here


Second, the solution

The implementation code is as follows:

import os
import pandas as pd

path1 = "你放所有csv的文件夹路径"             # 你放所有csv的文件夹路径

path2 = "./data"         # 新建一个文件夹 文件夹名data  当前目录下  你也可以指定
if not os.path.exists(path2):
    os.mkdir(path2)

for filename in os.listdir(path):
    # 是csv文件
    if filename.endswith(".csv"):
        file_path1 = path1 + "/" + filename
        # 读取csv可能会编码错误  还可加参数 engine="python" 或者指定编码 encoding="utf-8"就可以解决
        df1 = pd.read_csv(file_path1)
        # 索引指定列的数据
        df2 = df1[['时间', '风机', '平均齿轮箱主滤芯1_1压力',
                  '平均齿轮箱主滤芯1_2压力', '平均齿轮箱主滤芯2_1压力', 
                   '平均齿轮箱主滤芯2_2压力']]
        # 保存到新建的文件夹 文件夹名data下面
        df2.to_csv(path2 + "/" + filename,
                   index=False, encoding="gb2312")
        
print("完成!")

Summary: Will Python PythonThe basic file operations of Python , Pandas reading data, indexing the data of the specified column, and saving the data can be solved (a matter of a few minutes) . When reading csv, the encoding may be wrong. Add the parameter engine="python", or specify the encoding="utf-8/gbk/gb2312", and try more to solve it. Save the data to a csv file, if there is a Chinese column name, it will be garbled when Excel opens, and you can specify encoding="gb2312".


Guess you like

Origin blog.csdn.net/fyfugoyfa/article/details/123504668