我有一个如下所示的数据框
Unit_ID Added_Date Status
105 2019-10-02 08:14:16 Rented
106 2020-10-21 07:19:13 Rented
109 2020-01-02 07:18:19 Rented
108 2020-06-01 04:15:28 Vacant
从上述数据中,我想在“ Added_Date”列中过滤当天或当天的数据。
预期输出:
Unit_ID Added_Date Status
105 2019-10-02 08:14:16 Rented
109 2020-01-02 07:18:19 Rented
答案 0 :(得分:1)
您可以这样做
from datetime import datetime
# Make sure your date column is a date an not a string
df['Added_Date'] = pd.to_datetime(df['Added_Date'])
# Compare with today's date
df.loc[df['Added_Date'] > pd.Timestamp(datetime.now()), 'Added_Date']
输出:
1 2020-10-21 07:19:13
3 2020-06-01 04:15:28
Name: Added_Date, dtype: datetime64[ns]
pd.to_datetime()
和pd.Timestamp()
确保日期具有相同的格式,并且可以与标准的熊猫运算符进行比较。