How to use groupby to put values from a date-column sorted into 12 months and plot them

Hello!
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.
image
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!

Hello,

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 = [
        ('2022-05-10', 70),
        ('2022-05-15', 72),
        ('2022-05-17', 79),
        ('2022-06-21', 80),
        ('2022-06-22', 81),
    ]

def get_year_month(date):
    """Return year and month part of an ISO 8601 date."""
    return date[:7]

month_groups = groupby(input_data, lambda day_data: get_year_month(day_data[0]))
for month, month_data in month_groups:
    print(month, median(day_data[1] for day_data in month_data))

output:

2022-05 72
2022-06 80.5

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.