Pandas: Group by calendar-week, then plot grouped barplots for the real datetime

前端 未结 3 1654
情书的邮戳
情书的邮戳 2021-01-30 15:05

EDIT

I found a quite nice solution and posted it below as an answer. The result will look like this:

\"ent

3条回答
  •  不思量自难忘°
    2021-01-30 15:46

    Okay I answer the question myself as I finally figured it out. The key is to not group by calendar week (as you would loose information about the year) but rather group by a string containing calendar week and year.

    Then change the layout (reshaping) as mentioned in the question already by using pivot. The date will be the index. Use reset_index() to make the current date-index a column and instead get a integer-range as an index (which is then in the correct order to be plotted (lowest-year/calendar week is index 0 and highest year/calendar week is the highest integer).

    Select the date-column as a new variable ticks as a list and delete that column from the DataFrame. Now plot the bars and simply set the labels of the xticks to ticks. Completey solution is quite easy and here:

    codes = list('ABCDEFGH'); 
    dates = pd.Series(pd.date_range('2013-11-01', '2014-01-31')); 
    dates = dates.append(dates)
    dates.sort()
    df = pd.DataFrame({'amount': np.random.randint(1, 10, dates.size), 'col1': np.random.choice(codes, dates.size), 'col2': np.random.choice(codes, dates.size), 'date': dates})
    
    kw = lambda x: x.isocalendar()[1]; 
    kw_year = lambda x: str(x.year) + ' - ' + str(x.isocalendar()[1])
    grouped = df.groupby([df['date'].map(kw_year), 'col1'], sort=False, as_index=False).agg({'amount': 'sum'})
    A = grouped.pivot(index='date', columns='col1', values='amount').fillna(0).reset_index()
    
    ticks = A.date.values.tolist()
    del A['date']
    ax = A.plot(kind='bar')
    ax.set_xticklabels(ticks)
    

    RESULT:

    enter image description here

提交回复
热议问题