I need help for using groupby to put values from a date-column sorted into 12 months and plot them with the median of price in those months.
This is how the dataframe looks like. Every ID has its price and I want to put the dates first into 12 groups of months. And then calculate the median of them and plot the median value into a bar chart.
Any help is appreciated!
please next time give the example data as a Python data structure. Also it is good if you show the code you have already written and point to the particular problems you encountered.
I created some data similar to yours. The code below does the following:
- Group the data by month. Notice the grouping key provided as a lambda function.
- Iterate the groups.
- For each month group print the month and the median of the price.
from itertools import groupby
from statistics import median
# list of day_data tuples (date, price)
input_data = [
"""Return year and month part of an ISO 8601 date."""
month_groups = groupby(input_data, lambda day_data: get_year_month(day_data))
for month, month_data in month_groups:
print(month, median(day_data for day_data in month_data))
Now you just need to understand the program and modify it for your input data format and the desired output format and add the plotting part.