Ask Your Question

Revision history [back]

Assuming you have a dataframe named df containing the columns year, month, and price, you can merge the year and month columns to create a new column date and then group by this column to calculate the average price for each date as follows:

import pandas as pd

# Merge year and month columns to create date column
df['date'] = pd.to_datetime(df['year'].astype(str) + '-' + df['month'].astype(str), format='%Y-%m')

# Group by date and calculate average price
average_prices = df.groupby('date')['price'].mean()

This will create a new Series object average_prices containing the average price for each date.