簡體   English   中英

使用日期比較條件計算非nan值的pandas數據幀中的值

[英]Counting values in pandas dataframe of non-nan values with date comparison condition

我有以下數據框:

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的條件,我正在嘗試計算每列中存在的值的數量。

我的代碼:

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())

熊貓方式Series.sumSeries.gt

df['Date_1'].gt(today).sum()

如果您需要更多列,您可以這樣做:

s = df[['Date_1','Date_2','Date_3']].gt(today).sum()

這創建了一個系列。 您可以使用以下方法訪問值:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM