파이썬(Python)&판다스(Pandas)&Polars

[Pandas] 여러 파일의 행 수, 열 수를 하나의 데이터 프레임으로 만들기(listdir(), shape())

송채채 2023. 2. 2. 16:16
file_list = os.listdir()
file_list = [file for file in file_list if file.endswith(".txt")] # 확장자
print(file_list)

df = pd.DataFrame()
df["파일명"] = file_list
df["행 수"] = None
df["열 수"] = None

for i in range(len(df)):
    file = df['파일명'][i]
    tmp = pd.read_csv(file, sep="|", low_memory=False, encoding="cp949") # 인코딩 선택
    df['행 수'][i] = tmp.shape[0]
    df['열 수'][i] = tmp.shape[1]

 

반응형