1

My pandas dataframe is in two time dimensions, the date and the period the data fall in. Making a pivot table out of it further explains the structure. But how to plot a line chart along the two time dimensions

raw_data = {'period': ['2016-05-26', '2016-05-26', '2016-05-26', '2016-05-27', '2016-05-27'],
    'date': ['2016-05-26', '2016-05-27', '2016-05-28', '2016-05-26', '2016-05-27'],
    'mid_score': [25, 94, 57, 62, 70],
    'post_score': [5, 43, 23, 23, 51]}
df = pd.DataFrame(raw_data, columns = ['period', 'date', 'mid_score', 'post_score'])
df

enter image description here

df.groupby(['period','date']).sum()

enter image description here

What is the best way to plot this kind of grouped data, is it possible to have it like this kind of line chart?

enter image description here

DevEx
  • 4,337
  • 13
  • 46
  • 68
  • 1
    I think the easiest is use `df.groupby(['period','date']).sum().plot()`. But there is problem with values in axis x. You get tuples, because after groupby you get multiindex. – jezrael May 26 '16 at 06:16
  • you may want to check this [answer](http://stackoverflow.com/a/20546657/5741205) – MaxU - stand with Ukraine May 26 '16 at 08:47
  • Building on @jezrael's comment, you can modify the dataframe's index to get the tick labels you want. For instance: `df.groupby(['period','date']).sum().reset_index(level=0, drop=True).plot()`. However, I'm not sure why you have repeating dates. – IanS May 26 '16 at 09:15
  • @jezrael x-axis tuples would be difficult to visualize for many values of x-axis – DevEx May 26 '16 at 10:54
  • Exactly, and it is not nice. – jezrael May 26 '16 at 11:04
  • But @DevEx you have only one date on the x-axis in the example you showed. Which is it, `period` or `date`? – IanS May 26 '16 at 11:17
  • @IanS the example shows how x-axes can be grouped, year is first level, month is the second level. In my own case, period is the first level, date is the second level, and the aggregation term is sum – DevEx May 26 '16 at 11:48

0 Answers0