본문 바로가기
2/[ Python ]

How do I rename columns in a pandas DataFrame?

by Kieran_Han 2020. 11. 26.

Source: youtu.be/0uBirYFhizE

ufo = pd.read_csv('http://bit.ly/uforeports')

ufo.head()

ufo.columns

ufo.rename(columns = {'Colors Reported':'Colors_Reported', 'Shape Reported':'Shape_Reported'}, inplace=True)

ufo.columns

ufo_cols = ['city', 'colors reported', 'shape reported', 'state', 'time']

ufo.columns = ufo_cols

ufo.head()

#파일을 불러오면서 columns 이름 자체를 바꿈

ufo = pd.read_csv('http://bit.ly/uforeports', name = ufo_cols, header = ())

ufo.head()

#type 많이 안해도 되는 rename 방법

ufo.columns

#space를 _로 대체함

ufo.colums = ufo.columns.str.replace(' ', '_')

ufo.columns