Python 具有日期比较条件的非nan值的数据帧中的计数值

Python 具有日期比较条件的非nan值的数据帧中的计数值,python,pandas,dataframe,Python,Pandas,Dataframe,我有以下数据帧: Date_1 Date_2 Date_3 2019-12-18 13:43:47 2019-12-18 13:43:47 2019-12-18 13:43:48 2019-12-18 13:43:47 2020-12-18 17:51:17 2020-12-18 17:51:17 2020-12-18 17:51:17 2

我有以下数据帧:

Date_1                  Date_2                  Date_3
2019-12-18 13:43:47                             2019-12-18 13:43:47
2019-12-18 13:43:48     2019-12-18 13:43:47     
2020-12-18 17:51:17
2020-12-18 17:51:17     2020-12-18 17:51:17     2020-12-18 17:51:17
我试图计算每列中出现的值的数量,如果它们满足日期大于当前日期的条件

我的代码:

today=pd.Timestamp.today() - pd.Timedelta(days=1)

total_date_1_events = len([df['Date_1']>today])+1
total_date_2_events = len([df['Date_2']>today])+1
total_date_3_events = len([df['Date_3']>today])+1
如果我打印3个变量中的每一个,它们都会输出相同的结果,即4,我理解这是因为空行也会被计数

我希望得到以下结果:

total_date_1_events = 2 # because there are only 2 dates that are bigger than today
total_date_2_events = 1 # because there are only 1 date that is bigger than today
total_date_3_events = 1 # because there are only 1 date that is bigger than today
感谢您的建议。

只需执行以下操作:

sum(df.Date_1>pd.Timestamp.today())
sum(df.Date_1>pd.Timestamp.today())
sum(df.Date_1>pd.Timestamp.today())
方式和:


如果您需要更多专栏,您可以:

s = df[['Date_1','Date_2','Date_3']].gt(today).sum()
这将创建一个系列。您可以使用以下方式访问值:

s['Date_1']
s['Date_2'] 

len(df['Date\u 1']>today)
等于
len(df['Date\u 1'])
始终,您需要:
df['Date\u 1'].gt(today).sum().add(1)
df['Date\u 1']>today
返回一个带有True或False的序列(与
df['Date\u 1'].gt(today)
相同),但序列的长度是相同的…不确定
gt
代表什么以及它来自哪个包。
gt
相同,另一方面,您不需要放入list
len df['Date_1']>today)
是序列的长度等于
len(df)
但是
len([df['Date_1']>today])
是在列表的第一个元素中包含序列的列表的长度,也就是说,我得到以下错误:
AttributeError:'numpy.int64'对象没有属性'add'
抱歉,仅替换
。add(1)在这种情况下,我们不需要添加1,因为我们已经有了总数,谢谢!OKEY,但是我认为最好考虑<代码> S= DF[[ DATEY1,'DATEY2','DATEY3' ]]GT(今天).SUME(< /代码>,而写同一行N次:)我认为这是一个更干净的解决方案,而你使用的是<代码> []
访问列而不是我讨厌的属性样式!
s['Date_1']
s['Date_2']