Development/Python

[Python] 판다스(pandas) read_csv() 함수로 파일 읽을 때 'Unnamed: 0' 제거하기

ragu29 2023. 6. 14. 21:02

pandas의 read_csv() 함수로 파일을 읽어올 때 아래와 같이 그냥 파일 경로만 넣으면 'Unnamed:0'과 같은 달갑지 않은 컬럼이 붙어서 읽혀질 때가 있다.

 

### anti-pattern ###

import pandas as pd
sample_txt = pd.read_csv('file_dir')

 

read_csv() 함수 인자 중 index_col이 따로 특정되지 않아서 발생하는 문제이다(default setting은 index_col=None). index_col을 아래와 같이 원하는 컬럼으로 지정해주면 해결된다.

 

### best-practice ###

import pandas as pd
sample_txt = pd.read_csv('file_dir', index_col=0)

 
Enjoy your modeling:)