我有以下数据框:
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())
关于python - 具有日期比较条件的非纳米值的 Pandas 数据框中的计数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60323683/