我有一個如下所示的資料集,它的時間列基于毫秒。
pid_col ,timestamp_col ,value_col
31,2019-03-29 07:14:56.999999756,0.0
31,2019-03-29 07:14:57.250000,0.614595
31,2019-03-29 07:14:57.500000,0.678615
31,2019-03-29 07:14:57.750000,0.687578
31,2019-03-29 07:14:58.000000244,0.559804
31,2019-03-29 07:14:58.250000,0.522672
31,2019-03-29 07:14:58.499999512,0.51627
31,2019-03-29 07:14:58.750000,0.51627
31,2019-03-29 07:14:59.000000244,0.517551
31,2019-03-29 07:14:59.250000,0.51627
31,2019-03-29 07:14:59.500000244,0.509868
31,2019-03-29 07:14:59.750000488,0.513709
31,2019-03-29 07:15:00,0.513709
31,2019-03-29 07:15:00.249999512,0.518831
31,2019-03-29 07:15:00.500000,0.531635
我怎么能每 5 秒計算一次值的平均值?對于這個資料集,我應該每五秒精確計算一次值的平均值......我的意思是前 5 秒的值應該在 7:14:56 到 7 之間計算: 15:01 等每 5 秒。這是我的代碼:
col_list = ["timestamp", "pid","value"]
df = read_csv("data.csv", usecols=col_list)
df['timestamp'] = to_datetime(df['timestamp'], unit='ms')
df = df.groupby(['pid', Grouper(freq='5S', key='timestamp')], as_index=False) \
.agg({'timestamp': 'first', 'value': 'mean'})
感謝您的幫助
uj5u.com熱心網友回復:
有一個很好的庫datetime,可以在日期之間進行操作。舉個例子:
from datetime import datetime, timedelta
# datetime(year, month, day, hour, minute, second, microsecond)
time0 = datetime(2019, 3, 29, 7, 14, 57, 500000)
print(time0)
fiveseconds = timedelta(seconds=5)
print(fiveseconds)
time1 = time0 fiveseconds
print(time1)
給出輸出
2019-03-29 07:14:57.500000
0:00:05
2019-03-29 07:15:02.500000
您可以在兩者之間進行比較:
from datetime import datetime, timedelta
time0 = datetime(2019, 3, 29, 7, 14, 57, 500000)
fourseconds = timedelta(seconds=4)
fiveseconds = timedelta(seconds=5)
sixseconds = timedelta(seconds=6)
time1 = time0 fiveseconds
print(time1 < (time0 fourseconds)) # False
print(time1 < (time0 sixseconds)) # True
所以,對于你的問題:
from datetime import datetime, timedelta
from numpy import floor
def convert(timestr):
"""
It receives a string, like ""2019-03-29 07:14:57.250000"
And returns a datetime instance
"""
date = timestr.split(" ")
year, month, day = date[0].split("-")
year = int(year)
month = int(month)
day = int(day)
hour, minute, second = date[1].split(":")
hour = int(hour)
minute = int(minute)
intsecond = int(second.split(".")[0])
if "." in second:
microsecond = int(floor(1e 6 * float("0." second.split(".")[1])))
else:
microsecond = 0
return datetime(year, month, day, hour, minute, intsecond, microsecond)
listtimes = ["2019-03-29 07:14:56.999999756",
"2019-03-29 07:14:57.250000",
"2019-03-29 07:14:57.500000",
"2019-03-29 07:14:57.750000",
"2019-03-29 07:14:58.000000244",
"2019-03-29 07:14:58.250000",
"2019-03-29 07:14:58.499999512",
"2019-03-29 07:14:58.750000",
"2019-03-29 07:14:59.000000244",
"2019-03-29 07:14:59.250000",
"2019-03-29 07:14:59.500000244",
"2019-03-29 07:14:59.750000488",
"2019-03-29 07:15:00",
"2019-03-29 07:15:00.249999512",
"2019-03-29 07:15:00.500000"]
listvalues = [0.0,
0.614595,
0.678615,
0.687578,
0.559804,
0.522672,
0.51627,
0.51627,
0.517551,
0.51627,
0.509868,
0.513709,
0.513709,
0.518831,
0.531635]
dt = timedelta(seconds=5)
averagevalues = []
time0 = convert(listtimes[0])
time1 = time0 dt
counter = 0
mysum = 0
for i, v in enumerate(listvalues):
if convert(listtimes[i]) >= time1:
averagevalues.append(mysum / counter)
counter = 0
mysum = 0
time1 = dt
counter = 1
mysum = v
if counter != 0:
averagevalues.append(mysum / counter)
print(averagevalues)
給出結果
[0.5144918]
因此,如果您有更大的值串列和更大的時間,該串列averagevalues將對每個5 seconds. 在這個例子中,所有的時間都在2019-03-29 07:14:56and之間"2019-03-29 07:15:01,所以我們只有一個值averagevalues
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/344677.html
