如何使用索引 [0,1,2,...23] 将一年分为 365 天并转换为字典

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

我在整理来自 df 的数据时遇到了一些问题,因此我可以在一个列表中获取数据,其中索引每天从 0 到 23。

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()

我想要一个看起来像(实际上是字典)的列表,它每天获取从 0 到 23 的值,并将它们与索引 [0,1,2,3...23] 一起列出.相反,我现在开始关注

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

谢谢

我不太明白你的问题,但也许这会有所帮助。它写入一年中的所有时间并将索引重置为 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')