gpt4 book ai didi

python - 使用 OR 根据两列过滤数据框

转载 作者:行者123 更新时间:2023-11-30 21:52:57 25 4
gpt4 key购买 nike

我需要过滤数据帧以减少更新用户属性的时间。

+----------+------------+------------+
| userCol1 | dateCol1 | dateCol2 |
+----------+------------+------------+
| user1 | 2020-01-16 | 2019-12-30 |
| user2 | 2019-10-31 | 2020-01-12 |
| user3 | 2019-08-15 | 2019-09-30 |
| user4 | 2019-08-25 | NaN |
+----------+------------+------------+

上面是数据框的示例。我需要为 datecol1 的最新日期的任何用户过滤它或datecol2 is <= today-90 days 。在上面的例子中,上面的数据帧应该产生 user2user4留在数据框中进行处理。

我编写的代码(尚未测试,所以我不知道它是否有效)不会过滤数据帧,而是尝试循环整个内容;这是代码。

 for row in df3.itertuples() :
print(row.username)
print(row.Password_Last_Set)
print(row.Password_Last_forgot)
if row.Password_Last_Forgot is 'NaN' and row.Password_Last_Set <= today.timedelta(days=90) :
print('password expired based on last set, no forgot passwords')
elif row.Password_Last_Forgot is not 'NaN' and row.Password_Last_Forgot > row.Password_Last_Set and row.Password_Last_Forgot <= today.timedelta(days=90) :
print('password expired based on last forgot')
elif row.Password_Last_Forgot is not 'NaN' and row.Password_Last_Forgot < row.Password_Last_Set and row.Password_Last_Set <= today.timedelta(days=90) :
print('password expired based on last set')

如何在循环用户以对剩余用户执行操作之前进行过滤?

最佳答案

使用boolean indexing使用 max 表示最新日期时间:

df[['dateCol1','dateCol2']] = df[['dateCol1','dateCol2']].apply(pd.to_datetime)

cols = ['dateCol1','dateCol2']
df1 = df.loc[df[cols].max(axis=1)<=pd.Timestamp.now() - pd.Timedelta(90, unit='d'), 'userCol1']
print (df1)
2 user3
3 user4
Name: userCol1, dtype: object

关于python - 使用 OR 根据两列过滤数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59782290/

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