2017-09-12 9 views
1

다음에서이 메시지를 보냅니다. questionpandas 피벗 테이블의 열 머리글을 바꾸는 방법은 무엇입니까?

이렇게 피벗 한 후 데이터 프레임을 얻었습니다.

 
         AVG GrossProfit  AVG PMV  Loss%   Sales  
ParentAuction  Copart IAA  Copart IAA Copart IAA Copart IAA 
Make                   
Acura    112.99 NaN   -15.53 NaN 36.46 NaN  96.0 NaN 

어떻게이 열 형식으로 열 수준을 변경합니까? MultiIndex를 정렬 sort_index

 
ParentAuction     Copart         IAA   
       AVG GrossProfit AVG PMV Loss% Sales AVG GrossProfit AVG PMV Loss% Sales 
Make                   
Acura     112.99 -15.53 36.46 96.0    NaN  NaN  NaN  NaN 

답변

2

사용 swaplevel :

df = df.swaplevel(0,1, axis=1).sort_index(axis=1) 
print (df) 
ParentAuction   Copart         IAA   \ 
       AVG GrossProfit AVG PMV Loss% Sales AVG GrossProfit AVG PMV 
Make                   
Acura     112.99 -15.53 36.46 96.0    NaN  NaN 

ParentAuction    
       Loss% Sales 
Make       
Acura   NaN NaN