Skip to content
Advertisement

Add averages to existing plot with pandas.DataFrame

I have a pandas data-frame of the form

JavaScript

and I want to plot the last 7 days together with the average over the weekdays.

I can create / plot the average by using

JavaScript
JavaScript

avg_week

and I can create / plot the last 7 days by using

JavaScript

7_days

but I fail to combine them to a single plot since the average uses weekday and minutes after midnight (actually idx of avg_week) instead of a datetime obj.

What else I worked out so far is setting the weekdays manually

JavaScript

avg_labels

but the problem of course persists.

Any ideas? Thanks a lot!

Advertisement

Answer

Consider assigning the weekday average as a separate new column with groupby.transform. Then plot both columns on same x-axis in a time series plot.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement