gpt4 book ai didi

python-3.x - 如何比较多逻辑语句中数据帧之间的日期时间?

转载 作者:行者123 更新时间:2023-12-02 00:55:10 26 4
gpt4 key购买 nike

我在比较来自多逻辑语句内部的两个数据帧之间的日期时遇到问题。

df1:

 EmailAddress     DateTimeCreated
1@1 2019-02-12 20:47:00

df2:

 EmailAddress     DateTimeCreated
1@1.com 2019-02-07 20:47:00
2@2.com 2018-11-13 20:47:00
3@3.com 2018-11-04 20:47:00

每当 df1 中有一行时,我想做三件事:

 1. Compare to see if `EmailAddress` from df1 is present in df2:
1a. If `EmailAddress` is present, compare `DateTimeCreated` in df1 to `DateTimeCreated` in df2,
2. If `DateTimeCreated` in df1 is greater than today-90 days append df1 into df2.

简单来说:

我想查看 df2 中是否存在电子邮件地址,如果存在,请比较 df2 中创建的 datetime,看看它是否大于 today-90days since last time person answer。如果超过 90 天,则将 df1 中的行附加到 df2 中。

我的逻辑是附加所有不确定我做错了什么的东西:

import pandas as pd
from datetime import datetime, timedelta

df2.append(df2.loc[df2.EmailAddress.isin(df1.EmailAddress)&(df2.DateTimeCreated.ge(datetime.today() - timedelta(90)))])

搞砸约会我做错了什么?

编辑:

在上面的示例中,在数据帧之间,不会附加来自 df1 的行,因为 DateTimeCreated 在 TODAY() - 90 天之间。

最佳答案

解释请引用内联评论。请注意,您需要重命名 df1 列以匹配此解决方案中的 df2 列。

import pandas as pd
import datetime

from datetime import timedelta, datetime

df1 = pd.DataFrame({'EmailAddress':['2@2.com'], 'DateTimeCreated':[datetime(2019,2,12,20,47,0)]})
df2 = pd.DataFrame({'EmailAddress':['1@1.com', '2@2.com', '3@3.com'],
'DateTimeCreated':[
datetime(2019,2,7,20,47,0),
datetime(2018,11,13,20,47,0),
datetime(2019,11,4,20,47,0)]})

# Get all expired rows
df3 = df2.loc[datetime.now() - df2['DateTimeCreated'] > timedelta(days=90)]
# Update it with the timestamp from df1
df3 = df3.set_index('EmailAddress').join(df1.set_index('EmailAddress'), how='inner', rsuffix='_r')
df3.drop('DateTimeCreated', axis=1, inplace=True)
df3.columns = ['DateTimeCreated']
# Patch df2 with the latest timestamp
df2 = df3.combine_first(df2.set_index('EmailAddress')).reset_index()

# Patch again for rows in df1 that are not in df2
df1 = df1.loc[df1['EmailAddress'].apply(lambda x: 1 if x not in df2['EmailAddress'].tolist() else 0) == 1]
df2 = pd.concat([df2, df1])

>>>df2
EmailAddress DateTimeCreated
0 1@1.com 2019-02-07 20:47:00
1 2@2.com 2019-02-12 20:47:00
2 3@3.com 2019-11-04 20:47:00

关于python-3.x - 如何比较多逻辑语句中数据帧之间的日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54777328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com