Home > front end >  How to sort a year into 365 days with index [0,1,2,...23] and conver to dict
How to sort a year into 365 days with index [0,1,2,...23] and conver to dict

Time:03-07

I'm having some trouble arranging data from df, so I can get data in a list where the indexes goes from 0 to 23 for each day.

timeStart = pd.to_datetime(dt.datetime(2021, 1, 1, 0, 0, 0))
timeEnd = pd.to_datetime(dt.datetime(2021, 12, 31, 23, 0, 0))
df = df.loc[(df['HourUTC'] >= timeStart) & (df['HourUTC'] <= timeEnd)]

dataList = df['SpotPriceDKK']/1000
#Rewriting the dataframe from a list to dictionary
dataDict = dataList.to_dict()

I want a list that look like (actually a dictionary), it takes the values from 0 to 23 each day and list them with the indexes [0,1,2,3...23] for each day. Instead I right now get following

output:
dataList
Out[140]: 
61314    0.18119
61307    0.17844
61300    0.17650
61293    0.17657
61286    0.17903
  
29       0.25253
22       0.24599
15       0.24049
8        0.22130
1        0.34654

Thank you

CodePudding user response:

I don´t exactly understand your problem but maybe this could help. It writes all hours of a year and reset the index at 00:00.

df = pd.DataFrame()

for i in pd.date_range('2022-1-1 00:00:00', periods=356, freq='D'):
    date_day = pd.Series(pd.date_range(f'{i} 00:00:00', periods=24, freq='H'))
    df = pd.concat([df, date_day], ignore_index = False )



df.to_csv('test.csv')
  • Related