我有一个变量"date_0";其对应于某个时期的感染日期,并且我必须创建另一个变量";date_ 1";从具有日期条件(>=33天(的上一个变量(date_0("date_0";以及";date_ 1";格式为%Y-%m-%d%H:%m:%S。所以基本上我应该做一个这样的日期条件:
if df['date_1] >= 33 days then df['date_1] else df['date_0']
我这样做:
df['date_1']= np.where((df['date_1']>= 33d), df['date_1'],df['date_O'])
我也尝试过这个:
df['date_1']= np.where((df['date_1'].days>= 33), df['date_1'],df['date_O'])
但我失败了,你能帮帮我吗?
您可以使用相当于np.where的panda。例如:
import pandas as pd
df = pd.DataFrame({'date_0': [pd.Timestamp('2022-01-01'), pd.Timestamp('2022-02-01')],
'date_1': [pd.Timestamp('2022-01-29'), pd.Timestamp('2022-03-15')]})
df['days_diff'] = (df['date_1']-df['date_0']).dt.days
# date_select:
# use date 1 if the difference is greater equal 33 days else use date 0
df['date_select'] = df['date_1'].where(df['days_diff'] >= 33, df['date_0'])
df
date_0 date_1 days_diff date_select
0 2022-01-01 2022-01-29 28 2022-01-01
1 2022-02-01 2022-03-15 42 2022-03-15