[018] DataFrame 구조 조작
1. columns, Index 상호 변경
- DataFrame.set_index(컬럼명) : 지정한 컬럼을 index로 설정
- DataFrame.set_index([컬럼명1, 컬럼명2, ...]) : 컬럼 목록을 index(Multi index)로 설정
columns에서 index 쪽으로 이동됨, 기존 index는 제거됨
- DataFrame.reset_index() : 모든 index가 columns로 이동됨
index는 RangeIndex로 대체됨
2. Series -> DataFrame 형태로 변경
3. DataFrame 행/열 전환
- DataFrame.T : index, columns의 위치가 바뀜
4. DataFrame 컬럼 추가
- DataFrame.insert(위치, 컬럼, 값) : inplace 동작됨(새로운 return 없음)
- inplace 동작 : 바로 적용된다는 것
5. DataFrame 여러 행 추가
- DataFrame.append(DataFrame) : 새로운 DataFrame 반환
- 2개를 1개로 합치는 작업 가능
6. 여러 개의 DataFrame을 합쳐 하나의 DataFrame 생성
- pd.concat([df1, df2, ...], axis=0) : index 방향으로 합치기
- pd.concat([df1, df2, ...], axis=1) : columns 방향으로 합치기
- DataFrame.append()와는 달리 여러 개의 DataFrame 목록을 주어 여러 개를 한 번에 합칠 수 있음
<출처>
인프런 - [EduAtoZ] 빅데이터분석기사 실기 대비 Part2. 다양한 데이터 프레임 구조 조작