지난 글에 이어 오늘은 데이터프레임을 생성하는 법을 알아보겠다.
import pandas as pd
student_card = pd.DataFrame({'ID':[20190103, 20190222, 20190531],
'name':['Kim', 'Lee', 'Jeong'],
'class':['H', 'W', 'S']})
student_card
1.행 인덱스 지정
student_card = pd.DataFrame({'ID':[20190103, 20190222, 20190531],
'name':['Kim', 'Lee', 'Jeong'],
'class':['H', 'W', 'S']},
index = ['a', 'b', 'c']) #행 index 지정
student_card
열 인덱스 전체 변경
student_card.columns
student_card.columns = ['아이디','이름,'성']
열 인덱스 하나만 변경
rename
메소드를 쓴다.데이터명.rename({'기존인덱스명':'바꿀 인덱스 명'},axis = 0(행인덱스) or 1(열인덱스), inplace = True)
앞에서 말했듯이 DataFrame에는 열인덱스와 행 인덱스가 존재한다.#열인덱스(이름)을 NAME으로 변경
student_card.rename({'이름':'NAME'},axis = 1,inplace = True)
student_card
#행 인덱스 변경
student_card.rename({'b':'2'},axis = 0,inplace = True)
student_card = student_card.columns.str.lower()
student_card = student_card.columns.str.replace('s','S')