728x90
csv파일들이 여러개 있는 경우
하나하나 불러오기에 너무 많은 경우에 for문을 활용해서 불러올 수 있다.
import os
import pandas as pd
# CSV 파일이 있는 폴더 경로
folder_path = '/home/myname/projectname/data'
# 폴더 내 모든 파일 목록 가져오기
file_list = os.listdir(folder_path)
# CSV 파일만 필터링 (다른 파일들도 같이 있을 때 csv만 받기 위해서 사용)
csv_files = [file for file in file_list if file.endswith('.csv')]
total = []
# 각 CSV 파일을 순회하면서 읽기
for csv_file in csv_files:
file_path = os.path.join(folder_path, csv_file)
# CSV 파일을 DataFrame으로 읽기
df = pd.read_csv(file_path)
total.append(df)
다른 방법으로는
for roots, dirs, files in os.walk("./subway"):
for file in files:
locals()[file.split(".")[0]] = pd.read_csv(f"{roots}/{file}", encoding='cp949')
728x90
'자격증 > 빅데이터분석기사' 카테고리의 다른 글
[빅분기] 제 3유형 연습 (0) | 2024.07.04 |
---|---|
[빅분기] 제 1유형 연습 (0) | 2024.07.04 |
[빅분기] 제 2유형 연습 (0) | 2024.07.04 |
(pandas) file 다루기 (0) | 2023.12.27 |