Reputation: 781
I'm trying to create a plot to show profit over time through a pandas dataframe. Here are the steps I have taken:
profit_data=agg_data.groupby(['segment','yyyy_mm_dd'])[["profit"]].sum()
profit_data
This gives me a dataframe similar to the below:
profit
segment yyyy_mm_dd
Core 2019-06-01 100
2019-06-02 100
2019-06-03 100
2019-06-04 100
2019-06-05 100
NonCore 2019-06-07 100
2019-06-08 100
2019-06-09 100
2019-06-10 100
...
...
I then try to plot this using matplotlib
:
profit_data.plot()
The above does generate a plot, however my segments are one continuous line rather than two different lines (one for each segment). What change do I need to make so each segment is plotted?
Upvotes: 0
Views: 48
Reputation: 373
Your output dataframe is only one column, so only one column gets plotted.
To solve this, you need to reshape your dataframe into two columns - one for each segment:
df_unstacked = df.unstack(level="segment")
Alternatively, you could select which indices to plot and plot twice:
df.loc["Core"].plot(label="Core")
df.loc["NonCore"].plot(label="NonCore")
Hope this helps!
Upvotes: 1