我使用截斷來做到這一點:
import random
import pandas as pd
import datetime as dt
ts = [dt.datetime.now() + dt.timedelta(minutes = random.randint(1000)) for _ in range(1000)]
df = pd.DataFrame(ts, columns = ['ts'])
def truncate(t):
return dt.datetime(month = t.month, day = t.day, year = t.year, hour = t.hour, minute = (55/10) * 10)
df.ts.map(truncate).value_counts()
會給你每10分鐘
2016-02-20 00:50:00 79
2016-02-19 23:50:00 75
2016-02-20 08:50:00 72
2016-02-19 21:50:00 70
...
編輯計數:
由A. Leistra的方法要好得多,我學到了一些東西爲好。它從上面的設置工作:
df.set_index('ts', inplace = True)
df['count'] = 1
df.resample('10T', how = sum).head()
count | ts
--- | ---
2016-02-19 21:00:00 5
2016-02-19 21:10:00 11
2016-02-19 21:20:00 17
2016-02-19 21:30:00 13
2016-02-19 21:40:00 11
那麼,時代的格式?你試過什麼了?你必須給我們更多的工作。 – n1c9
請看看這裏學習如何問一個好問題:http://stackoverflow.com/help/mcve –
只是增加了更多的細節.. – SN65