forked from replayce/back_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_csv.py
More file actions
32 lines (25 loc) · 1.18 KB
/
merge_csv.py
File metadata and controls
32 lines (25 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import os
import pandas as pd
# 합치고 싶은 CSV 파일들이 있는 폴더 경로를 설정하세요
folder_path = "C:/Users/User/Desktop/busan/해운대/excel"
# 폴더 내의 모든 Excel 파일 목록 가져오기
excel_files = [file for file in os.listdir(folder_path) if file.endswith('.xlsx')]
# 병합할 데이터프레임 초기화
merged_data = pd.DataFrame()
# 모든 Excel 파일을 읽어서 병합
for file in excel_files:
file_path = os.path.join(folder_path, file)
try:
# Excel 파일 읽기
df = pd.read_excel(file_path)
merged_data = pd.concat([merged_data, df], ignore_index=True)
except Exception as e:
print(f"파일 {file} 처리 중 오류: {e}")
# 날짜와 시간 순으로 정렬
if '관측일자' in merged_data.columns:
merged_data['관측일자'] = pd.to_datetime(merged_data['관측일자'], errors='coerce')
merged_data = merged_data.sort_values(by='관측일자').reset_index(drop=True)
# 병합된 데이터 저장
output_path = os.path.join(folder_path, "해운대통합.csv")
merged_data.to_csv(output_path, index=False, encoding='utf-8-sig')
print(f"병합된 데이터가 저장되었습니다: {output_path}")