"""
#!/usr/bin/env python
# -*- coding:utf-8 -*-
@Project : Rcsv(1)
@File : hbcsv1.py
@Author : hxy-ZW
@Time : 2023/3/1 8:54
@Description:合并一个文件下的多个csv文件并加上一定的距离
"""
import os
import pandas as pd
import time
import numpy as np
def HBcsv():
start_time = time.time()
result_table = []
# topdown的默认值是True,表示首先返回根目录树下的文件,然后遍历目录树下的子目录。
# 值设为False时,则表示先遍历目录树下的子目录,返回子目录下的文件,最后返回根目录下的文件
# os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下。
# os.walk() 方法是一个简单易用的文件、目录遍历器,可以帮助我们高效的处理文件、目录方面的事情。
# for file in os.walk("F:\\test4", topdown=True):
for file in os.walk("F:\\test csv\\2", topdown=True):
# for file in os.walk("F:\\test\\csv", topdown=True):
# for file in os.listdir(data_path):
print(file)
# path=data_path+'/'+file
i = 0
for table in file[2]:
path = file[0] + "/" + table
data = pd.read_csv(path, header=None, encoding="utf-8", engine="python", index_col=False)
# data[0]=data[0]+10000
# print(data[0])
# print(data[0]+10000)
print(max(data[0]))
# print(type(q))
# print(data[:,1])
# print(data.describe())
# print(type(data))
result_table.append(data)
# result_table=np.array(result_table)
# print(result_table[0])
if i == 0:
# q=max(result_table[0].loc[:,0])
pass
else:
# result_table[i].loc[:,0]= min(result_table[i].loc[:,0])+q+10000
# result_table[i].loc[:,0]= min(result_table[i].loc[:,0])+max(result_table[i-1].loc[:,0])+10000
# min(result_table[i].loc[:,0])=max(result_table[i-1].loc[:,0])+10000
result_table[i].loc[:, 0] = result_table[i].loc[:, 0] + max(result_table[i - 1].loc[:, 0]) + 100
# print(result_table[0].loc[:,0])
# print(result_table[i])
i = i + 1
print("当前执行表格:", table)
result_data = pd.concat(result_table)
result_data.to_csv("F:\\zhengwangwork\\test csv\\df.csv", encoding="utf_8_sig", index=False)
end_time = time.time()
use_time = start_time - end_time
use_time = round(use_time, 2)
print("用时" + str(use_time) + "秒,表格已合并")
df = pd.read_csv('F:\\zhengwangwork\\test csv\\df.csv')
print(df)
# df = df.drop(0)
# 重新设置索引 从0开始 drop=True 丢弃原来的索引列
# df.reset_index(drop=True, inplace=True)
# 设置索引 从1开始
# df.index = [index for index in range(1, len(df.index) + 1)]
# index=False 去掉索引值
df.to_csv("F:\\test csv\\3.csv", header=None, index=False)
if __name__ == '__main__':
HBcsv()
读取文件夹下的csv并合并按照一定的规律
最新推荐文章于 2024-07-12 16:16:27 发布