2017-03-14 1 views
1

I 다음 dataframe 있습니다하기 matplotlib에서 두 개의 열별로 그룹화 dataframe 음모와 팬더 방법

     total_gross_profit 
    first_day_week var 
     Feb-06   1 45293.09 
        2 61949.54 
     Feb-13   1 44634.72 
        2 34584.15 
     Feb-20   1 43796.89 
        2 37308.57 
     Feb-27   1 44136.21 
        2 38237.67 
     Jan-16   1 74695.91 
        2 75702.02 
     Jan-23   1 86101.05 
        2 69518.39 
     Jan-30   1 65913.56 
        2 74823.94 
     Mar-06   1 34256.47 
        2 31953.00 

first_day_weekvar 열별로 그룹화 내가 x 축에 first_day_week이 막대 플롯을해야하고 다른 색상 var의 각 값에 대한 first_day_week 두 개의 막대 그래프의 각 항목과 유사한 뭔가 (barplot를 다음의 데이터는 totaly 가짜) :

enter image description here

답변

4

당신은 unstack으로 바꿀 필요가 다음 DataFrame.plot.bar에 의해 플롯 :

print (df.unstack()['total_gross_profit']) 
var     1   2 
first_day_week      
Feb-06   45293.09 61949.54 
Feb-13   44634.72 34584.15 
Feb-20   43796.89 37308.57 
Feb-27   44136.21 38237.67 
Jan-16   74695.91 75702.02 
Jan-23   86101.05 69518.39 
Jan-30   65913.56 74823.94 
Mar-06   34256.47 31953.00 


df.unstack()['total_gross_profit'].plot.bar(rot=0) 

graph

종류, 날짜로 변환 할 필요 인덱스를 정렬

다음 reindex :

df1 = df.unstack()['total_gross_profit'] 
idx = pd.to_datetime(df1.index, format='%b-%d').sort_values().strftime('%b-%d') 
print (idx) 
['Jan-16' 'Jan-23' 'Jan-30' 'Feb-06' 'Feb-13' 'Feb-20' 'Feb-27' 'Mar-06'] 

df1.reindex(idx).plot.bar(rot=90) 

graph1

+0

완벽한, 계획을 세우기 전에 'first_day_week'에 시간 기준으로 정렬 할 수있는 방법이 있습니까? – sanaz

+0

sort_index()가 도움이 될까요? – sanaz

+0

죄송합니다. 잠시만 기다려주십시오. – jezrael