【问题标题】:How do I find the window of time between the earliest time stamp and the latest time stamp of one column and group it by another column?如何找到一列的最早时间戳和最新时间​​戳之间的时间窗口,并将其按另一列分组?
【发布时间】: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)



【问题讨论】:

标签: python pandas datetime time window


【解决方案1】:

您可以使用np.ptp 这样做

import pandas as pd
import numpy as np

data = {'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'],
'Title': ['The order is green','The order is green','The order is green','The order is green','The membership is cancelled','The membership is cancelled','The membership is cancelled']}

df = pd.DataFrame(data)

df['Test_Ordered_at'] = pd.to_datetime(df['Test_Ordered_at'])
print(df.groupby('Title')['Test_Ordered_at'].agg(np.ptp))

Title
 The membership is cancelled   02:00:00
 The order is green            03:50:00

【讨论】:

  • 我试过了,得到了这个错误异常:必须产生聚合值
  • 我已将答案调整为将Test_Ordered_at 转换为日期时间,请重试。
  • 我试了新的出现了这个错误 ValueError: Function does not reduce 在处理上述异常的过程中,又发生了一个异常:
  • 如果您的数据框 df 与您描述的一样,这应该可以工作。我已经使用从您的示例中获取的数据集更新了我的答案。请检查您的数据框是否有任何差异。
【解决方案2】:
  1. 一旦是日期时间,简单聚合和计算
data = '''Test_Ordered_at,        Title
2020-04-07 15:06:00, The order is green
2020-04-07 18:56:00, The order is green
2020-04-07 15:07:00, The order is green
2020-04-07 18:55:00, The order is green 
2020-03-07 16:55:00, The membership is cancelled
2020-03-07 17:55:00, The membership is cancelled 
2020-03-07 18:55:00, The membership is cancelled'''
da = [[i.strip() for i in l.split(",")] for l in data.split("\n")]
da
df = pd.DataFrame(da[1:], columns=da[0])
df.Test_Ordered_at = pd.to_datetime(df.Test_Ordered_at)
df2 = df.groupby("Title")["Test_Ordered_at"].agg(["min","max"]).reset_index()
df2["Test_Ordered_at"] = df2["max"] - df2["min"]
df2.drop(columns=["min","max"])

输出

    Title   Test_Ordered_at
0   The membership is cancelled 02:00:00
1   The order is green  03:50:00

【讨论】:

    猜你喜欢
    • 2020-09-28
    • 2021-12-24
    • 2014-02-07
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多