从pandas dataframe中的每个ID中的第一个时间戳中的第一个时间戳减去最后时间戳
I have a dataframe (df) with the following structure:
retweet_datetime | tweet_id | tweet_datetime |
---|---|---|
2020-04-24 03:33:15 | 85053699 | 2020-04-24 02:28:22 |
2020-04-24 02:43:35 | 85053699 | 2020-04 -24 02:28:22 |
2020-04-18 04:24:03 | 86095361 | 2020-04-18 00:06:01 |
2020-04-18 00:19:19:08 | 86095361 | 2020-04-18 00:06:01 |
2020-01 2020-04-04-18 00:18 00:18:18:38 | 86095361 | 2020- 04-18 00:06:01 |
2020-04-18 00:07:08 | 86095361 | 2020-04-18 00:06:01 |
retweet_datetime从最新转发到最新转发。
我想创建两个新列,如下所示:
-
tweet_lifetime1
:上次转推时间与第一个转推时间之间的区别,即,对于每个Tweet_id:last retweet_dateTime -first retweet_dateet_datect_dateetimetweet_lifetime
tweet_lifetime2
- tweet_lifetime2
/code>:上次转推时间和推文创建时间(
tweet_datetime
)之间的
更新
区别 示例,对于推文ID:“ 86095361” :
-
tweet_lifetime1
=2020-04-18 04:24:03-2020-2020-2020-04-18 00:07:07: 08
(04:16:55
) -
tweet_lifetime2
= = =2020-04-18 04:24:03-2020-04-18 00:06:01
(04:18:02
)
预期的输出DF:
retweet_dateTime | Tweet_id tweet_id | tweet_dateet_dateet_id tweet_dateTime | Lifetime1 | Lifetime2 |
---|---|---|---|---|
2020-04-24 03:33:15 | 85053699 | 2020-04-24 02:28:22 | 00:49:40 | 01:04:53 |
2020-04-18 04:24:03 | 86095361 | 2020-04-18 004-18 00:06:01 | 04:16:55 | 04:18:02 |
我见过几个类似的帖子,但它们主要减去连续的行。例如,我可以为每个Tweet ID减去每个retweet_datetimes之间的时间差:如下:
df2 = df.assign(delta = df.groupby('tweet_id')['retweet_datetime'].diff())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 noflowl noreferrer“> nater gotnegation” href =“ http://pandas.pydata.org/pandas-docs/stable/reference/reference/pandas.series.sub.html” rel =“ nofollow noreferrer”> a>, A>用于Drop列
TMP
处理后:如果需要格式
HH:MM:SS
使用:Use named aggregation with subtract column with
Series.sub
,DataFrame.pop
is used for drop columntmp
after processing:If need format
HH:MM:SS
use:GroupBy和Agg带有
首先
要选择每个组的第一行,然后使用的最后一个
,以选择每个组的最后一个值。然后JOIN
汇总的数据框架并计算Lifetime1
和LifeTime2
通过减去相关列Groupby and agg with
first
to select the first row per group, then groupby and aggretweet_datetime
withlast
to select last value per group. Thenjoin
aggregated dataframes and calculatelifetime1
andlifetime2
by subtracting relevant columns