在lamda函数中如何使用多个if?

时间:2019-06-19 14:25:13

标签: python-3.x pandas numpy datetime lambda

在日期框架中,我有“日期”列,如果日期> CurrentDate(datetime.now())将状态更新为“过期”,如果日期

Date              Status
2019-06-28      SW Expires 2018-Q2
2020-01-20      SW Expires 2018-Q1
2018-07-18      SW Expired
X               Current
Y               Current
2019-06-20      Current


df['Qtr'] = pd.to_datetime(df['Date']).dt.quarter
df['Qtr']=pd.PeriodIndex(pd.to_datetime(df['Date']), freq='Q')
df['Qtr']=df['Qtr'].astype(str)
df['Qtr']="SW Expires"+' '+df['Qtr']


df['Status'] = df['Date'].apply (lambda x : 'SW Expired' if x > 
CurrentDate else(Midrange_SWProduct['Qtr'] x < CurrentDate else('current' 
Midrange_SWProduct['Qtr'] x == CurrentDate or 'X' or 'Y' else NA )))

1 个答案:

答案 0 :(得分:0)

最好的方法是两次编写自定义函数和调用Apply函数。

def custom_funct(row):
     do anthing
df['res']=df.apply(lambda row: custom_funct(row),axis=1)