Home > Mobile >  Plotting row based data
Plotting row based data

Time:11-18

I have some data that looks something like this:

df =

id      period    15-nov    16-nov    17-nov    18-nov
------------------------------------------------------
vodka   night     1         2         3         1
vodka   day       0         1         2         0
vodka   evening   1         2         2         0
rum     night     1         2         3         3
rum     day       1         2         2         0
rum     evening   1         3         2         1

What I would like to do is to plot the values of the header dates, but grouped by id and period. So essentially this would become 6 line plots, with the x-axis given as the dates.

However, maybe I'm just tired, but this data set is weirdly put together imo, so my usual groupby stuff doesn't work because only some of the headers are the x-axis values. I was thinking about transforming the date columns into one single column, and then adding the date headers as another column giving me 4 total columns - but more rows, and then use the groupby function. But I can't think of any clever way to do this, accept maybe doing it kind of manually.

So is that the way for me to go, or am I missing an easy transformation trick ?

CodePudding user response:

You could unpivot your dataframe using enter image description here

CodePudding user response:

import seaborn as sns
sns.set()
df.set_index(['id','period']).T.plot(kind='line')

enter image description here

  • Related