在Dataframe数据表中,如何实现SQL中的case when的功能:
比如SQL中case when写成:
case when a.等待时长<=1*12 then '1年以内'
when a.等待时长>1*12 and a.等待时长<=2*12 then '1-2年'
when a.等待时长>2*12 then '2年以上'
else '其他'
end as 等待时长划分
在Python中,Dataframe可以和lambda使用实现上面的功能:
df['等待时长划分'] = df.apply(lambda row:
'1年以内' if (row['等待时长']<=1*12)else
'1-2年' if (row['等待时长']>1*12) & (row['等待时长']<=2*12)else
'2年以上' if (row['等待时长']>2*12)else
'其他', axis=1)