I need to filter a dataframe down to reduce time updating user attributes.
+----------+------------+------------+
| 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 |
+----------+------------+------------+
above is an example of the dataframe. I need to filter it for any user that the LATEST date of either datecol1
or datecol2 is <= today-90 days
. In the above example the above dataframe should result in user2
and user4
left in the dataframe for processing.
The code I wrote (and haven't tested so I do not know if it works) doesn't filter the dataframe and instead tries to loop over the entire thing; here is the code.
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')
How can I filter prior to looping over the users to perform an action on the remaining users?
Use boolean indexing
with max
for latest datetime:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.