
样本数据
我有一个R代码,我想把它转换成类似的python代码。这是R代码片段:
data$total <- ifelse(data$dpd_gt_30 == 1 , rowSums(data[0:2]),
ifelse(data$dpd_gt_30 == 3 , rowSums(data[2:4]),1000))我所做的是:
data['total']=data.iloc[:, 0:2].sum(axis=1).where(data['dpd_gt_30'] == 1,1000)我如何添加多个条件(如果需要的话超过2个)?
编辑:
我按照指示做了以下事情:
conds = [
df['dpd_gt_30'] == 1,
df['dpd_gt_30'] == 3,
df['dpd_gt_30'] not in [1,3]
]
choices = [
df.iloc[:,0:2].sum(axis=1),
df.iloc[:,2:4].sum(axis=1),
1000
]
df['Total'] = np.select(conds, choices)现在,我该如何处理那些不符合条件的值呢?(数值不是1或3的情况)
发布于 2020-12-14 17:00:14
使用np.where
data['total'] = np.where(data['dpd_gt_30']==1, data.iloc[:,:2].sum(axis=1),
data.iloc[:,2:].sum(axis=1)
)https://stackoverflow.com/questions/65293009
复制相似问题