pandas dataframe 의 column 이름 변경하기 여러 방법

Q04

Pandas DataFrame 에 있는 column 이름 변경

In [1]:
import pandas as pd
In [2]:
ufo = pd.read_csv('http://bit.ly/uforeports')
In [4]:
ufo.head()
Out[4]:
City Colors Reported Shape Reported State Time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00
In [5]:
ufo.columns
Out[5]:
Index(['City', 'Colors Reported', 'Shape Reported', 'State', 'Time'], dtype='object')
In [6]:
ufo.rename(columns={'Colors Reported':'Colors_Reported', 'Shape Reported':'Shape_Reported'}, inplace=True)
In [7]:
ufo.columns
Out[7]:
Index(['City', 'Colors_Reported', 'Shape_Reported', 'State', 'Time'], dtype='object')
In [8]:
ufo_cols = ['city','colors reported', 'shape reported', 'state','time']
In [9]:
ufo.columns = ufo_cols
In [11]:
ufo.head()
Out[11]:
city colors reported shape reported state time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00
In [14]:
ufo = pd.read_csv('http://bit.ly/uforeports', names=ufo_cols, header=0)
In [15]:
ufo.head()
Out[15]:
city colors reported shape reported state time
0 Ithaca NaN TRIANGLE NY 6/1/1930 22:00
1 Willingboro NaN OTHER NJ 6/30/1930 20:00
2 Holyoke NaN OVAL CO 2/15/1931 14:00
3 Abilene NaN DISK KS 6/1/1931 13:00
4 New York Worlds Fair NaN LIGHT NY 4/18/1933 19:00
In [16]:
ufo.columns = ufo.columns.str.replace(' ','_')

댓글

Designed by JB FACTORY