【发布时间】:2020-10-29 17:30:08
【问题描述】:
我正在处理一个大型数据文件,我想找到从最早时间戳到最新时间戳的一列的时间窗口,并按另一列中标题的字符串对其进行分组。我在 python 和 juypter 工作。
Test_Ordered_at 标题
2020-04-07 15:06:00 订单是绿色的
2020-04-07 18:56:00 订单是绿色的
2020-04-07 15:07:00 订单是绿色的
2020-04-07 18:55:00 订单为绿色
2020-03-07 16:55:00 会员资格被取消
2020-03-07 17:55:00 会员资格被取消
2020-03-07 18:55:00 会员资格被取消
结果应该是这样的:
Test_Ordered_at 标题
03:50:00 订单是绿色的
02:00:00 会员资格被取消
这是我下面的代码
import pandas as pd
from datetime import datetime
from dateutil import parser
notmiss = df
notmiss['Test_Ordered_At'] = notmiss['Test_Ordered_At'].astype('datetime64[ns]')
print(notmiss.head())
# Add a new column instance, this adds a value to each instance
notmiss['instance'] = 1
# set index to time, this makes df a time series df and then you can apply pandas time series functions.
tf = notmiss.set_index(notmiss['Test_Ordered_At'], drop=True, inplace=True)
【问题讨论】:
-
试试这个,
df.groupby('Title')['Test_Ordered_at'].agg(['min', 'max']).diff(axis=1) -
^ 成功了!有没有办法平均所有的最大时间戳?我只是一个得到一个数字,这就是所有最大值的平均值。
标签: python pandas datetime time window