alex3465
alex3465

Reputation: 419

Sales data : Plot number of ordered each item over time

I have dataframe with time-series data and want to plot number of each item over time.

       date    item      ordered 
1 01-05-2020      1           1 
2 01-05-2020      1          23 
3 03-06-2020      2           4
4 03-07-2020      2           5
5 04-09-2020      3           4

df_new = df.groupby(df[['date','item']])['ordered'].sum().reset_index()
df_new.plot()

Upvotes: 1

Views: 435

Answers (1)

jezrael
jezrael

Reputation: 863166

Use DataFrame.pivot_table before ploting, also dont convert DatetimeIndex to column by reset_index before ploting:

df_new = df.pivot_table(index='date', columns='item', values='ordered', aggfunc='sum')
print (df_new)
item           1    2    3
date                      
01-05-2020  24.0  NaN  NaN
03-06-2020   NaN  4.0  NaN
03-07-2020   NaN  5.0  NaN
04-09-2020   NaN  NaN  4.0

df_new.plot()

Your solution:

df_new = df.groupby(['date','item'])['ordered'].sum().unstack()
print (df_new)
item           1    2    3
date                      
01-05-2020  24.0  NaN  NaN
03-06-2020   NaN  4.0  NaN
03-07-2020   NaN  5.0  NaN
04-09-2020   NaN  NaN  4.0

df_new.plot()

Upvotes: 2

Related Questions