Python – 基于另一列中的值获取两个colums的最大值还是最小值?

假设有以下数据的dataframe

key score1 score2 count 1 0.87 0.13 0 2 0.67 0.33 1 3 0.46 0.54 1 4 0.28 0.72 0 5 0.41 0.59 1 

如果count == 0或max [score1,score2] count> 0时,findmin [score1,score2]的最短途径是什么?

目前的解决scheme是

 data['mini']=data[[score1, score2]].min(axis=1) data['maxi']=data[[score1, score2]].max(axis=1) data['fin_score']= data['mini'].where(data['count']==0, data['maxi']) 

有没有办法可以做得更清脆(在一半的命令),就像在Excel中这将如下,然后拖动公式跨所有行

 =IF(count>0,MAX(B2:C2),MIN(B2:C2)) 

结果会是这样的

 key score1 score2 count fin_score 1 0.87 0.13 0 0.13 2 0.67 0.33 1 0.67 3 0.46 0.54 1 0.54 4 0.28 0.72 0 0.28 5 0.41 0.59 1 0.59 

Excel的IF函数等价于数组: np.where

 df['fin_score'] = np.where(df['count']==0, df[['score1', 'score2']].min(axis=1), df[['score1', 'score2']].max(axis=1)) df Out: key score1 score2 count fin_score 0 1 0.87 0.13 0 0.13 1 2 0.67 0.33 1 0.67 2 3 0.46 0.54 1 0.54 3 4 0.28 0.72 0 0.28 4 5 0.41 0.59 1 0.59 

为什么你需要额外的值存储在行?

 data['fin_score'] = (max if data['count'] else min)(map(lambda k: data['score' + k], ('1', '2')))