본문 바로가기
Python

[python][pandas] 판다스 그룹 집계하기pandas.DataFrame.groupby.aggregate

by Chandler.j 2020. 12. 30.
반응형

fig1. title


#0 소스데이터

fig2. source of dataframe

 

#1 col1기준으로 그룹핑한 데이터의 집계

df.groupby('col1').agg(['size', 'mean', 'std', 'min', 'max'])

y_train_gb = y_train_pd.groupby('round_eGFR').agg(['size', 'mean', 'std', 'min', 'max'])
y_train_gb

fig3. output of #1

#2 column별 원하는 집계 사용가능

df.groupby('col1').agg({'col1' : ['size'], 'col2' : ['size', 'mean'], 'col3' : ['std'], 'col4' : ['min', 'max']})

y_train_gb = y_train_pd.groupby('round_eGFR').agg({'true_eGFR' : ['size'], 
							'pred_eGFR':['size','mean'], 
							'pred-true':['std'], 
							'ABS pred-true':['min', 'max']})
y_train_gb

fig4. output of #2

#3 column 이름 변경해주기

y_train_gb = y_train_pd.groupby('round_eGFR').agg(['mean', 'std'])
y_train_gb.columns = ["_".join(x) for x in y_train_gb.columns.ravel()]
y_train_gb

 

fig5. output of #3

 


TOP

Designed by 티스토리