코딩/Python
[KT AICE] Pandas 활용 - Dataframe 변형하기
JuBro
2024. 8. 18. 23:27
728x90
Groupby
범주형 컬럼을 기준으로 같은 값을 묶어 통계 또는 집계결과를 얻어 사용하는 것
#Example
dataframe.groupby('성별').mean()
groupby는 데이터 분할(split) > 적용(applying) > 데이터 병합(combine) 세 단계를 거쳐서 진행된다.
Pivot_table
DataFrame 형태를 변경하는 것
#Example
pd.pivot_table(data=sample, index='고객ID', columns='상품코드', values='구매금액',aggfunc='mean')
stack, unstack
stack : 컬럼 ➡️ 인덱스로 변환
unstack : 인덱스 ➡️ 컬럼으로 변환
reference
pandas.DataFrame.stack — pandas 2.2.2 documentation
Level(s) to stack from the column axis onto the index axis, defined as one index or label, or a list of indices or labels.
pandas.pydata.org
728x90