Thursday 28 October 2021

Weighted average in pandas with weights based on the value of a column?

I have the following dataframe

 id        type    side       score       
 601166    p       right      2  
 601166    p       left       6        
 601166    p       right      2  
 601166    p       left       4      
 601166    r       left       2  
 601166    r       left       2  
 601166    r       right      6  
 601166                       2  
 601009    r       left       6  
 601009    r       right      8  
 601939    p       left       2  
 601939    p       left       2  

I have calculated the average score for each id, type and side with:

df_result=df.groupby(["id", "type","side"])["score"].mean()

 id        type    side       mean       
 601166    p       right      2  
 601166    p       left       5        
 601166    r       right      6  
 601166    r       left       2   
 601166                       2       

But now I would like to calculate the average score for each id and type and add weights to the average scores on each side: the lowest average score for the left or right side counts for 75%, the highest score for 25%.

An example result for id 601166, first calculate the average for each side. The side with the lowest score (right) counts for 75%, the other side (left) for 25%. Empty values can be skipped.

 id        type         mean       
 601166    p            2,75  
 601166    r            3  

Any idea how I can add this to my code?



from Weighted average in pandas with weights based on the value of a column?

No comments:

Post a Comment