我df1有
Acc_id Acc_name Acc_end_date
qqq-1 test1 12/31/2021
www-2 test2 05/28/2022
yyy-6 test3 06/15/2022
zzz-6 test4 06/17/2022
kkk-6 test5 03/16/2022
Acc_end_date格式為 mm/dd/yy 。
我試圖只獲得那些Acc_end_date- 今天日期> 30的帳戶。
意思是,從今天開始應該經過 30 天才Acc_id能被選中。
我嘗試了以下
# changing the column to datetime format
df1['Acc_end_date']= pd.to_datetime(df1['Acc_end_date'])
# getting todays date and creating an array with those number of Acc_id and substracting
todays_date = []
for i in range(df1.shape[0]):
temp = date.today()
todays_date.append(temp)
np.array(todays_date)
df2 = df1[df1['Acc_end_date'] - todays_date>30]
我收到錯誤訊息
TypeError: unsupported operand type(s) for -: 'DatetimeArray' and 'list'
給出錯誤為
TypeError: unsupported operand type(s) for -: 'DatetimeArray' and 'datetime.date'
試試這個
df2 = df1[only_inactive['Acc_end_date'] - date.today()>30]
我無法獲得正確的日期和正確的減法格式。
請幫助任何方法
uj5u.com熱心網友回復:
IIUC,您可以使用:
df2 = df1[pd.to_datetime(df1['Acc_end_date'], dayfirst=False)
.rsub(pd.Timestamp('today')).gt('30d')]
輸出:
Acc_id Acc_name Acc_end_date
0 qqq-1 test1 12/31/2021
4 kkk-6 test5 03/16/2022
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/493401.html
上一篇:按索引和列值過濾Pandas
