The data_list
and the monthly_values
array are in correlation with each other, so the data point '2019-09-01 00:00:00'= 15 , 2019-10-01 00:00:00'= 39.6... etc
. The year_changes
function below shows the indexes where a new year has occurred. I am trying to code a function that shows me the average values of all the monthly values within the given year. So since there are 4 months present in 2019 2019-09-01 00:00:00 - 2020-01-01 00:00:00
it takes in the sum of the numbers 15., 39.6, 0.2, 34.3
and divides by the number of months in 2019 which is 4 resulting in the Expected Output
of 22.28
. How would I be able to code such a thing?
import datetime
import numpy as np
import pandas as pd
from pandas import DataFrame
date_list = ['2019-09-01 00:00:00', '2019-10-01 00:00:00', '2019-11-01 00:00:00',
'2019-12-01 00:00:00', '2020-01-01 00:00:00', '2020-02-01 00:00:00',
'2020-03-01 00:00:00', '2020-04-01 00:00:00', '2020-05-01 00:00:00',
'2020-06-01 00:00:00', '2020-07-01 00:00:00', '2020-08-01 00:00:00',
'2020-09-01 00:00:00','2020-10-01 00:00:00', '2020-11-01 00:00:00',
'2020-12-01 00:00:00','2021-01-01 00:00:00','2021-02-01 00:00:00', '2021-03-01 00:00:00',
'2021-04-01 00:00:00','2021-05-01 00:00:00', '2021-06-01 00:00:00',
'2021-07-01 00:00:00']
monthly_values = np.array([ 15., 39.6, 0.2, 34.3, 19.6, 26.8, 15.7, 26., 12.6, 15.5, 18.6, 2.3, 6.5,
2.5, 12.2, 11.6, 93.9, 25.5, 26.5, -16.5, -1.4, -1.8, 5.])
data = DataFrame (date_list,columns=['Data'])
datetime = pd.to_datetime(data['Data'])
year_changes = data.loc[np.where(datetime.dt.year.diff().gt(0))].index.tolist()
Expected Output Yearly Values:
2019 Average: 22.28
2020 Average: 14.16
2021 Avreage: 21.03