본문 바로가기
데이터 분석/시각화

pandas로 논문 형식의 table 작성하기

by 직_장인 2023. 1. 2.

1. Data 형태

데이터

- COLUMN은 0, 1로 구분되어 있다.

- column은 one, two, three로 구분되어 있다.

- 변수는 4가지가 있다.(Variable 1~4)

- COLUMN, column을 이용하여 멀티컬럼(Multicolumn)을 만들고, 변수의 평균, 편차를 보고자 한다.

 

2. 평균 편차 계산

variables = ['var1', 'var2', 'var3', 'var4']
 
Table_mean = (Table.groupby(['COLUMN', 'column'])[variables].mean().T).round(2)
Table_std = (Table.groupby(['COLUMN', 'column'])[variables].std().T).round(2)
 
col_0 = Table_mean.iloc[:, 0].map('{:.2f} ± '.format) + Table_std.iloc[:, 0].map('{:.2f}'.format)
col_1 = Table_mean.iloc[:, 1].map('{:.2f} ± '.format) + Table_std.iloc[:, 1].map('{:.2f}'.format)
col_2 = Table_mean.iloc[:, 2].map('{:.2f} ± '.format) + Table_std.iloc[:, 2].map('{:.2f}'.format)
col_3 = Table_mean.iloc[:, 3].map('{:.2f} ± '.format) + Table_std.iloc[:, 3].map('{:.2f}'.format)
col_4 = Table_mean.iloc[:, 4].map('{:.2f} ± '.format) + Table_std.iloc[:, 4].map('{:.2f}'.format)
col_5 = Table_mean.iloc[:, 5].map('{:.2f} ± '.format) + Table_std.iloc[:, 5].map('{:.2f}'.format)
Table_mean_std = pd.concat([col_0,col_1,col_2,col_3,col_4,col_5], axis=1)

Table_mean_std.columns = [['COLUMN 0', 'COLUMN 0', 'COLUMN 0', 'COLUMN 1', 'COLUMN 1', 'COLUMN 1'],
                 	  ['column one', 'column two', 'column three', 'column one', 'column two', 'column three']]
 
Table_mean_std

 

- grouby를 이용하여 COLUMN, column으로 묶어서 평균과 편차를 계산하고 데이터프레임 형태로 만든다.

- 평균, 편차의 데이터프레임 각각의 컬럼을 하나씩 가져온다.

- 소수점 2째자리까지 표시하는 str형태로 formating 한 뒤, 평균값과 편차값을 함께 표시한다.

  (1.0의 경우는 1.00으로 표시하기 위해 위와같은 형태로 하였다.)

- 평균값, 편차값을 함께 표시한 값을 concat하여 하나의 데이터프레임으로 만든다.

- .column을 이용하여 Multicolumn으로 표시한다.

 

평균 편차 결과

 

3. 각 그룹간 mann-whitney u test, p-value 표시

col_0_one = Table[(Table.COLUMN == 0) & (Table.column == 'one')] # & == and
col_0_two = Table[(Table.COLUMN == 0) & (Table.column == 'two')]
col_0_thr = Table[(Table.COLUMN == 0) & (Table.column == 'three')]
col_1_one = Table[(Table.COLUMN == 1) & (Table.column == 'one')]
col_1_two = Table[(Table.COLUMN == 1) & (Table.column == 'two')]
col_1_thr = Table[(Table.COLUMN == 1) & (Table.column == 'three')]
 
p_value_lists = []
for rows in variables:
    p_value_list = [stats.mannwhitneyu(col_0_one[rows], col_1_one[rows])[1],
                    stats.mannwhitneyu(col_0_two[rows], col_1_two[rows])[1],
                    stats.mannwhitneyu(col_0_thr[rows], col_1_thr[rows])[1]]
    p_value_lists.append(p_value_list)
   
Table_pvalue = (pd.DataFrame(p_value_lists, 
			     index=variables, 
          	             columns=[['P Value', 'P Value', 'P Value'], 
            	  		      ['column one', 'column two', 'column three']])).round(2)
Table_pvalue

 

- 각 그룹을 새로운 데이터프레임 형태로 만든다.

- 변수 갯수만큼 for문을 돌면서 mann-whitney u test를 진행하고 p-value([1]) 값을 가져와 list에 하나씩 추가(append) 한다.

  (데이터가 정규분포를 따르지 않기 때문에, mann-whitney u test를 했다. 이 부분은 필요에따라 수정하면 된다.)

- list를 데이터프레임형태로 만들고, index와 column을 재지정한다.

 

p value결과

 

4. 데이터 결합

pd.concat([Table_mean_std, Table_pvalue], axis=1)

 

- 평균, 편차 데이터프레임과 p-value 데이터프레임을 결합한다.

 

평균, 편차, p-value 결과

 

'데이터 분석 > 시각화' 카테고리의 다른 글

matplotlib으로 마커 커스텀, animation 만들기  (0) 2023.01.04

댓글