您可以使用 datetime 和 time 模块来获取时间间隔的序列。然后使用 pandas 将字典转换为数据框。这是执行此操作的代码。
import time, datetime
import pandas as pd
#set the dictionary as time and value
data = {'Time':[],'Value':[]}
#set a to 00:00 (HH:MM)
a = datetime.datetime(1,1,1,0,0,0)
#loop through the code to create 60 mins. You can increase loop if you want more values
#skip by 5 to get your 5 minute interval
for i in range (0,61,5):
# add the time and value into the dictionary
data['Time'].append(a.strftime('%H:%M'))
data['Value'].append(i*2)
#add 5 minutes to your date-time variable
a += datetime.timedelta(minutes=5)
#now that you have all the values in dictionary 'data', convert to DataFrame
df = pd.DataFrame.from_dict(data)
#print the dataframe
print (df)
#for your reference, I also printed the dictionary
print (data)
字典将如下所示:
{'Time': ['00:00', '00:05', '00:10', '00:15', '00:20', '00:25', '00:30', '00:35', '00:40', '00:45', '00:50', '00:55', '01:00'], 'Value': [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]}
数据框将如下所示:
Time Value
0 00:00 0
1 00:05 10
2 00:10 20
3 00:15 30
4 00:20 40
5 00:25 50
6 00:30 60
7 00:35 70
8 00:40 80
9 00:45 90
10 00:50 100
11 00:55 110
12 01:00 120